How to find a point where a line intersects an ellipse in 2D (C#)

耗尽温柔 提交于 2019-12-10 11:03:32

问题


I need to find a point where a line (its origin is ellipse' center) intersects an ellipse in 2D... I can easily find a point on a circle, because I know an angle F and the circle' radius (R):

x = x0 + R * cosF
y = y0 + R * sinF

However I just can't figure how am I supposed to deal with an ellipse... I know it's dimensions (A & B), but what is the way of finding parameter T?!

x = x0 + A * cosT
y = y0 + B * sinT

From what I understand the parameter T (T angle) is not far from the F angle (approximately +-15 degrees in some cases), but I just can't figure how to calculate it!!!

If there is a kind hearted soul, please help me with this problem...


回答1:


The standard equation of an ellipse, stationed at 0,0, is:

1 = (x)^2 / (a)    +  (y)^2 / (b)

Where a is 1/2 the diameter on the horizontal axis, and b is 1/2 the diameter on the vertical axis.

you have a line, assuming an equation:

y = (m)(x - x0) + y0

So, let us plug-and-play!

1 = (x)^2 / (a)   +   (m(x - x0) + y0)^2 / (b)

1 = x^2 / a  +  (mx + (y0 - mx0))^2 / b

1 = x^2 / a  +  (m^2 * x^2 + 2mx*(y0 - mx0) + (y0 - mx0)^2) / b

1 = x^2 / a  + (m^2 x^2) / b + (2mx*(y0 - mx0) + (y0^2 - 2y0mx0 + m^2*x0^2)) / b

1 = ((x^2 * b) / (a * b)) + ((m^2 * x^2 * a) / (a * b)) + (2mxy0 - 2m^2xx0)/b + (y0^2 - 2y0mx0 + m^2*x0^2)/b

1 = ((bx^2 + am^2x^2)/(ab)) + (x*(2my0 - 2m^2x0))/b + (y0^2 - 2y0mx0 + m^2*x0^2)/b

0 = x^2*((b + a*m^2)/(ab)) + x*((2my0 - 2m^2x0)/b) + (((y0^2 - 2y0mx0 + m^2*x0^2)/b) - 1)

That last equation follows the form of a standard quadratic equation.

So just use the quadratic formula, with:

((b + a*m^2)/(ab))
((2my0 - 2m^2x0)/b)
and 
(((y0^2 - 2y0mx0 + m^2*x0^2)/b) - 1)

to get the X values at the intersections; Then, plug in those values into your original line equation to get the Y values.

Good luck!




回答2:


Don't do it this way. Instead check the equation that forms an ellipse and that forming a line and solve the set:

The ellipse: (x/a)^2 + (y/b)^2 = 1 Your line: y = cx

You know a, b and c, so finding a solution is going to be easy. You'll find two solutions, because the line crosses the ellipse twice.

EDIT: Note I moved your ellipse's center to (0,0). It makes everything easier. Just add (x0,y0) to the solution.




回答3:


I wrote a C# code for your problem and I hope you can find it helpful. the distance function inside this code calculates euclidean distance between two points in space.

wX denotes horizontal radios of ellipse and wY denotes vertical radios.

private PointF LineIntersectEllipse(PointF A, PointF B, float wX, float wY)
{
      double dx = B.X - A.X;
      double dy = B.Y - A.Y;
      double theta = Math.Atan2(dy, dx);
      double r = distance(A, B) - ((wX * wY) / Math.Sqrt(Math.Pow(wY * Math.Cos(theta), 2) + Math.Pow(wX * Math.Sin(theta), 2)));
      return PointF((float)(A.X + r * Math.Cos(theta)), (float)(A.Y + r * Math.Sin(theta)));
}


来源:https://stackoverflow.com/questions/10692541/how-to-find-a-point-where-a-line-intersects-an-ellipse-in-2d-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!