masasa
masasa

Reputation: 260

calculating an intercept point between a straight line and an ellipse - python

Iv'e been trying lately to calculate a point an ellipse

enter image description here

The desired point is the green point , knowing the red dots and the ellipse equation.

I've used numpy linspace to create an array on points and iterate them using zip(x axis , y axis) between the red points , and using the ellipse equation figure which of the points is the closest to 1. (which is the outcome of the ellipse equation ).

this concept works most of the time , but in some location of the red outer dot , this method doesn't seem to give good outcome

long story short, any idea how to calculate the green dot in python? p.s - ellipse might have angle, both of hes axis are known.

Upvotes: 1

Views: 1102

Answers (2)

masasa
masasa

Reputation: 260

I end up using the ellipse equation from this answer:

and created an in_ellipse function

then Iv'e used the Intermediate value theorem , to get a good estimation of the point

def in_ellipse(point, ellipse):
    return true if point in ellipse
return false


dot_a = ellipse_center
dot_b = dot
for i in range(20):
    center_point = ((dot_b.y - dot_a.y)/2, (dot_b.x - dot_a.x)/2)
    if in_ellipse(center_point):
        dot_a = center_point
    else:
        dot_b = center_point

return center_point

this system gives the point in 7 (2^20) digits resolution after decimal point you can increase the range for better resolution.

Upvotes: 1

MBo
MBo

Reputation: 80187

Let ellipse center is (0,0) (otherwise just subtract center coordinates), semi-axes are a, b and rotation angle is theta. We can build affine tranformation to transform ellipse into circle and apply the same transform to point P.

1) Rotate by -theta

 px1 = px * Cos(theta) + py * Sin(theta)
 py1 = -px * Sin(theta) + py * Cos(theta)

2) Extend (or shrink) along OY axis by a/b times

px2 = px1
py2 = py1 * a / b

3) Find intersection point

plen = hypot(px2, py2)  (length of p2 vector)
if (a > plen), then segment doesn't intersect ellipse - it fully lies inside

ix = a * px2 / plen
iy = a * py2 / plen

4) Make backward shrinking

ix2 = ix
iy2 = iy * b / a

5) Make backward rotation

ixfinal = ix2 * Cos(theta) - iy2 * Sin(theta)
iyfinal = ix2 * Sin(theta) + iy2 * Cos(theta)

Upvotes: 0

Related Questions