Ellipses in BBC Basic


We start with the circle program previously discussed:

      MODE8:OFF
      ORIGIN 640,512
      R=300
      PLOT69,0,0 : REM mark the centre
      MOVE R,0
      FOR t=0 TO 360
        x=R*COSRADt
        y=R*SINRADt
        DRAW x,y
        delay=INKEY(1)
      NEXT

We can modify this quite easily to draw an ellipse:

      MODE8:OFF
      ORIGIN 640,512
      a=500 : b= 300
      PLOT69,0,0 : REM mark the centre
      MOVE a,0
      FOR t=0 TO 360
        x=a*COSRADt
        y=b*SINRADt
        DRAW x,y
        delay=INKEY(1)
      NEXT


Here a represents the semi-major axis (half the ellipse's longer axis) lying here along the x-axis
and   b represents the semi-minor axis (half the ellipse's shorter axis) lying here along the y-axis

In maths books, the representation of an ellipse is often given in terms of the ellipticity of the ellipse, e.where

            b =  a * SQR(1 - e^2),  where * is a multiplication sign, SQR specifies a square root and e^2 specificies e to the power two or "e squared".

(e is zero for a circle, unity for a parabola and between these two value for an ellipse).

The two  foci of the ellipse lie at a distance of  a*e either side of the the centre point on the major axis. In order to represent a planetary orbit we need to move the ellipse along the x-axis by a*e in order to bring a focus to the origin which now signifies the position of the sun:

      MODE8:OFF
      ORIGIN 140,512
      a=500 : b= 300 : e = SQR(1-(b/a)^2)
      PLOT69,0,0
      REM mark the origin, now also one focus of ellipse
      MOVE a+(a*e),0
      FOR t=0 TO 360
        x=(a*COSRADt) + (a*e)
        y=b*SINRADt
        DRAW x,y
        delay=INKEY(1)
      NEXT


 If we now wish to show the ellipse at any angle,T (measured clockwise) from the horizontal (x-axis) we need to rotate each point of the ellipse about the origin:

      MODE8:OFF
      ORIGIN 140,700
      a=500 : b= 300 : e = SQR(1-(b/a)^2) : T=20
      PLOT69,0,0
      REM mark the origin, now also one focus of ellipse
      FOR t=0 TO 360
        x=a*COSRADt
        y=b*SINRADt
        REM shift focus to origin
        xx=a*COSRADt + (a*e)
        yy=y
        REM rotate all points by T degrees (clockwise)
        xxx=(xx*COSRADT) + (yy*SINRADT)
        yyy=-(xx*SINRADT) + (yy*COSRADT)
        IF t=0 MOVE xxx,yyy
        DRAW xxx,yyy
        delay=INKEY(1)
      NEXT



Having got to this point we are now well equipped to represent planet and satellite orbits about their central bodies (sun, earth etc).

For more ellipse demos click here

Richard Weston's Homepage