All files / src/elements/point HarmonicElement.ts

100% Statements 74/74
100% Branches 6/6
100% Functions 2/2
100% Lines 74/74

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 68 69 70 71 72 73 74 751x 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 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 222x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 221x 222x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 222x 1x  
/*----------------------------------------------------------------------+
|    Title:	HarmonicElement.ts                                          |
|    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:    February, 1996.   Version 2.0.0 May, 1997.                |
|    TypeScript Port: 2026, Nelson Brown, brownnrl@gmail.com            |
|                           https://www.nelsonbrown.net/                |
+----------------------------------------------------------------------*/
 
import {PointElement} from "./PointElement";
 
export class HarmonicElement extends PointElement {
  /*--------------------------------------------------------------------+
  | The harmonic conjugate A of B with respect to the points C and D.   |
  | If B,C,D are collinear, then A also lies on that line; if not,      |
  | A lies on the circumcircle through B,C,D.                           |
  +--------------------------------------------------------------------*/
 
  private _B: PointElement;
  private _C: PointElement;
  private _D: PointElement;
  private _E: PointElement;
  private _F: PointElement;
  private _M: PointElement;
 
  constructor(B: PointElement, C: PointElement, D: PointElement) {
    super();
    this.dimension = 0;
    this._B = B;
    this._C = C;
    this._D = D;
    this._E = new PointElement();
    this._F = new PointElement();
    this._M = new PointElement();
  }
 
  public update() {
    if (this._B.z === 0.0 && this._C.z === 0.0 && this._D.z === 0.0) {
      // 2D case: treat B,C,D as complex numbers
      // A = (C(B-D) + D(B-C)) / ((B-D) + (B-C))
      let Ex = this._B.x - this._C.x, Ey = this._B.y - this._C.y;
      let Fx = this._B.x - this._D.x, Fy = this._B.y - this._D.y;
      let BFx = this._C.x * Fx - this._C.y * Fy;
      let BFy = this._C.x * Fy + this._C.y * Fx;
      let DEx = this._D.x * Ex - this._D.y * Ey;
      let DEy = this._D.x * Ey + this._D.y * Ex;
      let den = (Fx + Ex) * (Fx + Ex) + (Fy + Ey) * (Fy + Ey);
      let numx = (BFx + DEx) * (Fx + Ex) + (BFy + DEy) * (Fy + Ey);
      let numy = -(BFx + DEx) * (Fy + Ey) + (BFy + DEy) * (Fx + Ex);
      this._x = numx / den;
      this._y = numy / den;
    } else {
      // 3D case
      this._M.to(this._C).plus(this._D).times(0.5);     // M = (C+D)/2
      this._F.to(this._C).minus(this._M);                // F = C-M
      this._E.to(this._B).minus(this._M);                // E = B-M
      let rB2 = 1.0 / this._E.length2();
      let C2 = this._F.length2();
      this.to(this._F)
          .times(2.0 * PointElement.dot(this._E, this._F))
          .minus(this._E.times(C2))
          .times(rB2)
          .plus(this._M);
    }
  }
}