display.newArc()

It would be very nice to have a factory function to draw an arc. Something like that:

display.newArc(x, y, width, height, start, stop)

x = x coordinate of the arc ellipse
y = y coordinate of the arc ellipse
width = width of the arc ellipse
height = height of the arc ellipse
start = angle to start the arc ellipse
stop = angle to stop the arc ellipse [import]uid: 8970 topic_id: 8549 reply_id: 308549[/import]

And also:

display.newEllipse(x, y, width, height)

display.newTriangle (x1, y1, x2, y2, x3, y3)

display.newQuadr (x1, y1, x2, y2, x3, y3, x3, y3)
As 2D is the corebusiness of Corona, these new functions would be legitimate… [import]uid: 8970 topic_id: 8549 reply_id: 30767[/import]

I just posted regarding an arc, then noticed this. I am happy to see others have interest in the same type of feature. [import]uid: 10008 topic_id: 8549 reply_id: 33664[/import]

You can actually append functions to display yourself if you really want.

Just copy this to the top of your main.lua or whatever:

 function display.newArc(x,y,w,h,s,e)  
 local xc,yc,xt,yt,cos,sin = x+w/2,y+h/2,0,0,math.cos,math.sin  
 s,e = s or 0, e or 360  
 s,e = math.rad(s),math.rad(e)  
 w,h = w/2,h/2  
 local l = display.newLine(0,0,0,0)  
 for t=s,e,0.02 do l:append(xc + w\*cos(t), yc - h\*sin(t)) end  
 return l  
 end  
 function display.newEllipse(x,y,w,h)  
 return display.newArc(x,y,w,h)  
 end  
 function display.newTriangle(x,y,x2,y2,x3,y3)  
 local l = display.newLine(x,y,x2,y2)  
 l:append(x3,y3,x,y)  
 return l  
 end  
 function display.newQuad(x,y,x2,y2,x3,y3,x4,y4)  
 local l = display.newLine(x,y,x2,y2)  
 l:append(x3,y3,x4,y4,x,y)  
 return l  
 end  
  

-Angelo [import]uid: 12822 topic_id: 8549 reply_id: 33782[/import]

Hey sweet! Thanks for the code Yobonja! [import]uid: 11636 topic_id: 8549 reply_id: 33791[/import]

No problem, Here’s an alternative Ellipse function that returns a filled in ellipse:

 function display.newEllipse(x,y,w,h)  
 local circle = display.newCircle(0,0,w/2)  
 local ellipse = display.newGroup()  
 ellipse:insert(circle)  
 ellipse:setReferencePoint(display.CenterReferencePoint)  
 ellipse.x, ellipse.y = w/2, h/2  
 ellipse.yScale = h/w  
 circle.x, circle.y = x, y+h  
 return circle  
 end  
  

strokeWidth isn’t as nice as it should be though.

Also, I forgot to take out some useless variables, so here’s the Arc code again:

 function display.newArc(x,y,w,h,s,e)  
 local xc,yc,cos,sin = x+w/2,y+h/2,math.cos,math.sin  
 s,e = s or 0, e or 360  
 s,e = math.rad(s),math.rad(e)  
 w,h = w/2,h/2  
 local l = display.newLine(0,0,0,0)  
 for t=s,e,0.02 do l:append(xc + w\*cos(t), yc - h\*sin(t)) end  
 return l  
 end  

-Angelo [import]uid: 12822 topic_id: 8549 reply_id: 33798[/import]

@Yobonja : Thanks for sharing! +++ [import]uid: 8970 topic_id: 8549 reply_id: 33799[/import]

Thanks for the wonderful tip Angelo!

Do you know an easy way to create a physics object for an arc as well? I attempted to use a polygon with multiple small faces, but it cause a lot of physics glitches (object sticking in object, etc)

I’ve also considered doing a circular sensor, and making my own check on the quadrant of the circle that the object is colliding with based on the event location, but it doesn’t seem like that would be very optimal. [import]uid: 10008 topic_id: 8549 reply_id: 33816[/import]

I would think that a bunch of small rects jointed together would work, but I don’t know. Do you need a quarter circle or an arc? If you just need a wedge, I think you can manage it with a custom shape. but you can’t do a concave shape, so for an arc I’d joint several rects together.

I also think the circular sensor might work depending on your situation, if you’re doing a view cone it should be fine, but I’m not sure how well it would work if you’re trying to pile cheese wedges. One way to find out though right?

-Angelo [import]uid: 12822 topic_id: 8549 reply_id: 34019[/import]

The problem with connecting the multiple small rects is that occasionally an object will get stuck between them. I tested this with a simple “pinball gun” system. When the ball was sent up and hit the inner curved edge, it would get occasionally caught in the edge between two rect. I even attempted to force overlap in each rect so there was no space between, but it still occurred. [import]uid: 10008 topic_id: 8549 reply_id: 34030[/import]

Is the isBullet property of the ball set to true?(it should be) I have some experience with a pinball like setup ~ we make curves out of smaller elements in some of our levels, and it seems to work fine, even at high speeds.

It all depends on what you’re trying to do, If the arcs ar not going to move, like they are just attached to the background, they don’t even need to be jointed together.

Also, you could modify the Arc function to make the arcs out of rects instead of a line, just be sure to adjust the granularity so you don’t make too many rects.

-Angelo [import]uid: 12822 topic_id: 8549 reply_id: 34055[/import]

I gave this code a try, and while it’s pretty cool - it ends up creating a pretty jagged arc, not smooth enough. Plus it would be great if it acted like a circle where we can fill in the arc slice with a fill color. Is there anything out there like this? [import]uid: 84239 topic_id: 8549 reply_id: 81050[/import]

I know this thread is over 3 years old, but I stumbled on it last week while trying to draw a pie / donut chart in one of our Corona apps.

Ended up using the code from @Yobonja but had to tweak it a little bit to fix some jagged edges I was seeing.

Here’s the modified code. Hopefully someone finds it useful.

Basically I just modified it to start the line at the Arc’s start coordinates, rather than at 0,0

function display.newArc(x,y,w,h,s,e) local xc,yc,cos,sin,l = x+w/2,y+h/2,math.cos,math.sin,nil s,e = s or 0, e or 360 s,e = math.rad(s),math.rad(e) w,h = w/2,h/2 for t=s,e,0.01 do local nx = xc + w\*cos(t) local ny = yc - h\*sin(t) if not l then l = display.newLine(nx, ny, nx, ny) else l:append(nx, ny) end end return l end

Hah cool! I had totally forgotten about this :slight_smile: I’m glad it helped you somehow.

-Angelo

I know this thread is over 3 years old, but I stumbled on it last week while trying to draw a pie / donut chart in one of our Corona apps.

Ended up using the code from @Yobonja but had to tweak it a little bit to fix some jagged edges I was seeing.

Here’s the modified code. Hopefully someone finds it useful.

Basically I just modified it to start the line at the Arc’s start coordinates, rather than at 0,0

function display.newArc(x,y,w,h,s,e) local xc,yc,cos,sin,l = x+w/2,y+h/2,math.cos,math.sin,nil s,e = s or 0, e or 360 s,e = math.rad(s),math.rad(e) w,h = w/2,h/2 for t=s,e,0.01 do local nx = xc + w\*cos(t) local ny = yc - h\*sin(t) if not l then l = display.newLine(nx, ny, nx, ny) else l:append(nx, ny) end end return l end

Hah cool! I had totally forgotten about this :slight_smile: I’m glad it helped you somehow.

-Angelo