Skip to content

Instantly share code, notes, and snippets.

@theawesomecoder61
Last active May 5, 2024 15:44
Show Gist options
  • Save theawesomecoder61/d2c3a3d42bbce809ca446a85b4dda754 to your computer and use it in GitHub Desktop.
Save theawesomecoder61/d2c3a3d42bbce809ca446a85b4dda754 to your computer and use it in GitHub Desktop.
Draw arcs or pie charts/graphs in Garry's Mod. I did not make this, this is only re-uploaded for archival reasons.
-- Draws an arc on your screen.
-- startang and endang are in degrees,
-- radius is the total radius of the outside edge to the center.
-- cx, cy are the x,y coordinates of the center of the arc.
-- roughness determines how many triangles are drawn. Number between 1-360; 2 or 3 is a good number.
function draw.Arc(cx,cy,radius,thickness,startang,endang,roughness,color)
surface.SetDrawColor(color)
surface.DrawArc(surface.PrecacheArc(cx,cy,radius,thickness,startang,endang,roughness))
end
function surface.PrecacheArc(cx,cy,radius,thickness,startang,endang,roughness)
local triarc = {}
-- local deg2rad = math.pi / 180
-- Define step
local roughness = math.max(roughness or 1, 1)
local step = roughness
-- Correct start/end ang
local startang,endang = startang or 0, endang or 0
if startang > endang then
step = math.abs(step) * -1
end
-- Create the inner circle's points.
local inner = {}
local r = radius - thickness
for deg=startang, endang, step do
local rad = math.rad(deg)
-- local rad = deg2rad * deg
local ox, oy = cx+(math.cos(rad)*r), cy+(-math.sin(rad)*r)
table.insert(inner, {
x=ox,
y=oy,
u=(ox-cx)/radius + .5,
v=(oy-cy)/radius + .5,
})
end
-- Create the outer circle's points.
local outer = {}
for deg=startang, endang, step do
local rad = math.rad(deg)
-- local rad = deg2rad * deg
local ox, oy = cx+(math.cos(rad)*radius), cy+(-math.sin(rad)*radius)
table.insert(outer, {
x=ox,
y=oy,
u=(ox-cx)/radius + .5,
v=(oy-cy)/radius + .5,
})
end
-- Triangulize the points.
for tri=1,#inner*2 do -- twice as many triangles as there are degrees.
local p1,p2,p3
p1 = outer[math.floor(tri/2)+1]
p3 = inner[math.floor((tri+1)/2)+1]
if tri%2 == 0 then --if the number is even use outer.
p2 = outer[math.floor((tri+1)/2)]
else
p2 = inner[math.floor((tri+1)/2)]
end
table.insert(triarc, {p1,p2,p3})
end
-- Return a table of triangles to draw.
return triarc
end
function surface.DrawArc(arc) //Draw a premade arc.
for k,v in ipairs(arc) do
surface.DrawPoly(v)
end
end
@ksprugevics
Copy link

Pretty neat!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment