DxDrawCircle: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "{{Useful Function}} <lowercasetitle/> __NOTOC__ This function draws a 2D line in a circle shape on the screen - rendered for one frame. This should be used in conjunction with on...")
 
(48 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Useful Function}}
{{Client Function}}
<lowercasetitle/>
<lowercasetitle/>
__NOTOC__
__NOTOC__
This function draws a 2D line in a circle shape on the screen - rendered for one frame. This should be used in conjunction with onClientRender in order to display it continuously.
{{New feature/item|3.0156|1.5.5|13977|This function draws a circle shape on the screen - rendered for '''one''' frame. This should be used in conjunction with [[onClientRender]] in order to be display continuously.}}


==Syntax==
==Syntax==
<syntaxhighlight lang="lua">void dxDrawCircle( int posX, int posY[, int radius, int, width, int angleAmount, int red, int green, int blue, int alpha, bool postGUI ] )</syntaxhighlight>
<syntaxhighlight lang="lua">bool dxDrawCircle ( float posX, float posY, float radius [, float startAngle = 0.0, float stopAngle = 360.0, color theColor = white,
                    color theCenterColor = theColor, int segments = 32, int ratio = 1, bool postGUI = false ] )</syntaxhighlight>


===Required Arguments===
===Required Arguments===
[[Image:DxDrawCircle_Saml1er.png|thumb|An example of how dxDrawCircle function works in practice.]]
* '''posX''': An integer representing the '''absolute''' X position of the circle center, represented by pixels on the screen.
* '''posX''': An integer representing the '''absolute''' X position of the circle center, represented by pixels on the screen.
* '''posY''': An integer representing the '''absolute''' Y position of the circle center, represented by pixels on the screen.
* '''posY''': An integer representing the '''absolute''' Y position of the circle center, represented by pixels on the screen.
* '''radius''': An integer representing the radius scale of the circle that is being drawn.


===Optional Arguments===
===Optional Arguments===
{{OptionalArg}}
{{OptionalArg}}
* '''radius''': An integer representing the radius scale of the circle that is being drawn.
* '''startAngle''': An integer representing the angle of the first point of the circle.
* '''width''': An integer representing the width of the line that is being drawn.
* '''stopAngle''': An integer representing the angle of the last point of the circle.
* '''angleAmount''': An integer representing the tightness of the circle. Lower amount makes it smoother, higher amount makes it more of a clock looking circle.
* '''theColor''': An integer of the hex color, produced using [[tocolor]] or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
* '''red''': An integer representing the amount of red color.
* '''theCenterColor''': An integer of the hex color, produced using [[tocolor]] or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
* '''green''': An integer representing the amount of green color.
* '''segments''': An integer ranging from 3-1024 representing how many triangles are used to form the circle, more segments = smoother circle. Note: using lots of segments may cause lag.
* '''blue''': An integer representing the amount of blue color.
* '''ratio''': Ratio between width and height, e.g: 2 would mean that the width of the circle is 2 times the height.
* '''alpha''': An integer representing the amount of transparency.
* '''postGUI''': A bool representing whether the circle should be drawn on top of or behind any ingame GUI (rendered by CEGUI).
* '''postGUI''': A bool representing whether the line should be drawn on top of or behind any ingame GUI (rendered by CEGUI).
 
===Returns===
Returns ''true'' if the creation of the 2D circle was successful, ''false'' otherwise.


==Code==
==Example==
<section name="Clientside Script" class="client" show="true">
This function draws a rectangle with rounded corners.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local deg2rad = math.pi/180
function dxDrawRoundedRectangle(x, y, rx, ry, color, radius)
local rad2deg = 180/math.pi
    rx = rx - radius * 2
    ry = ry - radius * 2
    x = x + radius
    y = y + radius


local function clamp(val, lower, upper)
    if (rx >= 0) and (ry >= 0) then
if (lower > upper) then lower, upper = upper, lower end
        dxDrawRectangle(x, y, rx, ry, color)
return math.max(lower, math.min(upper, val))
        dxDrawRectangle(x, y - radius, rx, radius, color)
end
        dxDrawRectangle(x, y + ry, rx, radius, color)
        dxDrawRectangle(x - radius, y, radius, ry, color)
        dxDrawRectangle(x + rx, y, radius, ry, color)


