All files / src/elements/circle SphereIntersectionElement.ts

100% Statements 67/67
100% Branches 4/4
100% Functions 4/4
100% Lines 67/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 681x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 11x 11x 11x 11x 11x 1x 1x 40x 40x 40x 40x 40x 40x 40x 40x 40x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x  
/*----------------------------------------------------------------------+
|    Title:	SphereIntersectionElement.ts                                |
|    Originally: IntersectionSS.java (renamed for clarity)             |
|    A port of the software Geometry Applet by                          |
|    Author:    David E. Joyce                                          |
|        Department of Mathematics and Computer Science                 |
|        Clark University                                               |
|        Worcester, MA 01610-1477                                       |
|        U.S.A.                                                         |
|                                                                       |
|        http://aleph0.clarku.edu/~djoyce/home.html                     |
|        djoyce@clarku.edu                                              |
|                                                                       |
|    Date:    May, 1997.                                                |
|    TypeScript Port: 2026, Nelson Brown, brownnrl@gmail.com            |
|                           https://www.nelsonbrown.net/                |
+----------------------------------------------------------------------*/
 
import {CircleElement} from "./CircleElement";
import {SphereElement} from "../sphere/SphereElement";
import {PointElement} from "../point/PointElement";
import {PerpendicularPlane} from "../plane/PerpendicularPlane";
 
export class SphereIntersectionElement extends CircleElement {
  /*--------------------------------------------------------------------+
  | This circle is the intersection of two spheres S and T. The center  |
  | of the circle is Center, which is A, lies in the plane AP, and      |
  | has radius AB.                                                      |
  +--------------------------------------------------------------------*/
 
  private _S: SphereElement;
  private _T: SphereElement;
 
  constructor(S: SphereElement, T: SphereElement) {
    super();
    this.dimension = 2;
    this._S = S;
    this._T = T;
    this.Center = this.A = new PointElement();
    this.B = new PointElement();
    this.AP = new PerpendicularPlane({A: this.Center, E: T.Center});
    this.Center._AP = this.AP;
  }
 
  public update() {
    let d2 = this._T.Center.distance2(this._S.Center);
    let t2 = this._T.radius2();
    let factor = 0.5 + (t2 - this._S.radius2()) / (2.0 * d2);
    this.Center.to(this._S.Center).minus(this._T.Center).times(factor).plus(this._T.Center);
    let radius = Math.sqrt(t2 - this.Center.distance2(this._T.Center));
    this.B.to(this.Center);
    this.B.z += radius;
    this.AP.update();
  }
 
  public translate(dx: number, dy: number) {
    this.Center.translate(dx, dy);
    this.B.translate(dx, dy);
    this.AP.translate(dx, dy);
  }
 
  public rotate(pivot: PointElement, ac: number, as: number) {
    this.Center.rotate(pivot, ac, as);
    this.B.rotate(pivot, ac, as);
    this.AP.rotate(pivot, ac, as);
  }
}