Reputation: 11
I've been trying to figure this out for a while and haven't found the answer. Given: Height of ellipse, Width of ellipse, Xposition of vector, Ypostion of vector, Direction of vector.
Find the distance to the edge of the circle
Here's a simple diagram: Distance to the edge of a circle
I ran across this post: Calculate Point collision between a point of a given vector and the edge of a Circle But this for a circle, not an ellipse.
This is my first time posting on here. I would be very grateful for any help or pointers about this.
Upvotes: 1
Views: 849
Reputation: 235
One fairly easy way of doing this is to represent both the ellipse and the vector in their cartesian forms
x^2/a^2 + y^2/b^2 = 1 where a & b are the lengths of the semi-major (half the width) and semi-minor (half the height) axes and the centre of the ellipse is assumed to be at (0,0)
and
y - ypos = m(x - xpos) where xpos and ypos are the position of your vector and m is the slope, the cosine of the angle (direction) it makes with the x axis.
Solve them together to get the intercept and use pythagoras to calculate the distance.
This assumes that the centre of the ellipse is at (0,0) and the axes are parallel to the x and y coordinate axes. If this is not the case then you would need a more general equation for the ellipse which is discussed in great detail here in Wikipedia.
As wierdan points out in his comment you can get 0,1 or 2 solutions.
0 if the vector starts outside the ellipse and misses it completely.
1 if the vector is a tangent to the ellipse.
2 if the vector either passes through the ellipse or it's start point is inside the ellipse.
In the case of 2 solutions 0,1 or 2 may be valid
If the vector direction points away from the ellipse then the solutions are for the reciprocal vector, the one pointing 180 degrees in the opposite direction. This may also apply to the tangent solution. So the solution(s) are not valid by your criteria.
If the start point is inside the ellipse then one solution will be for the result you want and the other for the reciprocal vector. So only one solution will be valid.
If the vector passes through the ellipse then both solutions are valid, your choice if you ignore the furthest one.
Upvotes: 2