function dxDrawCircle(posX, posY, radius, width, angleAmount, red, green, blue, alpha, postGUI)
        dxDrawCircle(x, y, radius, 180, 270, color, color, 7)
radius = radius or 50
        dxDrawCircle(x + rx, y, radius, 270, 360, color, color, 7)
width = width or 5
        dxDrawCircle(x + rx, y + ry, radius, 0, 90, color, color, 7)
angleAmount = angleAmount or 1
        dxDrawCircle(x, y + ry, radius, 90, 180, color, color, 7)
red = red or 255
    end
green = green or 255
blue = blue or 255
alpha = alpha or 255
postGUI = postGUI or false
for i=0,360,angleAmount do
local _i = i*deg2rad
addEventHandler("onClientRender", root,
function()
dxDrawLine(math.cos(_i)*(radius-width)+x, math.sin(_i)*(radius-width)+y, math.cos(_i)*(radius+width)+x, math.sin(_i)*(radius+width)+y, tocolor(red, green, blue, alpha), width, postGUI)
end
)
end
end
end
dxDrawRoundedRectangle(350, 50, 100, 100, tocolor(0, 255, 0, 255), 20)
</syntaxhighlight>
</syntaxhighlight>
</section>


==Example==
==See Also==
<section name="Client" class="client" show="true">
{{Drawing_functions}}
This example draws a circle shape in the middle of the screen on resource start.
<syntaxhighlight lang="lua">
local sx, sy = guiGetScreenSize()
 
addEventHandler("onClientResourceStart", resourceRoot,
function()
dxDrawCircle(sx/2, sy/2)
end
)
</syntaxhighlight>
</section>


'''Author:''' Socialz
[[hu:dxDrawCircle]]
 
==See Also==
{{Useful_Functions}}

Revision as of 12:14, 6 March 2019

This function draws a circle shape on the screen - rendered for one frame. This should be used in conjunction with onClientRender in order to be display continuously.

Syntax

bool dxDrawCircle ( float posX, float posY, float radius [, float startAngle = 0.0, float stopAngle = 360.0, color theColor = white,
                    color theCenterColor = theColor, int segments = 32, int ratio = 1, bool postGUI = false ] )

Required Arguments

An example of how dxDrawCircle function works in practice.
  • posX: An integer representing the absolute X position of the circle center, represented by pixels on the screen.
  • posY: An integer representing the absolute Y position of the circle center, represented by pixels on the screen.
  • radius: An integer representing the radius scale of the circle that is being drawn.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • startAngle: An integer representing the angle of the first point of the circle.
  • stopAngle: An integer representing the angle of the last point of the circle.
  • theColor: An integer of the hex color, produced using tocolor or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
  • theCenterColor: An integer of the hex color, produced using tocolor or 0xAARRGGBB (AA = alpha, RR = red, GG = green, BB = blue).
  • segments: An integer ranging from 3-1024 representing how many triangles are used to form the circle, more segments = smoother circle. Note: using lots of segments may cause lag.
  • ratio: Ratio between width and height, e.g: 2 would mean that the width of the circle is 2 times the height.
  • postGUI: A bool representing whether the circle should be drawn on top of or behind any ingame GUI (rendered by CEGUI).

Returns

Returns true if the creation of the 2D circle was successful, false otherwise.

Example

This function draws a rectangle with rounded corners.

function dxDrawRoundedRectangle(x, y, rx, ry, color, radius)
    rx = rx - radius * 2
    ry = ry - radius * 2
    x = x + radius
    y = y + radius

    if (rx >= 0) and (ry >= 0) then
        dxDrawRectangle(x, y, rx, ry, color)
        dxDrawRectangle(x, y - radius, rx, radius, color)
        dxDrawRectangle(x, y + ry, rx, radius, color)
        dxDrawRectangle(x - radius, y, radius, ry, color)
        dxDrawRectangle(x + rx, y, radius, ry, color)

        dxDrawCircle(x, y, radius, 180, 270, color, color, 7)
        dxDrawCircle(x + rx, y, radius, 270, 360, color, color, 7)
        dxDrawCircle(x + rx, y + ry, radius, 0, 90, color, color, 7)
        dxDrawCircle(x, y + ry, radius, 90, 180, color, color, 7)
    end
end
dxDrawRoundedRectangle(350, 50, 100, 100, tocolor(0, 255, 0, 255), 20)

See Also