5 let private EPS = 1.0e-5
7 let inline private ellipse2tr (x
: float) (y
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff
: float) : float =
8 aa
* x
* x
+ bb
* x
* y
+ cc
* y
* y
+ dd
* x
+ ee
* y
+ ff
10 let private nointpts
(a1
: float) (b1
: float) (a2
: float) (b2
: float) (h1
: float) (k1
: float) (h2
: float) (k2
: float) (phi_1
: float) (phi_2
: float) (h2_tr
: float) (k2_tr
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff
: float) =
13 let area_1 = Math.PI * a1b1
14 let area_2 = Math.PI * a2b2
15 let relsize = a1b1 - a2b2
19 if (h2_tr
* h2_tr
) / (a1
* a1
) + (k2_tr
* k2_tr
) / (b1
* b1
) < 1.0
30 if abs
(h1
- h2
) < EPS && abs
(k1
- k2
) < EPS && abs
(area_1 - area_2) < EPS
34 type private PointType = TANGENT_POINT | INTERSECTION_POINT
36 let private istanpt
(x
: float) (y
: float) (a1
: float) (b1
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff
: float) : PointType =
40 if x < 0.0 then -a1
else a1
45 then 2.0 * Math.PI - acos
(x / a1)
50 let x1 = a1 * cos
(theta + eps_radian)
51 let y1 = b1
* sin
(theta + eps_radian)
52 let x2 = a1 * cos
(theta - eps_radian)
53 let y2 = b1
* sin
(theta - eps_radian)
55 let test1 = ellipse2tr x1 y1 aa bb cc dd ee
ff
56 let test2 = ellipse2tr x2 y2 aa bb cc dd ee
ff
59 printf
"\t\t--- debug istanpt with (x,y)=(%f, %f), A1=%f, B1=%f\n" x y
a1 b1
60 printf
"theta=%f\n" theta
61 printf
"eps_Radian=%f\n" eps_radian
62 printf
"(x1, y1)=(%f, %f)\n" x1 y1
63 printf
"(x2, y2)=(%f, %f)\n" x2 y2
64 printf
"test1=%f\n" test1
65 printf
"test2=%f\n" test2
68 if test1 * test2 > 0.0
70 else INTERSECTION_POINT
72 let private twointpts
(x: float[]) (y
: float[]) (a1: float) (b1
: float) (phi_1
: float) (a2
: float) (b2
: float) (h2_tr
: float) (k2_tr
: float) (phi_2
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff: float) =
74 then x.[0] <- if x.[0] < 0.0 then -a1 else a1
78 then 2.0 * Math.PI - acos (x.[0] / a1)
79 else acos (x.[0] / a1)
82 then x.[1] <- if x.[1] < 0.0 then -a1 else a1
86 then 2.0 * Math.PI - acos (x.[1] / a1)
87 else acos (x.[1] / a1)
95 let xmid = a1 * cos
((theta1 + theta2) / 2.0)
96 let ymid = b1
* sin
((theta1 + theta2) / 2.0)
98 if ellipse2tr xmid ymid aa bb cc dd ee
ff > 0.0
106 theta1 <- theta1 - 2.0 * Math.PI
108 let trsign = if (theta2 - theta1) > Math.PI then 1.0 else -1.0
110 let mutable area1 = 0.5 * (a1 * b1
* (theta2 - theta1) + trsign * abs
(x.[0] * y
.[1] - x.[1] * y
.[0]))
115 printf
"TWO area1=%f\n" area1
117 area1 <- area1 + a1 * b1
119 let cosphi = cos
(phi_1
- phi_2
)
120 let sinphi = sin
(phi_1
- phi_2
)
122 let mutable x1_tr = (x.[0] - h2_tr
) * cosphi + (y
.[0] - k2_tr
) * -sinphi
123 let mutable y1_tr = (x.[0] - h2_tr
) * sinphi + (y
.[0] - k2_tr
) * cosphi
124 let mutable x2_tr = (x.[1] - h2_tr
) * cosphi + (y
.[1] - k2_tr
) * -sinphi
125 let mutable y2_tr = (x.[1] - h2_tr
) * sinphi + (y
.[1] - k2_tr
) * cosphi
129 x1_tr <- if x1_tr < 0.0 then -a2
else a2
133 theta1 <- 2.0 * Math.PI - acos (x1_tr / a2)
135 theta1 <- acos (x1_tr / a2)
139 x2_tr <- if x2_tr < 0.0 then -a2 else a2
143 theta2 <- 2.0 * Math.PI - acos (x2_tr / a2)
145 theta2 <- acos (x2_tr / a2)
153 let xmid = a2 * cos
((theta1 + theta2) / 2.0)
154 let ymid = b2
* sin
((theta1 + theta2) / 2.0)
156 let cosphi = cos
(phi_2
- phi_1
)
157 let sinphi = sin
(phi_2
- phi_1
)
158 let xmid_rt = xmid * cosphi + ymid * -sinphi + h2_tr
159 let ymid_rt = xmid * sinphi + ymid * cosphi + k2_tr
161 if (xmid_rt * xmid_rt) / (a1 * a1) + (ymid_rt * ymid_rt) / (b1
* b1
) > 1.0
169 theta1 <- theta1 - 2.0 * Math.PI
171 let trsign = if theta2 - theta1 > Math.PI then 1.0 else -1.0
173 let mutable area2 = 0.5 * (a2 * b2
* (theta2 - theta1) + trsign * abs
(x1_tr * y2_tr - x2_tr * y1_tr))
177 printf
"TWO area2=%f\n" area2
179 area2 <- area2 + a2 * b2
183 let private threeintpts
(xint
: float[]) (yint
: float[]) (a1: float) (b1
: float) (phi_1
: float) (a2: float) (b2
: float) (h2_tr
: float) (k2_tr
: float) (phi_2
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff: float) : float =
184 let mutable tanpts = 0
185 let mutable tanindex = 0
187 if istanpt
xint.[i
] yint
.[i
] a1 b2 aa bb cc dd ee
ff = TANGENT_POINT
192 printf
"tanindex=%d\n" tanindex
208 twointpts
xint yint
a1 b1 phi_1
a2 b2 h2_tr k2_tr phi_2 aa bb cc dd ee
ff
211 let private fourintpts
(xint: float[]) (yint
: float[]) (a1: float) (b1
: float) (phi_1
: float) (a2: float) (b2
: float) (h2_tr
: float) (k2_tr
: float) (phi_2
: float) (aa
: float) (bb
: float) (cc
: float) (dd
: float) (ee
: float) (ff: float) : float =
214 let area_1 = Math.PI * a1b1
215 let area_2 = Math.PI * a2b2
217 let theta = Array.zeroCreate
4
222 xint.[i
] <- if xint.[i
] < 0.0 then -a1 else a1
223 theta.[i
] <- if yint
.[i
] < 0.0 then 2.0 * Math.PI - acos (xint.[i
] / a1) else acos (xint.[i
] / a1)
227 printf
"k=%d: Theta = %f, xint=%f, yint=%f\n" k
theta.[k
] xint.[k
] yint
.[k
]
235 let mutable k = j
- 1
243 theta.[k+1] <- theta.[k]
244 xint.[k+1] <- xint.[k]
245 yint
.[k+1] <- yint
.[k]
255 printf
"AFTER sorting\n"
257 printf
"k=%d: Theta = %f, xint=%f, yint=%f\n" k theta.[k] xint.[k] yint
.[k]
260 let area1 = 0.5 * abs
((xint.[2] - xint.[0]) * (yint
.[3] - yint
.[1]) - (xint.[3] - xint.[1]) * (yint
.[2] - yint
.[0]))
262 let cosphi = cos
(phi_1
- phi_2
)
263 let sinphi = sin
(phi_1
- phi_2
)
265 let theta_tr = Array.zeroCreate
4
266 let xint_tr = Array.zeroCreate
4
267 let yint_tr = Array.zeroCreate
4
270 xint_tr.[i
] <- (xint.[i
] - h2_tr
) * cosphi + (yint
.[i
] - k2_tr
) * -sinphi
271 yint_tr.[i
] <- (xint.[i
] - h2_tr
) * sinphi + (yint
.[i
] - k2_tr
) * cosphi
273 if abs
xint_tr.[i
] > a2
275 xint_tr.[i
] <- if xint_tr.[i
] < 0.0 then -a2 else a2
277 theta_tr.[i
] <- if yint_tr.[i
] < 0.0 then 2.0 * Math.PI - acos (xint_tr.[i
] / a2) else acos (xint_tr.[i
] / a2)
279 let xmid = a1 * cos
((theta.[0] + theta.[1]) / 2.0)
280 let ymid = b1
* sin
((theta.[0] + theta.[1]) / 2.0)
282 let mutable area2, area3
, area4
, area5
= 0.0, 0.0, 0.0, 0.0
284 if ellipse2tr xmid ymid aa bb cc dd ee
ff < 0.0
286 area2 <- 0.5 * (a1b1 * (theta.[1] - theta.[0]) - abs
(xint.[0] * yint
.[1] - xint.[1] * yint
.[0]))
287 area3
<- 0.5 * (a1b1 * (theta.[3] - theta.[2]) - abs
(xint.[2] * yint
.[3] - xint.[3] * yint
.[2]))
288 area4
<- 0.5 * (a2b2 * (theta_tr.[2] - theta_tr.[1]) - abs
(xint_tr.[1] * yint_tr.[2] - xint_tr.[2] * yint_tr.[1]))
290 if theta_tr.[3] > theta_tr.[0]
292 area5
<- 0.5 * (a2b2 * (theta_tr.[0] - (theta_tr.[3] - 2.0 * Math.PI)) - abs
(xint_tr.[3] * yint_tr.[0] - xint_tr.[0] * yint_tr.[3]))
294 area5
<- 0.5 * (a2b2 * (theta_tr.[0] - theta_tr.[3]) - abs
(xint_tr.[3] * yint_tr.[0] - xint_tr.[0] * yint_tr.[3]))
296 area2 <- 0.5 * (a1b1 * (theta.[2] - theta.[1]) - abs
(xint.[1] * yint
.[2] - xint.[2] * yint
.[1]))
297 area3
<- 0.5 * (a1b1 * (theta.[0] - (theta.[3] - 2.0 * Math.PI)) - abs
(xint.[3] * yint
.[0] - xint.[0] * yint
.[3]))
298 area4
<- 0.5 * (a2b2 * (theta_tr.[1] - theta_tr.[0]) - abs
(xint_tr.[0] * yint_tr.[1] - xint_tr.[1] * yint_tr.[0]))
299 area5
<- 0.5 * (a2b2 * (theta_tr.[3] - theta_tr.[2]) - abs
(xint_tr.[2] * yint_tr.[3] - xint_tr.[3] * yint_tr.[2]))
304 printf
"\n\t\t-------------> area5 is negativ (%f). Add: pi*A2*B2=%f <------------\n" area5
area_2
306 area5
<- area5
+ area_2
311 printf
"\n\t\t-------------> area4 is negativ (%f). Add: pi*A2*B2=%f <------------\n" area4
area_2
313 area4
<- area4
+ area_2
318 printf
"\n\t\t-------------> area3 is negativ (%f). Add: pi*A2*B2=%f <------------\n" area3
area_1
320 area3
<- area3
+ area_1
325 printf
"\n\t\t-------------> area2 is negativ (%f). Add: pi*A2*B2=%f <------------\n" area2 area_1
327 area2 <- area2 + area_1
330 printf
"\narea1=%f, area2=%f area3=%f, area4=%f, area5=%f\n\n" area1 area2 area3 area4 area5
333 area1 + area2 + area3
+ area4
+ area5
335 let private quadroots
(p
: float[]) (r
: float[,]) =
336 let mutable b = -p
.[1] / (2.0 * p
.[0])
337 let c = p
.[2] / p
.[0]
338 let mutable d = b * b - c
359 let private cubicroots
(p
: float[]) (r
: float[,]) =
362 p
.[k] <- p
.[k] / p
.[0]
365 let mutable t = s * p
.[1]
366 let mutable b = 0.5 * (s * (t / 1.5 - p
.[2]) + p
.[3])
367 t <- (t - p
.[2]) / 3.0
368 let mutable c = t * t * t
369 let mutable d = b * b - c
373 d <- ((sqrt
d) + (abs
b)) ** (1.0 / 3.0)
380 d <- sqrt
(0.75) * (b - c)
385 if b > 0.0 && s <= 0.0 || b < 0.0 && s > 0.0
398 then d <- (atan
1.0) / 1.5
399 else d <- atan
((sqrt
-d) / (abs
b)) / 3.0
402 then b <- 2.0 * (sqrt
t)
403 else b <- -2.0 * (sqrt
t)
406 t <- -(sqrt
0.75
) * (sin
d) * b - 0.5 * c
429 let private biquadroots
(p
: float[]) (r
: float[,]) =
433 p
.[k] <- p
.[k] / p
.[0]
436 let mutable b = 2.0 * e
437 let mutable c = b ** 2.0
438 let mutable d = 0.75 * c
439 b <- p
.[3] + b *(c - p
.[2])
440 let mutable a = p
.[2] - d
441 c <- p
.[4] + e * (e * a - p
.[3])
444 let mutable quadExecuted = false
452 r
.[j
, k+2] <- r
.[j
, k]
457 r
.[1,k] <- r
.[1,k] - e
461 p.[2] <- (p.[1] * p.[1] - c) * 0.25
462 p.[3] <- b * b / -64.0
468 if r
.[2, k] = 0.0 && r
.[1, k] > 0.0
472 if a >= 0.0 && b >= 0.0
475 elif
a <= 0.0 && b <= 0.0
480 b <- 0.5 * (a + b / p.[1])
485 if not
quadExecuted && p.[2] < 0.0
493 elif not
quadExecuted
497 b <- (sqrt
p.[2]) * 2.0 + p.[1]
499 b <- -(sqrt
p.[2]) * 2.0 + p.[1]
512 // Return a tuple (area, x intersections, y intersections)
513 let EEOverlapArea (e1
: Types.Ellipse) (e2
: Types.Ellipse) : (float32
* float32
[] * float32
[]) option =
514 let h1, k1
, a1, b1
, phi_1
= float e1.Cx, float e1.Cy, float e1.A, float e1.B, float e1.Alpha
515 let h2, k2, a2, b2
, phi_2
= float e2.Cx, float e2.Cy, float e2.A, float e2.B, float e2.Alpha
517 if a1 <= EPS || b1 <= EPS || a2 <= EPS || b2 <= EPS
521 let phi_1 = phi_1 % Math.PI //(if phi_1 > Math.PI / 2.0 then phi_1 - Math.PI else phi_1) % Math.PI
522 let phi_2 = phi_2 % Math.PI //(if phi_2 > Math.PI / 2.0 then phi_2 - Math.PI else phi_2) % Math.PI
523 let h2_tr, k2_tr
, phi_2r
=
524 let cosphi = cos
phi_1
525 let sinphi = sin
phi_1
526 (h2 - h1) * cosphi + (k2 - k1
) * sinphi, (h1 - h2) * sinphi + (k2 - k1
) * cosphi, (phi_2 - phi_1) % (2.0 * Math.PI)
529 printf
"H2_TR=%f, K2_TR=%f, PHI_2R=%f\n" h2_tr k2_tr phi_2r
532 let cosphi = cos
phi_2r
533 let cosphi2 = cosphi ** 2.0
534 let sinphi = sin
phi_2r
535 let sinphi2 = sinphi ** 2.0
536 let cosphisinphi = 2.0 * cosphi * sinphi
539 let tmp0 = (cosphi * h2_tr + sinphi * k2_tr
) / a22
540 let tmp1 = (sinphi * h2_tr - cosphi * k2_tr
) / b22
541 let tmp2 = cosphi * h2_tr + sinphi * k2_tr
542 let tmp3 = sinphi * h2_tr - cosphi * k2_tr
544 let aa = cosphi2 / a22 + sinphi2 / b22
545 let bb = cosphisinphi / a22 - cosphisinphi / b22
546 let cc = sinphi2 / a22 + cosphi2 / b22
547 let dd = -2.0 * cosphi * tmp0 - 2.0 * sinphi * tmp1
548 let ee = -2.0 * sinphi * tmp0 + 2.0 * cosphi * tmp1
549 let ff = tmp2 * tmp2 / a22 + tmp3 * tmp3 / b22 - 1.0
552 (a1 * (a1 * aa - dd) + ff) * (a1 * (a1 * aa + dd) + ff)
553 2.0 * b1 * (a1 * a1 * (aa * ee - bb * dd) + ee * ff)
554 a1 * a1 * ((b1 * b1 * (2.0 * aa * cc - bb * bb) + dd * dd - 2.0 * aa * ff) - 2.0 * a1 * a1 * aa * aa) + b1 * b1 * (2.0 * cc * ff + ee * ee)
555 2.0 * b1 * (b1 * b1 * cc * ee + a1 * a1 * (bb * dd - aa * ee))
556 a1 * a1 * a1 * a1 * aa * aa + b1 * b1 * (a1 * a1 * (bb * bb - 2.0 * aa * cc) + b1 * b1 * cc * cc)
561 printf
"cy[%d]=%f\n" i
cy.[i
]
564 let py = Array.zeroCreate
<float> 5
565 let r = Array2D.zeroCreate
<float> 3 5
571 py.[4-i
] <- cy.[i
] / cy.[4]
575 printf
"py[%d]=%f\n" i
py.[i
]
580 elif abs
cy.[3] > EPS
583 py.[3 - i
] <- cy.[i
] / cy.[3]
588 elif abs
cy.[2] > EPS
591 py.[2-i
] <- cy.[i
] / cy.[2]
596 elif abs
cy.[1] > EPS
598 r.[1, 1] <- -cy.[0] / cy.[1]
606 printf
"nroots = %d\n" nroots
609 let ychk = Array.init
nroots (fun _ -> Double.MaxValue)
610 let mutable nychk = 0
611 for i
in 1 .. nroots do
612 if abs
r.[2, i
] < EPS
614 ychk.[nychk] <- r.[1, i
] * b1
617 printf
"ROOT is Real, i=%d --> %f (B1=%f)\n" i
r.[1, i
] b1
619 Array.sortInPlace
ychk
622 printf
"nychk=%d\n" ychk.Length
623 for j
in 0 .. ychk.Length - 1 do
624 printf
"\t j=%d, ychk=%f\n" j ychk.[j]
627 let mutable nintpts = 0
629 let xint = Array.zeroCreate
4
630 let yint = Array.zeroCreate
4
632 let mutable returnValue = 0.0
635 while returnValue = 0.0 && i < nychk do
637 printf
"------------->i=%d (nychk=%d)\n" i nychk
640 if not
(i < nychk - 1 && abs
(ychk.[i] - ychk.[i+1]) < EPS / 2.0)
643 printf
"check intersecting points. nintps is %d" nintpts
646 let x1 = if abs
ychk.[i] > b1 then 0.0 else a1 * sqrt
(1.0 - (ychk.[i] * ychk.[i]) / (b1 * b1))
650 printf
"\tx1=%f, y1=%f, A=%f. B=%f ---> ellipse2tr(x1)= %f\n" x1 ychk.[i] a1 b1 (ellipse2tr x1 ychk.[i] aa bb cc dd ee ff)
651 printf
"\tx2=%f, y1=%f, A=%f. B=%f ---> ellipse2tr(x2) %f\n" x2 ychk.[i] a1 b1 (ellipse2tr x2 ychk.[i] aa bb cc dd ee ff)
654 if abs
(ellipse2tr x1 ychk.[i] aa bb cc dd ee ff) < EPS
656 nintpts <- nintpts + 1
658 printf
"first if x1. acc nintps=%d\n" nintpts
664 xint.[nintpts-1] <- x1
665 yint.[nintpts-1] <- ychk.[i]
667 printf
"nintpts=%d, xint=%f, x2=%f, i=%d, yint=%f\n" nintpts x1 x2 i ychk.[i]
670 if returnValue <> -1.0 && abs
(ellipse2tr x2 ychk.[i] aa bb cc dd ee ff) < EPS && abs
(x2 - x1) > EPS
672 nintpts <- nintpts + 1
674 printf
"first if x2. nintps=%d, Dx=%f (eps2=%f) \n" nintpts (abs
(x2 - x1)) EPS
680 xint.[nintpts-1] <- x2
681 yint.[nintpts-1] <- ychk.[i]
684 printf
"nintpts=%d, x1=%f, xint=%f, i=%d, yint=%f\n" nintpts x1 x2 i ychk.[i]
689 printf
"i=%d, multiple roots: %f <--------> %f. continue\n" i ychk.[i] ychk.[i-1]
694 if returnValue = -1.0
700 | 0 | 1 -> nointpts
a1 b1 a2 b2 h1 k1
h2 k2 phi_1 phi_2 h2_tr k2_tr
aa bb cc dd ee ff
701 | 2 -> match istanpt
xint.[0] yint.[0] a1 b1 aa bb cc dd ee ff with
704 printf
"one point is tangent\n"
706 nointpts
a1 b1 a2 b2 h1 k1
h2 k2 phi_1 phi_2 h2_tr k2_tr
aa bb cc dd ee ff
708 | INTERSECTION_POINT ->
710 printf
"check twointpts\n"
712 twointpts
xint yint a1 b1 phi_1 a2 b2 h2_tr k2_tr
phi_2 aa bb cc dd ee ff
713 | 3 -> threeintpts
xint yint a1 b1 phi_1 a2 b2 h2_tr k2_tr
phi_2 aa bb cc dd ee ff
714 | 4 -> fourintpts
xint yint a1 b1 phi_1 a2 b2 h2_tr k2_tr
phi_2 aa bb cc dd ee ff
717 then Some (float32
area, [||], [||])
719 let xTransform : float32
[] = Array.zeroCreate
nintpts
720 let yTransform : float32
[] = Array.zeroCreate
nintpts
721 for i in 0 .. (nintpts - 1) do
722 xTransform.[i] <- float32
<| cos phi_1 * xint.[i] - sin
phi_1 * yint.[i] + h1
723 yTransform.[i] <- float32
<| sin phi_1 * xint.[i] + cos phi_1 * yint.[i] + k1
724 Some (float32
area, xTransform, yTransform)