All files / src/elements/polygon RegularPolygonElement.ts

100% Statements 60/60
100% Branches 8/8
100% Functions 4/4
100% Lines 60/60

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 611x 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 106x 106x 106x 106x 106x 106x 106x 106x 106x 243x 243x 106x 1x 1x 493x 1090x 1090x 1090x 493x 1x 1x 3x 9x 9x 3x 1x 1x 3x 5x 5x 3x 1x  
/*----------------------------------------------------------------------+
|    Title:	RegularPolygonElement.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: 2019, Nelson Brown, brownnrl@gmail.com            |
|                           https://www.nelsonbrown.net/                |
+----------------------------------------------------------------------*/
 
import {PolygonElement} from "./PolygonElement";
import {PointElement} from "../point/PointElement";
import {PlaneElement} from "../plane/PlaneElement";
 
export class RegularPolygonElement extends PolygonElement {
 
    private _cos: number;
    private _sin: number;
    private _P: PlaneElement;
 
    constructor(A: PointElement, B: PointElement, plane: PlaneElement, n: number, d: number = 1) {
        super();
        this._P = plane;
        const theta = Math.PI * d * (n - 2) / n;
        this._cos = Math.cos(theta);
        this._sin = Math.sin(theta);
        this.V = new Array(n);
        this.V[0] = A;
        this.V[1] = B;
        for (let i = 2; i < n; i++) {
            this.V[i] = new PointElement({ AP: plane });
        }
    }
 
    update(): void {
        for (let i = 2; i < this.V.length; i++) {
            this.V[i].to(this.V[i - 2]);
            this.V[i].rotate(this.V[i - 1], this._cos, this._sin, this._P);
        }
    }
 
    translate(dx: number, dy: number): void {
        for (let i = 2; i < this.V.length; i++) {
            this.V[i].translate(dx, dy);
        }
    }
 
    rotate(pivot: PointElement, ac: number, as: number): void {
        for (let i = 2; i < this.V.length; i++) {
            this.V[i].rotate(pivot, ac, as, this._P);
        }
    }
}