|
2 | 2 | * Geometrize
|
3 | 3 | * computational geometry and rendering library for JavaScript
|
4 | 4 | *
|
5 |
| -* @version 1.0.0 (2023-02-20 20:33:09) |
| 5 | +* @version 1.0.0 (2023-02-21 01:06:11) |
6 | 6 | * https://github.com/foo123/Geometrize
|
7 | 7 | *
|
8 | 8 | **//**
|
9 | 9 | * Geometrize
|
10 | 10 | * computational geometry and rendering library for JavaScript
|
11 | 11 | *
|
12 |
| -* @version 1.0.0 (2023-02-20 20:33:09) |
| 12 | +* @version 1.0.0 (2023-02-21 01:06:11) |
13 | 13 | * https://github.com/foo123/Geometrize
|
14 | 14 | *
|
15 | 15 | **/
|
@@ -4307,12 +4307,14 @@ var Ellipse = makeClass(EllipticArc2D, {
|
4307 | 4307 | }
|
4308 | 4308 | else if (Geometrize.Circle && (other instanceof Geometrize.Circle))
|
4309 | 4309 | {
|
4310 |
| - i = polyline_circle_intersection(self._lines, other.center, other.radius); |
| 4310 | + //i = polyline_circle_intersection(self._lines, other.center, other.radius); |
| 4311 | + i = ellipse_ellipse_intersection(self.center, self.radiusX, self.radiusY, self.cs, other.center, other.radius, other.radius, [1, 0]); |
4311 | 4312 | return i ? i.map(Point2D) : false
|
4312 | 4313 | }
|
4313 | 4314 | else if (other instanceof Ellipse)
|
4314 | 4315 | {
|
4315 |
| - i = polyline_ellipse_intersection(self._lines, other.center, other.radiusX, other.radiusY, other.cs); |
| 4316 | + //i = polyline_ellipse_intersection(self._lines, other.center, other.radiusX, other.radiusY, other.cs); |
| 4317 | + i = ellipse_ellipse_intersection(self.center, self.radiusX, self.radiusY, self. cs, other.center, other.radiusX, other.radiusY, other.cs); |
4316 | 4318 | return i ? i.map(Point2D) : false
|
4317 | 4319 | }
|
4318 | 4320 | else if (other instanceof Object2D)
|
@@ -5273,6 +5275,11 @@ function circle_circle_intersection(c1, r1, c2, r2)
|
5273 | 5275 | ;
|
5274 | 5276 | return is_strictly_equal(h, 0) ? [{x:px, y:py}] : [{x:px + h*dy, y:py - h*dx}, {x:px - h*dy, y:py + h*dx}];
|
5275 | 5277 | }
|
| 5278 | +function ellipse_ellipse_intersection(c1, rx1, ry1, cs1, c2, rx2, ry2, cs2) |
| 5279 | +{ |
| 5280 | + var q1 = ellipse2quadratic(c1, rx1, ry1, cs1), q2 = ellipse2quadratic(c2, rx2, ry2, cs2); |
| 5281 | + return solve_quadratic_quadratic_system(q1[0], q1[1], q1[2], q1[3], q1[4], q1[5], q2[0], q2[1], q2[2], q2[3], q2[4], q2[5]); |
| 5282 | +} |
5276 | 5283 | function polyline_line_intersection(polyline_points, p1, p2)
|
5277 | 5284 | {
|
5278 | 5285 | var i = [], j, p, n = polyline_points.length-1;
|
@@ -5522,6 +5529,53 @@ function solve_cubic(a, b, c, d)
|
5522 | 5529 | ];
|
5523 | 5530 | }
|
5524 | 5531 | }
|
| 5532 | +function solve_quartic(e, a, b, c, d) |
| 5533 | +{ |
| 5534 | + if (is_strictly_equal(e, 0)) return solve_cubic(a, b, c, d); |
| 5535 | + a /= e; b /= e; c /= e; d /= e; |
| 5536 | + // v^2 + (-2 b^3 + 9 a b c - 27 c^2 - 27 a^2 d + 72 b d)v + (b^2 - 3 a c + 12 d)^3 = 0 |
| 5537 | + var v, v1, v2, u, ur, D1, D2, p; |
| 5538 | + v = solve_quadratic(1, -2*pow(b, 3) + 9*a*b*c - 27*c*c - 27*a*a*d + 72*b*d, pow(b*b - 3*a*c + 12*d, 3)); |
| 5539 | + if (!v) return false; |
| 5540 | + // u = \frac{a^2}{4} +\frac{-2b+v_1^{1/3}+v_2^{1/3}}{3} |
| 5541 | + v1 = v[0]; |
| 5542 | + v2 = v[1] || v1; |
| 5543 | + u = a*a/4 + (-2*b + sign(v1)*pow(abs(v1), 1/3) + sign(v2)*pow(abs(v2), 1/3))/3; |
| 5544 | + if (0 >= u) return false; |
| 5545 | + ur = sqrt(u); |
| 5546 | + // x_{1,2} = -\tfrac{1}{4}a+\tfrac{1}{2}\sqrt{u}\pm\tfrac{1}{4}\sqrt{3a^2-8b-4u+\frac{-a^3+4ab-8c}{\sqrt{u}}} |
| 5547 | + // x_{3,4} = -\tfrac{1}{4}a-\tfrac{1}{2}\sqrt{u}\pm\tfrac{1}{4}\sqrt{3a^2-8b-4u-\frac{-a^3+4ab-8c}{\sqrt{u}}} |
| 5548 | + D1 = 3*a*a - 8*b - 4*u + (-pow(a, 3) + 4*a*b - 8*c)/ur; |
| 5549 | + D2 = 3*a*a - 8*b - 4*u - (-pow(a, 3) + 4*a*b - 8*c)/ur; |
| 5550 | + p = []; |
| 5551 | + if (0 <= D1) |
| 5552 | + { |
| 5553 | + if (is_strictly_equal(D1, 0)) |
| 5554 | + { |
| 5555 | + p.push(ur/2 - a/4); |
| 5556 | + } |
| 5557 | + else |
| 5558 | + { |
| 5559 | + D1 = sqrt(D1)/4; |
| 5560 | + p.push(ur/2 - a/4 + D1); |
| 5561 | + p.push(ur/2 - a/4 - D1); |
| 5562 | + } |
| 5563 | + } |
| 5564 | + if (0 <= D2) |
| 5565 | + { |
| 5566 | + if (is_strictly_equal(D2, 0)) |
| 5567 | + { |
| 5568 | + p.push(-ur/2 - a/4); |
| 5569 | + } |
| 5570 | + else |
| 5571 | + { |
| 5572 | + D2 = sqrt(D2)/4; |
| 5573 | + p.push(-ur/2 - a/4 + D2); |
| 5574 | + p.push(-ur/2 - a/4 - D2); |
| 5575 | + } |
| 5576 | + } |
| 5577 | + return p.length ? p : false; |
| 5578 | +} |
5525 | 5579 | function solve_linear_linear_system(a, b, c, k, l, m)
|
5526 | 5580 | {
|
5527 | 5581 | /*
|
@@ -5566,10 +5620,54 @@ function solve_linear_quadratic_system(m, n, k, a, b, c, d, e, f)
|
5566 | 5620 | return [{x:-(k + n*((-m*D - F)/R))/m, y:(-m*D - F)/R},{x:-(k + n*((m*D - F)/R))/m, y:(m*D - F)/R}];
|
5567 | 5621 | }
|
5568 | 5622 | }
|
| 5623 | +function solve_quadratic_quadratic_system(a1, b1, c1, d1, e1, f1, a2, b2, c2, d2, e2, f2) |
| 5624 | +{ |
| 5625 | + /* |
| 5626 | + a1 x^2 + b1 y^2 + c1 xy + d1 x + e1 y + f1 = 0 |
| 5627 | + a2 x^2 + b2 y^2 + c2 xy + d2 x + e2 y + f2 = 0 |
| 5628 | + */ |
| 5629 | + var q, x, y, n, p, i, j; |
| 5630 | + q = quadratics2quartic(a1, b1, c1, d1, e1, f1, a2, b2, c2, d2, e2, f2); |
| 5631 | + x = solve_quartic(q[0], q[1], q[2], q[3], q[4]); |
| 5632 | + if (!x) return false; |
| 5633 | + p = new Array(8); |
| 5634 | + j = 0; |
| 5635 | + for (i=0,n=x.length; i<n; ++i) |
| 5636 | + { |
| 5637 | + y = solve_quadratic(b1, c1*x[i] + e1, x[i]*(a1*x[i] + d1) + f1); |
| 5638 | + if (y) |
| 5639 | + { |
| 5640 | + p[j++] = {x:x[i], y:y[0]}; |
| 5641 | + if (1 < y.length) p[j++] = {x:x[i], y:y[1]}; |
| 5642 | + } |
| 5643 | + } |
| 5644 | + p.length = j; |
| 5645 | + return p.length ? p : false; |
| 5646 | +} |
5569 | 5647 | function line2linear(p1, p2)
|
5570 | 5648 | {
|
5571 | 5649 | return [p2.y - p1.y, p1.x - p2.x, p2.x*p1.y - p1.x*p2.y];
|
5572 | 5650 | }
|
| 5651 | +function quadratics2quartic(a_1, b_1, c_1, d_1, e_1, f_1, a_2, b_2, c_2, d_2, e_2, f_2) |
| 5652 | +{ |
| 5653 | +/* |
| 5654 | +Eliminate[{a_1x^2+b_1y^2+c_1xy+d_1x+e_1y+f_1 == 0, a_2x^2+b_2y^2+c_2xy+d_2x+e_2y+f_2 == 0}, y] |
| 5655 | +f_1 (2 a_1 b_2^2 x^2 - 2 a_2 b_1 b_2 x^2 - b_2 c_2 e_1 x - b_2 c_1 e_2 x + 2 b_1 c_2 e_2 x + b_1 c_2^2 x^2 - b_2 c_1 c_2 x^2 + 2 b_2^2 d_1 x - 2 b_1 b_2 d_2 x + b_1 e_2^2 - b_2 e_1 e_2 - 2 b_1 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_2^2 |
| 5656 | +*/ |
| 5657 | +/* |
| 5658 | +https://live.sympy.org/ |
| 5659 | +e = -2*a_2*b_2*c_1*e_1*x**3 + a_2*b_1*c_2*e_1*x**3 + a_1*b_2*c_2*e_1*x**3 + a_2*b_1*c_1*e_2*x**3 + a_1*b_2*c_1*e_2*x**3 - 2*a_1*b_1*c_2*e_2*x**3 - a_2*b_2*c_1**2*x**4 - a_1*b_1*c_2**2*x**4 + a_2*b_1*c_1*c_2*x**4 + a_1*b_2*c_1*c_2*x**4 - 2*a_1*b_2**2*d_1*x**3 + 2*a_2*b_1*b_2*d_1*x**3 - 2*a_2*b_1**2*d_2*x**3 + 2*a_1*b_1*b_2*d_2*x**3 - a_2*b_2*e_1**2*x**2 - a_1*b_1*e_2**2*x**2 + a_2*b_1*e_1*e_2*x**2 + a_1*b_2*e_1*e_2*x**2 - 2*a_2*b_1**2*f_2*x**2 + 2*a_1*b_1*b_2*f_2*x**2 - a_2**2*b_1**2*x**4 - a_1**2*b_2**2*x**4 + 2*a_1*a_2*b_1*b_2*x**4 + b_2*c_2*d_1*e_1*x**2 - 2*b_2*c_1*d_2*e_1*x**2 + b_1*c_2*d_2*e_1*x**2 + b_2*c_1*d_1*e_2*x**2 - 2*b_1*c_2*d_1*e_2*x**2 + b_1*c_1*d_2*e_2*x**2 - b_1*c_2**2*d_1*x**3 + b_2*c_1*c_2*d_1*x**3 - b_2*c_1**2*d_2*x**3 + b_1*c_1*c_2*d_2*x**3 - 2*b_2*c_1*e_1*f_2*x + b_1*c_2*e_1*f_2*x + b_1*c_1*e_2*f_2*x - b_2*c_1**2*f_2*x**2 + b_1*c_1*c_2*f_2*x**2 - b_2*d_2*e_1**2*x - b_1*d_1*e_2**2*x + b_2*d_1*e_1*e_2*x + b_1*d_2*e_1*e_2*x + 2*b_1*b_2*d_1*f_2*x - 2*b_1**2*d_2*f_2*x - b_2**2*d_1**2*x**2 - b_1**2*d_2**2*x**2 + 2*b_1*b_2*d_1*d_2*x**2 - b_2*e_1**2*f_2 + b_1*e_1*e_2*f_2 - b_1**2*f_2**2 - (f_1*(2*a_1*b_2**2*x**2 - 2*a_2*b_1*b_2*x**2 - b_2*c_2*e_1*x - b_2*c_1*e_2*x + 2*b_1*c_2*e_2*x + b_1*c_2**2*x**2 - b_2*c_1*c_2*x**2 + 2*b_2**2*d_1*x - 2*b_1*b_2*d_2*x + b_1*e_2**2 - b_2*e_1*e_2 - 2*b_1*b_2*f_2) + b_2**2*f_1**2) |
| 5660 | +collect(expand(e), x) |
| 5661 | +-b_1**2*f_2**2 + 2*b_1*b_2*f_1*f_2 + b_1*e_1*e_2*f_2 - b_1*e_2**2*f_1 - b_2**2*f_1**2 - b_2*e_1**2*f_2 + b_2*e_1*e_2*f_1 + x**4*(-a_1**2*b_2**2 + 2*a_1*a_2*b_1*b_2 - a_1*b_1*c_2**2 + a_1*b_2*c_1*c_2 - a_2**2*b_1**2 + a_2*b_1*c_1*c_2 - a_2*b_2*c_1**2) + x**3*(2*a_1*b_1*b_2*d_2 - 2*a_1*b_1*c_2*e_2 - 2*a_1*b_2**2*d_1 + a_1*b_2*c_1*e_2 + a_1*b_2*c_2*e_1 - 2*a_2*b_1**2*d_2 + 2*a_2*b_1*b_2*d_1 + a_2*b_1*c_1*e_2 + a_2*b_1*c_2*e_1 - 2*a_2*b_2*c_1*e_1 + b_1*c_1*c_2*d_2 - b_1*c_2**2*d_1 - b_2*c_1**2*d_2 + b_2*c_1*c_2*d_1) + x**2*(2*a_1*b_1*b_2*f_2 - a_1*b_1*e_2**2 - 2*a_1*b_2**2*f_1 + a_1*b_2*e_1*e_2 - 2*a_2*b_1**2*f_2 + 2*a_2*b_1*b_2*f_1 + a_2*b_1*e_1*e_2 - a_2*b_2*e_1**2 - b_1**2*d_2**2 + 2*b_1*b_2*d_1*d_2 + b_1*c_1*c_2*f_2 + b_1*c_1*d_2*e_2 - b_1*c_2**2*f_1 - 2*b_1*c_2*d_1*e_2 + b_1*c_2*d_2*e_1 - b_2**2*d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*f_1 - 2*b_1*c_2*d_1*e_2 + b_1*c_2*d_2*e_1 - pow(b_2,2)*pow(d_1,2) - b_2*pow(c_1,2)*f_2 + b_2*c_1*c_2*f_1 + b_2*c_1*d_1*e_2 - 2*b_2*c_1*d_2*e_1 + b_2*c_2*d_1*e_1, |
| 5667 | + -2*pow(b_1,2)*d_2*f_2 + 2*b_1*b_2*d_1*f_2 + 2*b_1*b_2*d_2*f_1 + b_1*c_1*e_2*f_2 + b_1*c_2*e_1*f_2 - 2*b_1*c_2*e_2*f_1 - b_1*d_1*pow(e_2,2) + b_1*d_2*e_1*e_2 - 2*pow(b_2,2)*d_1*f_1 - 2*b_2*c_1*e_1*f_2 + b_2*c_1*e_2*f_1 + b_2*c_2*e_1*f_1 + b_2*d_1*e_1*e_2 - b_2*d_2*pow(e_1,2), |
| 5668 | + -pow(b_1,2)*pow(f_2,2) + 2*b_1*b_2*f_1*f_2 + b_1*e_1*e_2*f_2 - b_1*pow(e_2,2)*f_1 - pow(b_2,2)*pow(f_1,2) - b_2*pow(e_1,2)*f_2 + b_2*e_1*e_2*f_1 |
| 5669 | + ]; |
| 5670 | +} |
5573 | 5671 | function circle2quadratic(center, radius)
|
5574 | 5672 | {
|
5575 | 5673 | var x0 = center.x, y0 = center.y;
|
|
0 commit comments