Display ellipse with an angle?

Hi,

I’m using current code to display an allipse:

  
 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)  
 l:setColor(54, 251, 9)  
 l.width = 4  
  
 mainGroup:insert( l )  
 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  
  
 Circle[Number] = display.newEllipse( Area[i].x - (Area[i].width / 2), Area[i].y-(Area[i].height / 2), Area[i].width, Area[i].height)  
  

This works perfectly, but now I want to display it with a certain angle/rotation.

I tried fiddling with it but can’t figure it out. Changing the s and e values didn’t work.

Anyone?
[import]uid: 50459 topic_id: 31973 reply_id: 331973[/import]

Circle[Number].rotation = angle; [import]uid: 105599 topic_id: 31973 reply_id: 127442[/import]

I tried that, but doesn’t work.

Probably because the ellipse is drawn in fucntion display.newArc, and you can’t change the rotation afterwards.
[import]uid: 50459 topic_id: 31973 reply_id: 127447[/import]

It worked fine for me, the origin was a bit wonky so the rotation point was not centered on the ellipse, I didn’t tackle the rotation-point issue in the algorithm, but the ellipse rotated fine.

Everything is being returned through a line object. You want to change the rotation property of this. It works.
Also, i wouldn’t do “mainGroup:insert( l )” inside your global display.newArc function. It very severely limits the utility. You should insert the object after the line object is returned.

mainGroup:insert( Circle[Number] ); [import]uid: 105599 topic_id: 31973 reply_id: 127449[/import]

@rmbsoft,

I think it should be as easy as changing this:

function display.newEllipse(x,y,w,h)  
 return display.newArc(x,y,w,h)  
end  
function display.newEllipse(x,y,w,h, angle)  
 local aGroup = display.newGroup()  
 local theArc = display.newArc(x,y,w,h)  
 aGroup:insert(theArc)  
 aGroup.rotation = angle  
 return aGroup  
end  

Cheers,
Ed
Roaming Gamer, LLC.
SSK for Corona SDK (github) (videos)

[import]uid: 110228 topic_id: 31973 reply_id: 127469[/import]

@rmbsoft,

OK, that wasn’t exactly right. I poked around at your code and made these changes:

 function display.newArc(group, x,y,w,h,s,e,rot) -- modification of original code by: rmbsoft (Corona Forums Member)  
 local theArc = display.newGroup()  
  
 local xc,yc,xt,yt,cos,sin = 0,0,0,0,math.cos,math.sin --w/2,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)  
 l:setColor(54, 251, 9)  
 l.width = 4  
  
 theArc:insert( l )  
  
 for t=s,e,0.02 do   
 local cx,cy = xc + w\*cos(t), yc - h\*sin(t)  
 l:append(cx,cy)   
 end  
  
 group:insert( theArc )  
  
 -- Center, Rotate, then translate   
 theArc.x,theArc.y = 0,0  
 theArc.rotation = rot  
 theArc.x,theArc.y = x,y  
  
 return theArc  
 end  
  
 function display.newEllipse(group, x, y, w, h, rot) -- modification of original code by: rmbsoft (Corona Forums Member)  
 return newArc(group, x, y, w, h, nil, nil, rot)  
 end  

Here the changes are in action.
Cheers,
Ed
Roaming Gamer, LLC.
SSK for Corona SDK (github) (videos)

[import]uid: 110228 topic_id: 31973 reply_id: 127479[/import]

@emaurina

Thanks, your solution seems to work perfectly for me!

@james309

Thanks for your help! [import]uid: 50459 topic_id: 31973 reply_id: 127492[/import]

Circle[Number].rotation = angle; [import]uid: 105599 topic_id: 31973 reply_id: 127442[/import]

I tried that, but doesn’t work.

Probably because the ellipse is drawn in fucntion display.newArc, and you can’t change the rotation afterwards.
[import]uid: 50459 topic_id: 31973 reply_id: 127447[/import]

It worked fine for me, the origin was a bit wonky so the rotation point was not centered on the ellipse, I didn’t tackle the rotation-point issue in the algorithm, but the ellipse rotated fine.

Everything is being returned through a line object. You want to change the rotation property of this. It works.
Also, i wouldn’t do “mainGroup:insert( l )” inside your global display.newArc function. It very severely limits the utility. You should insert the object after the line object is returned.

mainGroup:insert( Circle[Number] ); [import]uid: 105599 topic_id: 31973 reply_id: 127449[/import]

@rmbsoft,

I think it should be as easy as changing this:

function display.newEllipse(x,y,w,h)  
 return display.newArc(x,y,w,h)  
end  
function display.newEllipse(x,y,w,h, angle)  
 local aGroup = display.newGroup()  
 local theArc = display.newArc(x,y,w,h)  
 aGroup:insert(theArc)  
 aGroup.rotation = angle  
 return aGroup  
end  

Cheers,
Ed
Roaming Gamer, LLC.
SSK for Corona SDK (github) (videos)

[import]uid: 110228 topic_id: 31973 reply_id: 127469[/import]

@rmbsoft,

OK, that wasn’t exactly right. I poked around at your code and made these changes:

 function display.newArc(group, x,y,w,h,s,e,rot) -- modification of original code by: rmbsoft (Corona Forums Member)  
 local theArc = display.newGroup()  
  
 local xc,yc,xt,yt,cos,sin = 0,0,0,0,math.cos,math.sin --w/2,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)  
 l:setColor(54, 251, 9)  
 l.width = 4  
  
 theArc:insert( l )  
  
 for t=s,e,0.02 do   
 local cx,cy = xc + w\*cos(t), yc - h\*sin(t)  
 l:append(cx,cy)   
 end  
  
 group:insert( theArc )  
  
 -- Center, Rotate, then translate   
 theArc.x,theArc.y = 0,0  
 theArc.rotation = rot  
 theArc.x,theArc.y = x,y  
  
 return theArc  
 end  
  
 function display.newEllipse(group, x, y, w, h, rot) -- modification of original code by: rmbsoft (Corona Forums Member)  
 return newArc(group, x, y, w, h, nil, nil, rot)  
 end  

Here the changes are in action.
Cheers,
Ed
Roaming Gamer, LLC.
SSK for Corona SDK (github) (videos)

[import]uid: 110228 topic_id: 31973 reply_id: 127479[/import]

@emaurina

Thanks, your solution seems to work perfectly for me!

@james309

Thanks for your help! [import]uid: 50459 topic_id: 31973 reply_id: 127492[/import]