- let accuracy_extremum_search_1 = 10 // 3
- let accuracy_extremum_search_2 = 10 // 4
-
- // p3 as the referencial.
- let p1x = p1x - p3x
- let p1y = p1y - p3y
-
- let p2x = p2x - p3x
- let p2y = p2y - p3y
-
- // Convert to polar coordinates.
- let alpha1 = atan m1
- let alpha2 = atan m2
-
- let r1 = sqrt (p1x ** 2. + p1y ** 2.)
- let theta1 = atan2 p1y p1x
-
- let r2 = sqrt (p2x ** 2. + p2y ** 2.)
- let theta2 = atan2 p2y p2x
-
- let valid =
- 4. * sin (alpha1 - theta1) * (-r1 * sin (alpha1 - theta1) + r2 * sin (alpha1 - theta2)) *
- sin (alpha2 - theta2) * (-r1 * sin (alpha2 - theta1) + r2 * sin (alpha2 - theta2)) +
- r1 * r2 * sin (alpha1 - alpha2) ** 2. * sin (theta1 - theta2) ** 2. < 0.
-
- if valid
- then
- let r theta =
- (r1 * r2 * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) * sin (theta1 - theta2)) /
- (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.)
-
- let rabs = r >> abs
-
- // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
- let thetaTan, _ = goldenSectionSearch rabs accuracy_extremum_search_1 0. Math.PI Maximum
- let rTan = r thetaTan
-
- let PTanx = rTan * cos thetaTan
- let PTany = rTan * sin thetaTan
-
- let d1a = tan alpha1
- let d1b = -d1a * p1x + p1y
-
- let d2a = tan alpha2
- let d2b = -d2a * p2x + p2y
-
- let d3a = -1. / tan thetaTan
- let d3b = -d3a * PTanx + PTany
-
- let Ux = -(d1b - d2b) / (d1a - d2a)
- let Uy = -(d2a * d1b - d1a * d2b) / (d1a - d2a)
-
- let Vx = -(d1b - d3b) / (d1a - d3a)
- let Vy = -(d3a * d1b - d1a * d3b) / (d1a - d3a)
-
- let Wx = p1x + (p2x - p1x) / 2.
- let Wy = p1y + (p2y - p1y) / 2.
-
- let Zx = p1x + (PTanx - p1x) / 2.
- let Zy = p1y + (PTany - p1y) / 2.
-
- let va = -(-Vy + Zy) / (Vx - Zx)
- let vb = -(Zx * Vy - Vx * Zy) / (Vx - Zx)
-
- let ua = -(-Uy + Wy) / (Ux - Wx)
- let ub = -(Wx * Uy - Ux * Wy) / (Ux - Wx)
-
- let cx = -(vb - ub) / (va - ua)
- let cy = -(ua * vb - va * ub) / (va - ua)
-
- let rc = sqrt (cx ** 2. + cy ** 2.)
- let psi = atan2 cy cx
-
- let rellipse theta =
- sqrt (
- rc ** 2. + (r1 ** 2. * r2 ** 2. * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) ** 2. * sin (theta1 - theta2) ** 2.) /
- (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.) ** 2. -
- (2. * r1 * r2 * rc * cos (theta - psi) * (r1 * (cos (alpha2 + theta - theta1 - theta2) - cos (alpha2 - theta) * cos (theta1 - theta2)) * sin (alpha1 - theta1) + r2 * (-cos (alpha1 + theta - theta1 - theta2) + cos (alpha1 - theta) * cos (theta1 - theta2)) * sin (alpha2 - theta2)) * sin (theta1 - theta2)) /
- (sin (alpha1 - theta1) * sin (alpha2 - theta2) * (r1 * sin (theta - theta1) - r2 * sin (theta - theta2)) ** 2. - r1 * r2 * sin (alpha1 - theta) * sin (alpha2 - theta) * sin (theta1 - theta2) ** 2.))
-
- // We search for an interval [theta_a, theta_b] and assume the function is unimodal in this interval.
- let r1eTheta, r1e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Maximum // Pi/2 and not pi because the period is Pi.
- let r2eTheta, r2e = goldenSectionSearch rellipse accuracy_extremum_search_2 0. (Math.PI / 2.) Minimum
-
- let rr1e = r r1eTheta
- let r1ex = rr1e * cos r1eTheta
- let r1ey = rr1e * sin r1eTheta
- let mutable alpha = atan ((r1ey - cy) / (r1ex - cx))
- if alpha < 0.
- then
- alpha <- alpha + Math.PI
-
- // Ride off the p3 referential.
- let cx = cx + p3x
- let cy = cy + p3y
-
- Some (Types.Ellipse(float32 cx, float32 cy, float32 r1e, float32 r2e, float32 alpha))
- else
- None
-
-let ellipse2 (p1x: float) (p1y: float) (m1: float) (p2x: float) (p2y: float) (m2: float) (p3x: float) (p3y: float) : Types.Ellipse option =