Ellipse, rotated about an angle

Post Reply
Henko
Posts: 829
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Ellipse, rotated about an angle

Post by Henko » Mon Mar 11, 2019 7:01 pm

The standard sB function is not capable of drawing an ellipse under an angle.
This function does.
A88CF834-44BF-4924-9053-F6E43606E154.png
A88CF834-44BF-4924-9053-F6E43606E154.png (1.98 MiB) Viewed 55 times

Code: Select all

graphics ! graphics clear
get screen size sw,sh ! cx=sw/2 ! cy=sh/2
draw color 0,0,0 ! draw size 3
for phi=0 to 360 step 15
  ellipse(cx,cy,200,80,phi) ! pause .3
  next phi
stop

' draw ellipse rotated counterclockwise with phi degrees
' xf,yf = coordinates of left focal point
' ra,rb = horizontal and vertical radii (ra > rb supposed)
' phi   = rotation angle in degrees
' option angle in calling code segment is untouched
'
def ellipse(xf,yf,ra,rb,phi)
if sin(90)=1 then oa=1 else oa=0
option angle degrees
d_alfa=1
e=sqrt(1-rb*rb/ra/ra)
rho=ra*(1-e*e)/(1-e*cos(-phi))
x=xf+rho ! y=yf
draw to x,y
for alfa=d_alfa to 360 step d_alfa
  rho=ra*(1-e*e)/(1-e*cos(alfa-phi))
  x=xf+rho*cos(alfa) ! y=yf-rho*sin(alfa)
  draw line to x,y
  next alfa
if oa=1 then option angle degrees else option angle radians
end def

matt7
Posts: 91
Joined: Sun Jul 12, 2015 5:00 pm
My devices: iPhone 8, Windows
Location: Kentucky, USA

Re: Ellipse, rotated about an angle

Post by matt7 » Mon Mar 11, 2019 9:22 pm

I always forget about the DRAW TO and DRAW LINE TO functions! Nice use of them.

Because of the difference in screen resolutions, I did not get those thin gaps between line segments on my iPhone like in your screenshot. But if I make the DRAW SIZE larger then I do start to get some and part of the ellipse can appear transparent. But this can be avoided if you set DRAW LINECAP ROUND before calling the ellipse function.

User avatar
rbytes
Posts: 1906
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone X
iPad 4
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Ellipse, rotated about an angle

Post by rbytes » Mon Mar 11, 2019 10:52 pm

Another cool drawing effect. Thanks, Henk!
####### Living the colorful life #######

Henko
Posts: 829
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Ellipse, rotated about an angle

Post by Henko » Tue Mar 12, 2019 7:03 am

rbytes wrote:
Mon Mar 11, 2019 10:52 pm
Another cool drawing effect. Thanks, Henk!

Putting d_alfa=15 in the function (a very coarse ellipse) :

4D9190F9-D2D6-4126-A08F-1DEDBFD68310.png
4D9190F9-D2D6-4126-A08F-1DEDBFD68310.png (1.92 MiB) Viewed 35 times

Henko
Posts: 829
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Re: Ellipse, rotated about an angle

Post by Henko » Tue Mar 12, 2019 9:51 am

To finalize the ellipse "library", fuctions are added to draw or fill an ellips which is rotated about the centre of the ellips in stead of rotation about a focal point.
CAC989EF-2C5C-4AC2-80E2-152FD671711E.png
CAC989EF-2C5C-4AC2-80E2-152FD671711E.png (1.74 MiB) Viewed 32 times

Code: Select all

graphics ! graphics clear 
get screen size sw,sh ! cx=sw/2 ! cy=sh/2
draw color 0,0,0 ! draw size 3
for phi=45 to 315 step 90
  draw_ellipse(cx,cy,360,60,phi) ! pause .3
  fill_ellipse(cx,cy,300,80,phi+45,.5,.5,.5,.3)
  next phi
stop


' draw ellipse rotated counterclockwise with phi degrees
' xc,yc = coordinates of the centre of the ellipse
' ra,rb = horizontal and vertical radii (ra > rb supposed)
' phi   = rotation angle in degrees
' option angle in calling code segment is untouched
'
def draw_ellipse(xc,yc,ra,rb,phi)
if sin(90)=1 then oa=1 else oa=0
option angle degrees
dt=6 ! sp=sin(phi) ! cp=cos(phi)
draw to xc+ra*cos(phi),yc-ra*sin(phi)
for t=dt to 360 step dt
  st=sin(t) ! ct=cos(t)
  draw line to xc+ra*cp*ct-rb*sp*st,yc-ra*sp*ct-rb*cp*st
  next t
if oa=1 then option angle degrees else option angle radians
end def

' fill ellipse rotated counterclockwise with phi degrees
' xc,yc = coordinates of the centre of the ellipse
' ra,rb = horizontal and vertical radii (ra > rb supposed)
' phi   = rotation angle in degrees
' R,G,B,A = color and alpha info
' option angle in calling code segment is untouched
'
def fill_ellipse(xc,yc,ra,rb,phi,R,G,B,A)
if sin(90)=1 then oa=1 else oa=0
option angle degrees
fill color R,G,B ! fill alpha A
dt=6 ! np=int(360/dt) ! sp=sin(phi) ! cp=cos(phi)
dim x(np+2),y(np+2)
ip=0
for t=0 to 360 step dt
  ip+=1 ! st=sin(t) ! ct=cos(t)
  x(ip)= xc+ra*cp*ct-rb*sp*st ! y(ip)= yc-ra*sp*ct-rb*cp*st
  next t
fill poly x,y
if oa=1 then option angle degrees else option angle radians
end def

' draw ellipse rotated counterclockwise with phi degrees
' xf,yf = coordinates of left focal point
' ra,rb = horizontal and vertical radii (ra > rb supposed)
' phi   = rotation angle in degrees
' option angle in calling code segment is untouched
'
def ellipse(xf,yf,ra,rb,phi)
if sin(90)=1 then oa=1 else oa=0
option angle degrees
d_alfa=10
e=sqrt(1-rb*rb/ra/ra)
rho=ra*(1-e*e)/(1-e*cos(-phi))
draw to xf+rho,yf
for alfa=d_alfa to 360 step d_alfa
  rho=ra*(1-e*e)/(1-e*cos(alfa-phi))
  x=xf+rho*cos(alfa) ! y=yf-rho*sin(alfa)
  draw line to x,y
  next alfa
if oa=1 then option angle degrees else option angle radians
end def

Post Reply