Hello everyone! I've been looking around for a while, and I have yet to find out how to make circle. Any help?
I used suface.DrawCircle() but that just makes an outline
Thank you, and have a good day!
There's an example:
[url]http://wiki.garrysmod.com/page/surface/DrawPoly[/url]
Depending on the circumstances you're using it in you might want to just use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/DrawTexturedRectUV]surface.DrawTexturedRectUV[/url] with a circle material. Using the polygon method will result in lower quality circles (at least in my experience).
[QUOTE=Robotboy655;48454931]There's an example:
[url]http://wiki.garrysmod.com/page/surface/DrawPoly[/url][/QUOTE]
I'm gonna be completely honest, i'm new to coding and that page just mindfucks me.
This part is most confusing...
[CODE]
function draw.Circle( x, y, radius, seg )
local cir = {}
table.insert( cir, { x = x, y = y, u = 0.5, v = 0.5 } )
for i = 0, seg do
local a = math.rad( ( i / seg ) * -360 )
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )
end
local a = math.rad( 0 ) -- This is need for non absolute segment counts
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )
surface.DrawPoly( cir )
end
hook.Add( "HUDPaint", "PolygonCircleTest", function()
surface.SetDrawColor( 0, 0, 0, 200 )
draw.NoTexture()
draw.Circle( ScrW() / 2, ScrH() / 2, 200, math.sin( CurTime() ) * 20 + 25 )
--Usage:
--draw.Circle( x, y, radius, segments )
end )
[/CODE]
Is there something else I need to study/ read to understand this? If so, what is it?
[QUOTE=ExZPErience;48455637]I'm gonna be completely honest, i'm new to coding and that page just mindfucks me.
This part is most confusing...
[CODE]
function draw.Circle( x, y, radius, seg )
local cir = {}
table.insert( cir, { x = x, y = y, u = 0.5, v = 0.5 } )
for i = 0, seg do
local a = math.rad( ( i / seg ) * -360 )
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )
end
local a = math.rad( 0 ) -- This is need for non absolute segment counts
table.insert( cir, { x = x + math.sin( a ) * radius, y = y + math.cos( a ) * radius, u = math.sin( a ) / 2 + 0.5, v = math.cos( a ) / 2 + 0.5 } )
surface.DrawPoly( cir )
end
hook.Add( "HUDPaint", "PolygonCircleTest", function()
surface.SetDrawColor( 0, 0, 0, 200 )
draw.NoTexture()
draw.Circle( ScrW() / 2, ScrH() / 2, 200, math.sin( CurTime() ) * 20 + 25 )
--Usage:
--draw.Circle( x, y, radius, segments )
end )
[/CODE]
Is there something else I need to study/ read to understand this? If so, what is it?[/QUOTE]
if you're implying you don't understand [i]any[/i] of that code you posted, i suggest you learn glua. [url=http://facepunch.com/showthread.php?t=1337945]this thread[/url] should help a lot.
but, in a nutshell, that code constructs a polygon with points roughly the shape of a circle. the greater the number of points (in the segments argument), the more perfect the circle is. however, it also will start being intensive on your gmod if you're drawing too many at once, so it's ideal to use the lowest value possible to create an okay circle
[QUOTE=ExZPErience;48455637]
Is there something else I need to study/ read to understand this? If so, what is it?
[/QUOTE]
If you're talking about the math behind it its trig.
[url]https://www.khanacademy.org/math/geometry/right_triangles_topic/cc-geometry-trig[/url]
I know this is going to sound weird, I understand trigonometry, but this part of the trigonometry wiki doesn't make sense.
[CODE]
function PointOnCircle( ang, radius, offX, offY )
ang = math.rad( ang )
local x = math.cos( ang ) * radius + offX
local y = math.sin( ang ) * radius + offY
return x, y
end
local numSquares = 36 --How many squares do we want to draw?
local interval = 360 / numSquares
local centerX, centerY = 200, 500
local radius = 120
hook.Add( "HUDPaint", "Draw a circle of boxes!", function()
for degrees = 1, 360, interval do --Start at 1, go to 360, and skip forward at even intervals.
local x, y = PointOnCircle( degrees, radius, centerX, centerY )
draw.RoundedBox( 4, x, y, 30, 30, Color( 255, 255, 0 ) )
end
end )
[/CODE]
If someone can break it down ( the code above) and explain it a bit more, would be helpful. Thanks :)
bump
Take the advice mitterdoo gave instead of asking to.be spoonfed because no one will.
[QUOTE=ExZPErience;48469670]I know this is going to sound weird, I understand trigonometry, but this part of the trigonometry wiki doesn't make sense.
[CODE]
function PointOnCircle( ang, radius, offX, offY )
ang = math.rad( ang )
local x = math.cos( ang ) * radius + offX
local y = math.sin( ang ) * radius + offY
return x, y
end
local numSquares = 36 --How many squares do we want to draw?
local interval = 360 / numSquares
local centerX, centerY = 200, 500
local radius = 120
hook.Add( "HUDPaint", "Draw a circle of boxes!", function()
for degrees = 1, 360, interval do --Start at 1, go to 360, and skip forward at even intervals.
local x, y = PointOnCircle( degrees, radius, centerX, centerY )
draw.RoundedBox( 4, x, y, 30, 30, Color( 255, 255, 0 ) )
end
end )
[/CODE]
If someone can break it down ( the code above) and explain it a bit more, would be helpful. Thanks :)[/QUOTE]
first of all, it doesn't look like you're taking my advice. secondly, that code won't draw a circle. it will draw a bunch of squares in the shape of a circle, but not the desired filled circle.
i whipped up some code with comments explaining everything in as much detail as possible.
[lua]
function PointOnCircle( ang, radius, offX, offY ) -- defines a function that gets a vertex on a circle. ang parameter is in degrees
ang = math.rad( ang ) -- converts degrees into radians (3.14159... radians is equal to 180 degrees)
--[[
the next two lines of code gets the x/y pos using the angle, which returns a 2D normal,
then they multiply that normal by the radius to keep it the same distance from the origin,
finally they add the offset values so the vertices are always around the circle
in other words, the 'offX' and 'offY' paramters as the center of the circle
]]
local x = math.cos( ang ) * radius + offX
local y = math.sin( ang ) * radius + offY
return x, y -- return the positions
end
local numSquares = 36 --How many squares do we want to draw?
local interval = 360 / numSquares -- divides a full circle (360 degrees) into numSquares different segments
local centerX, centerY = 200, 500 -- the position of the circle's center on the screen
local radius = 120 -- radius of circle
hook.Add( "HUDPaint", "Draw a circle of boxes!", function() -- call this function every time the client's HUD is drawn
local verts = {} -- make a table to put the points of the circle into later
for degrees = 1, 360, interval do --Start at 1, go to 360, and skip forward at even intervals.
local x, y = PointOnCircle( degrees, radius, centerX, centerY ) -- gets the point on the circle using the angle, radius, and origin of it
table.insert( verts, { x = x, y = y } ) -- insert this point into the verts table
end
local circleColor = Color( 255, 0, 0 ) -- set the circle color to red
surface.SetDrawColor( circleColor ) -- set the color of everything drawn after this (until called again)
draw.NoTexture()
surface.DrawPoly( verts ) -- draw the circle
end )
[/lua]
if you're [i]still[/i] not understanding how this works, i don't know what to tell you. we're not going to have a full course of trig taught on facepunch.
[QUOTE=mitterdoo;48475659]first of all, it doesn't look like you're taking my advice. secondly, that code won't draw a circle. it will draw a bunch of squares in the shape of a circle, but not the desired filled circle.
i whipped up some code with comments explaining everything in as much detail as possible.
[lua]
function PointOnCircle( ang, radius, offX, offY ) -- defines a function that gets a vertex on a circle. ang parameter is in degrees
ang = math.rad( ang ) -- converts degrees into radians (3.14159... radians is equal to 180 degrees)
--[[
the next two lines of code gets the x/y pos using the angle, which returns a 2D normal,
then they multiply that normal by the radius to keep it the same distance from the origin,
finally they add the offset values so the vertices are always around the circle
in other words, the 'offX' and 'offY' paramters as the center of the circle
]]
local x = math.cos( ang ) * radius + offX
local y = math.sin( ang ) * radius + offY
return x, y -- return the positions
end
local numSquares = 36 --How many squares do we want to draw?
local interval = 360 / numSquares -- divides a full circle (360 degrees) into numSquares different segments
local centerX, centerY = 200, 500 -- the position of the circle's center on the screen
local radius = 120 -- radius of circle
hook.Add( "HUDPaint", "Draw a circle of boxes!", function() -- call this function every time the client's HUD is drawn
local verts = {} -- make a table to put the points of the circle into later
for degrees = 1, 360, interval do --Start at 1, go to 360, and skip forward at even intervals.
local x, y = PointOnCircle( degrees, radius, centerX, centerY ) -- gets the point on the circle using the angle, radius, and origin of it
table.insert( verts, { x = x, y = y } ) -- insert this point into the verts table
end
local circleColor = Color( 255, 0, 0 ) -- set the circle color to red
surface.SetDrawColor( circleColor ) -- set the color of everything drawn after this (until called again)
draw.NoTexture()
surface.DrawPoly( verts ) -- draw the circle
end )
[/lua]
if you're [i]still[/i] not understanding how this works, i don't know what to tell you. we're not going to have a full course of trig taught on facepunch.[/QUOTE]
Breaking it down helped me in understanding how it works. Don't worry, I understand trig XD.
Thanks
Sorry, you need to Log In to post a reply to this thread.