Hello guys. I want to draw a filled circle. Someone has the code to do this?
[code]local function drawCircle( x, y, radius, smoothness, color )
local circle = {}
for i = 0, 360, smoothness do
circle[ #circle + 1 ] =
{
x = x + math.sin( -math.rad( i ) ) * radius,
y = y + math.cos( -math.rad( i ) ) * radius,
}
end
draw.NoTexture()
surface.SetDrawColor( color )
surface.DrawPoly( circle )
end[/code]
Smoothness is basically a number between 1 and 360.
It could be done more efficiently tho, this is a cut from an addon I made about 2 years ago.
There's an example in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/DrawPoly]surface.DrawPoly[/url]. I think the seg (or 'smoothness' in Netheous' example) would slow down rendering if it's too high.
By the way, out of personal interest, is there any code to draw a percentage circle (that doesn't look like it's missing a degree)? I mean a circle that has a bit of it in a different colour in the way GTA V's recording circle does.
[QUOTE=MPan1;48736184]There's an example in [IMG]http://wiki.garrysmod.com/favicon.ico[/IMG] [URL="http://wiki.garrysmod.com/page/surface/DrawPoly"]surface.DrawPoly[/URL]. I think the seg (or 'smoothness' in Netheous' example) would slow down rendering if it's too high.
By the way, out of personal interest, is there any code to draw a percentage circle (that doesn't look like it's missing a degree)? I mean a circle that has a bit of it in a different colour in the way GTA V's recording circle does.[/QUOTE]
Not neccessairly.
Any polygon with too many corners will affect performance - just increase number in smoothness until you start seeing difference, then lower it by 1 and leave it like that - that's the highest quality you can get for that specific circle of that specific size.
[editline]22nd September 2015[/editline]
If you want the best circle drawn without it damaging your performance at all (or notably) then use a material, if you want it to be like a pie chart then use UV rectangles.
if you just want to draw a filled circle you can use a texture and surface.DrawTexturedRect
[editline]22nd September 2015[/editline]
[QUOTE=MPan1;48736184]There's an example in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/DrawPoly]surface.DrawPoly[/url]. I think the seg (or 'smoothness' in Netheous' example) would slow down rendering if it's too high.
By the way, out of personal interest, is there any code to draw a percentage circle (that doesn't look like it's missing a degree)? I mean a circle that has a bit of it in a different colour in the way GTA V's recording circle does.[/QUOTE]
this code made by _kilburn
[lua]
----------------------------------------------------------------------
-- Purpose:
-- Paints a circle. CREDITS: _Kilburn
----------------------------------------------------------------------
local function drawSection(x, y, w, h, s, a0, a1, cw)
if cw then a0, a1 = a1, a0 end
a0 = math.tan(math.rad(a0 - s.ang))
a1 = math.tan(math.rad(a1 - s.ang))
local u1, v1 = 0.5 + s.dx + s.dy * a1, 0.5 + s.dy - s.dx * a1
local u2, v2 = 0.5 + s.dx + s.dy * a0, 0.5 + s.dy - s.dx * a0
surface.DrawPoly{
{
x = x + w * 0.5;
y = y + h * 0.5;
u = 0.5;
v = 0.5;
};
{
x = x + w * u1;
y = y + h * v1;
u = u1;
v = v1;
};
{
x = x + w * u2;
y = y + h * v2;
u = u2;
v = v2;
};
}
end
local function angDiff(a, b, cw)
local d
if cw then
d = b - a
else
d = a - b
end
d = math.NormalizeAngle(d)
if d < 0 then d = d + 360 end
return d
end
local function angBetween(x, a, b, cw)
return angDiff(x, a, cw) < angDiff(b, a, cw)
end
--[[
\ 2 /
3 x 1
/ 4 \
]]
local segs = {
{ang = 0, dx = 0.5, dy = 0};
{ang = 90, dx = 0, dy = -0.5};
{ang = 180, dx = -0.5, dy = 0};
{ang = -90, dx = 0, dy = 0.5};
}
function surface.drawSection(x, y, w, h, ang_start, ang_end, clockwise)
local ang_dist = angDiff(ang_end, ang_start, clockwise)
for _,s in ipairs(segs) do
local a0, a1
if clockwise then
a0, a1 = s.ang + 45, s.ang - 45
else
a0, a1 = s.ang - 45, s.ang + 45
end
local startsInSection = angBetween(ang_start, a0, a1, clockwise)
local endsInSection = angBetween(ang_end, a0, a1, clockwise)
local containsSection = angBetween(a0, ang_start, ang_end, clockwise) and angBetween(a1, ang_start, ang_end, clockwise)
if startsInSection or endsInSection then
if startsInSection and endsInSection and ang_dist <= 90 then
-- starts and ends within this section
drawSection(x, y, w, h, s, ang_start, ang_end, clockwise)
else
if startsInSection then
-- starts inside this section, ends outside
drawSection(x, y, w, h, s, ang_start, a1, clockwise)
end
if endsInSection then
-- starts outside this section, ends inside
drawSection(x, y, w, h, s, a0, ang_end, clockwise)
end
end
elseif containsSection then
-- traverses the entire section
drawSection(x, y, w, h, s, a0, a1, clockwise)
end
end
end
[/lua]
[QUOTE=Netheous;48736314]Not neccessairly.
Any polygon with too many corners will affect performance - just increase number in smoothness until you start seeing difference, then lower it by 1 and leave it like that - that's the highest quality you can get for that specific circle of that specific size.
[editline]22nd September 2015[/editline]
If you want the best circle drawn without it damaging your performance at all (or notably) then use a material, if you want it to be like a pie chart then use UV rectangles.[/QUOTE]
Can You send me an example of circle with materials?
Edit: There's anyway to do a "border progress bar" in the circle using materials? Or should I do with draw polygon?
Sorry, you need to Log In to post a reply to this thread.