All files / src/elements/point SphereSliderElement.ts

100% Statements 75/75
88.88% Branches 8/9
100% Functions 4/4
100% Lines 75/75

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 75 761x 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 37x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 204x 204x 1x 1x 10x 10x 10x 10x 6x 6x 6x 6x 6x 6x 10x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 2x 2x 8x 8x 1x  
/*----------------------------------------------------------------------+
|    Title:	SphereSliderElement.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";
import {SphereElement} from "../sphere/SphereElement";
 
export class SphereSliderElement extends PointElement {
 
  private _S: SphereElement;
  private _initx: number;
  private _inity: number;
  private _initz: number;
 
  constructor(S: SphereElement, x: number, y: number, z: number) {
    super();
    this.dimension = 0;
    this.draggable = true;
    this._S = S;
    this._x = this._initx = x;
    this._y = this._inity = y;
    this._z = this._initz = z;
  }
 
  public reset() {
    this._x = this._initx;
    this._y = this._inity;
    this._z = this._initz;
    this.toSphere(this._S.Center, this._S.radius());
  }
 
  public update() {
    this.toSphere(this._S.Center, this._S.radius());
  }
 
  public drag(tox: number, toy: number): boolean {
    let dist2 = (this._S.Center.x - tox) * (this._S.Center.x - tox)
              + (this._S.Center.y - toy) * (this._S.Center.y - toy);
    let r2 = this._S.radius2();
    if (dist2 <= r2) {
      this._x = tox;
      this._y = toy;
      if (this._z > this._S.Center.z)
        this._z = this._S.Center.z + Math.sqrt(r2 - dist2);
      else
        this._z = this._S.Center.z - Math.sqrt(r2 - dist2);
    } else {
      // beyond shadow of sphere
      tox -= this._S.Center.x;
      toy -= this._S.Center.y;
      let factor = Math.sqrt((tox * tox + toy * toy) / r2);
      tox = tox / factor + this._S.Center.x;
      toy = toy / factor + this._S.Center.y;
      if ((tox - this._x) * (tox - this._x) + (toy - this._y) * (toy - this._y) < 0.5)
        return false;
      this._x = tox;
      this._y = toy;
      this._z = this._S.Center.z;
    }
    return true;
  }
}