mapache wrote on Feb 8th, 2010, 10:13pm:-how to find a point on ellipse given an angle
or
-how to determine line (ray) & ellipse intersection point
In the following, assume an ellipse centred at the origin.
The "angle" you are feeding into the parametric equations,
Code:float ellTmpX = ((mjDia / 2) * cos(mouseAngle));
float ellTmpY = ((mnDia / 2) * sin(mouseAngle));
will produce the a point on the ellipse, but a line from the origin to this point does not subtend an angle of "mouseAngle" with the x-axis. In other words, the mouse cursor will not be on the line joining the origin and this point - except in the cases where the ellipse is a circle, and for the four points of the ellipse on the x-axis and y-axis.
Try going back to the drawing board (or pen/pencil and paper!) and doing the basic maths.
Do you have an angle to start with, or do you only think you need an angle to get your answer? It seems to me the input values are are things like mouse coordinates, ellipse centre, major and minor ellipse diameters... no "angle".
You ask about determining the intersection or a "ray" and the ellipse. How are you defining your ray? You don't seem to have that yet.
I suggest looking at some variant of:
y = mx + c
That is a simple line equation (for a line that isn't vertical).
For any given x, you can calculate y to find a point on the line.
What about the ellipse? One way to draw an ellipse is to go in the direction of the x-axis and work out what the y values are.
If (x/a)^2 + (y/b)^2 = 1
then (y/b)^2 = 1 - (x/a)^2
and y/b = +- sqrt( 1 - (x/a)^2 )
so y = +- b * sqrt( 1 - (x/a)^2 )
The +- (plus or minus) comes about since there is a "top half" of the ellipse and a "bottom half"
So.. when are these equations both satisfied?
mx + c = +- b * sqrt( 1 - (x^a)^2 )
Let's say c is 0 (the line passed through the origin)
x = +- b/m * sqrt( 1 - (x/a)^2 )
x^2 = (b/m)^2 * ( 1 - (x/a)^2 )
x^2 + (b/m)^2 * ((x/a)^2) = (b/m)^2
(x^2) * (1 + (b/am)^2) = (b/m)^2
x^2 = (b/m)^2 / (1 + (b/am)^2)
x = +- (b/m) * sqrt( 1 / (1 + (b/am)^2) )
Unit circle, line y = x :
x = +- (1/1) * sqrt( 1 / (1 + (1/1)^2) )
x = +- sqrt( 1 / 2 )
I tested this quickly in code... and it worked!
Code:/**
* spxlEllipseBoundary
* 2010-02-10 by subpixel
* http://subpixels.com
*/
// Ellipse centre
float Xc;
float Yc;
// Ellipse radii (a along x-axis, b along y-axis)
float a = 150;
float b = 80;
void setup()
{
size(600, 400, P2D);
Xc = width / 2;
Yc = width / 2;
ellipseMode(RADIUS);
}
void draw()
{
// Turn mouse coordinates into ellipse-centre coordinates
float x = mouseX - Xc;
float y = mouseY - Yc;
translate(Xc, Yc); // centre of ellipse
background(0);
stroke(255);
noFill();
ellipse(0, 0, a, b);
if (x != 0)
{
float m = y / x; // line gradient
// x = +- (b/m) * sqrt( 1 / (1 + (b/am)^2) )
// y = mx (+c, but c == 0)
float n1 = b / (a * m);
float n2 = n1 * n1;
float Px1 = + (b/m) * sqrt( 1 / (1 + n2) );
float Px2 = - Px1;
float Py1 = m * Px1;
float Py2 = m * Px2;
line (Px1, Py1, Px2, Py2);
}
}