Last active
September 16, 2024 17:56
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice kyane, thanks!