# Problem with drawing ellipse with code.

I am trying to draw an ellipse. This is the code. The logic I am using is to start with "Bigger" radius, and then uniformly decreasing it to "Smaller" radius. But i am getting this output :

that does not look like ellipse.

var a:Number =0;
var container_Mc:MovieClip ;

var w = 550/2 ;
var h = 400/2 ;

var k =0;
for ( var i =0 ; i< 360; i++,k++ )
{

if( i % 90 == 0 )
{

k=0
swap(w,h) ;
a = (w-h)/90 ;

}
var rad = getRadFromDeg(i);
var hypo = (h) + a*k;;

var xx =Math.cos( rad ) *  hypo

rad = getRadFromDeg(i);
var yy = Math.sin( rad) *  hypo

drawPoint(xx,yy)

}


The answer, uses integration, which i am not able to understand. Can anyone please throw some light, on how should i distribute the radial distances, so that it looks like ellipse ?

-
mh the question should be emigrated to porgramming stack exchange –  Dominic Michaelis Mar 10 '13 at 11:32
Why should thi slook like an ellipse at all? Why don't you simply plot (w*Math.cos(rad),h*Math.sin(rad))? –  Hagen von Eitzen Mar 10 '13 at 11:36
It's a question about mathematical concept. I added "code" just in case someone wanna look at the algorithm. I don't wanna get the code, i only wanna know, the right way to find it out. –  Vishwas Gagrani Mar 10 '13 at 11:36
@Hagen : (wMath.cos(rad),hMath.sin(rad)) is an equation of ellipse ? –  Vishwas Gagrani Mar 10 '13 at 11:37
Simplify your loop a little : for ( var i =0 ; i< 360; i++) drawPoint(Math.cos(getRadFromDeg(i)) * w, Math.sin(getRadFromDeg(i)) * h) –  Raymond Manzoni Mar 10 '13 at 11:42

## 1 Answer

If you really want points at equal angle increments, then the equations you need are:

$$s = \frac{1}{\sqrt{ {b^2}\cos^2{\theta} + {a^2}\sin^2{\theta} }}$$

$$x = sab \cos\theta$$

$$y = sab \sin\theta$$

In other words, you need to use $\text{hypo} = sab$. Here, $a$ and $b$ are the semi-axes of the ellipse, as usual.

These equations are derived from the polar equation of the standard ellipse, which is explained on this page. The polar equation (relative to the ellipse center) is:

$$r = \frac{ab}{\sqrt{ {b^2}\cos^2{\theta} + {a^2}\sin^2{\theta} }}$$

But, there is no good reason to place points at equal angular increments, so you may as well use the nice simple equations that the other answers gave.

If you want fast code, either use the forward differencing trick I mentioned in my other answer, or use the equations:

$$x = \frac{a(1-t^2)}{1+t^2} \quad ; \quad y = \frac{2bt}{1+t^2}$$

-
Why and where did S come from ? I mean, what rule has made you make the conclusion of using "s" in the algorithm. –  Vishwas Gagrani Mar 10 '13 at 13:37
The $s$ shows up if you take a line at angle $\theta$ and intersect it with the ellipse $x^2/a^2 + y^2/b^2 = 1$. In other words, it's an essential part of the polar equation of an ellipse. And, if you want equal angle increments, you ought to be thinking in terms of polar equations. –  bubba Mar 10 '13 at 13:50