I have defined a ellipse like this (startPoint, endPoint) <- major axis
end a height, which is the minor axis, now how can i easily determine if a point is inside a ellipse
Pual
I have defined a ellipse like this (startPoint, endPoint) <- major axis
end a height, which is the minor axis, now how can i easily determine if a point is inside a ellipse
Pual
You could create an awt.geom.Ellipse2D.Float and do ellipse.contains(Point2D.Float)
Or if you don’t want to use awt.geom you can ‘flatten’ your ellipse by turning it into lines & then make a ray from your point away from the ellipse & test how many times that line intersects the ellipse lines. If it’s an even number (or for an ellipse if it intersects at all) then the point is inside.
I don’t know if there’s a mathematical way of doing it, but google might tell you. Hope that helps,
Keith
Mathworld is your friend.
From a quick perusal of that page, it looks really easy to determine if a point is inside the ellipse if you know the two focus points - You simply sum the distance from the focus points to the point in question, if it’s above the length of the major axis, the point lies outside, otherwise inside.
Finding the focus points also looks pretty easy. Assuming that the ellipse is centered on the origin, with the major axis on the x-axis and with length 2 * a, and the minor axis on the y-axis with length 2 * b, the two focus points lie at ( -c, 0 ) and ( c, 0 ), where c = a * sqrt( 1 - b2 / a2 ) ( I’m getting this from equations 38 and 39 ).
So, for an ellipse as you have defined it, this code (using javax.vecmath classes) should probably work
/**
* Computes the focus points of an ellipse
*
* @param startPoint
* The start of the major axis
* @param endPoint
* The end of the major axis
* @param height
* The height of the ellipse
* @return A two element array, containing the focus points
*/
public static Point2d[] computeFocusPoints( Point2d startPoint, Point2d endPoint, double height )
{
double a = startPoint.distance( endPoint ) / 2;
double b = height / 2;
double eccentricity = Math.sqrt( 1 - ( ( b * b ) / ( a * a ) ) );
// compute the vector from the start to the first focus
Vector2d delta = new Vector2d();
delta.sub( endPoint, startPoint );
delta.scale( 0.5 );
assert delta.length() == a;
delta.scale( 1 - eccentricity );
Point2d f1 = new Point2d( startPoint );
f1.add( delta );
Point2d f2 = new Point2d( endPoint );
f2.sub( delta );
return new Point2d[] { f1, f2 };
}
I haven’t tested it, and the assert will almost certainly fail due to floating point errors, but it should get you going.