#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Drawing an arc

edited June 2012 Posts: 1

Is there any way I can draw an arc?
I want to draw some angles and having a function that draws an arc would be very helpful.

PS: sorry for bad english, english is not my first language.

Tagged:

• Posts: 1,285

Hi Ghabriel,

Not as far as I know. An arc/ part circle or bezier curve would be a great addition to Codea.

By the way you English is excellent.

Brig_G • edited June 2012 Posts: 80

You could draw an arc if you clip an ellipse...

Like this (paste in draw() in a new project)

``````    noFill()
clip(WIDTH/2,HEIGHT/2,200,200)
ellipse(WIDTH/2, HEIGHT/2,400)
clip()

``````

That means you define that you only draw a part of the ellipse...

• Posts: 159

I've faked drawing an arc in my code by drawing a load of ellipses. I draw the arc to an image as I presume this is cheaper to draw repeatedly.

``````    ARC_IMAGE = image(180,180)
setContext(ARC_IMAGE)
ellipseMode(CENTER)

fill(255,0,0,255)
noStroke()
local cos = math.cos
local sin = math.sin
for i = 0,45 do
local angle = i*90
local x = 55 * cos( rad(angle) )
local y = 55 * sin( rad(angle) )
ellipse(x+90,y+90,10,10)
end

setContext()
``````
• Posts: 65

"By the way you English is excellent."

I lolled. Was that intentional? • Posts: 2,161

I have some code for rendering bezier curves. I'll dig it out later (it's buried in a project so I'll need to extract the code into something easy to use independently).

• Posts: 93
``````function vecline(dva,dvb)
va = dva *size --+ offset
vb = dvb * size --+ offset
line(va.x,va.y,vb.x,vb.y)
end

function jointlimb(pointa,pointb,lengthlimit,leanr,multiplier,curves)
if curves == nil then
curves = 1
end

if multiplier == nil then
curves = 1
end

arc =   -pointa + pointb
if arc:len() > lengthlimit then
arc = arc:normalize() * lengthlimit
curves = 0
else
offset = ((lengthlimit - arc:len()))*arc:normalize():rotate90()/2

end

arcdraw = arc/(curves+1)

if curves == 0 then

vecline(pointa,pointa+arcdraw)
else
curvepart=(curves+1)/2
pointe = pointa
pointd = pointe
for i=0,curves+1 do
pointc = pointa + arcdraw*(i)
pointc = pointc + offset*quadslide*multiplier

if math.floor(i/2) == i/2 then
pointe = pointc
else
pointd= pointc
end
vecline(pointd,pointe)
end
end
end
``````
• edited June 2012 Posts: 1,285

Hi @peteroyle,

Nah, that wasn't intenshinul, just my keyboard dyslexia!!!

Andrew, any code on curves or arcs would be appreciated - thanks.

Bri_G • Posts: 489

Below is a Codea version 1.5 suggestion, making use of an example shader:

```--
-- Arc
--

supportedOrientations(LANDSCAPE_ANY)
function setup()
cx, cy = WIDTH / 2, HEIGHT / 2
parameter.integer("width", 1, 100, 40)
parameter.integer("radius", 100, WIDTH / 2, 200)
parameter.integer("startAngle", -180, 180, -130)
parameter.integer("endAngle", -180, 180, 130)
stroke(118, 223, 25)
end

function draw()
background(64)
strokeWidth(width)
arc(cx, cy, radius, startAngle, endAngle)
end

function arc(x, y, radius, a1, a2)
local m = mesh()
m.shader.size = (1 - strokeWidth()/radius) * 0.5
m:draw()
end
```
• Posts: 12

(sigh) Alright, well this is the Questions section of the forum, and I'd been asking about arcs before. I like the arc function provided, and have reworked it slightly in the code below which lets you draw a simple two-part pie chart, with customisable colours, fraction of one colour and the other, and radius.

What I do not understand, still, is why the code doesn't also display a blue semicircle over the pie chart. I took the code I'd written to try using the Arc shader, and then used @mpilgrem's arc function, then tried to make my construction and invocation of the mesh and shader match. And I still don't see my mesh. What am I missing here?

Richard

``````-- Shader Pie Chart

-- Use this function to perform your initial setup
displayMode(STANDARD)

function setup()
parameter.number("Fraction",0,1,0.5)
parameter.color("PartOne",255,255,0)
parameter.color("PartTwo",255,0,0)

mymesh = mesh()

end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)
-- rotate(90)
local cx,cy, radius, startAngle, endAngle
cx = WIDTH/2
cy = HEIGHT/2
stroke(PartOne)
startAngle = -180
endAngle = 360*Fraction-180
arc(cx, cy, Radius, startAngle, endAngle)
stroke(PartTwo)
startAngle = endAngle
endAngle = 180
arc(cx, cy, Radius, startAngle, endAngle)

-- Configure out custom uniforms for the ripple shader
-- Attempt to draw a blue semicircle using a standalone, re-used mesh.
-- Draw the mesh
mymesh:draw()
end

--
-- Arc
--

function arc(x, y, radius, a1, a2)
local m = mesh()