All files / src/elements/sector SectorElement.ts

96.21% Statements 127/132
91.3% Branches 21/23
100% Functions 11/11
96.21% Lines 127/132

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 1331x 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 1x 1x 1x 1x 1x 1x 1x 1x 103x 103x 103x 58x 58x 58x 58x 58x 103x 1x 1x 1793x 1793x 1x 1x 1951x 1951x 1x 1x 1x 1x 1x 1x 1x 1x 251x 251x     251x 251x 251x 251x 251x 1x 1x 1290x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1091x 1x 1x 1290x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 451x 1290x       451x 451x 451x 1x 1x 1290x 1x 1x 1x 1x 1x 1x 1x 1x 251x 251x 1x 1x  
/*----------------------------------------------------------------------+
|    Title:	SectorElement.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 {GeomElement} from "../GeomElement";
import {PointElement} from "../point/PointElement";
import {PlaneElement} from "../plane/PlaneElement";
import {Midpoint} from "../point/Midpoint";
import {SlateCanvas} from "../../Slate";
 
export interface ISectorElementConstruction {
    O: PointElement;
    A: PointElement;
    B: PointElement;
    P: PlaneElement;
}
 
export class SectorElement extends GeomElement {
 
    _Center : PointElement;
    _A : PointElement;
    _B : PointElement;
    // A and B are two points on a circle with the given center
 
    _P : PlaneElement; // plane of the circle
 
    _angle : number = null;
 
    constructor(isec?: ISectorElementConstruction) {
        super();
        this.dimension = 2;
        if(isec != null) {
            this._Center = isec.O;
            this._A = isec.A;
            this._B = isec.B;
            this._P = isec.P;
        }
    }
 
    radius() : number {
        return this._Center.distance(this._A);
    }
 
    defined() {
        return this._A.defined() && this._B.defined() && this._Center.defined();
    }
 
 
    // Here, we differ from the original implementation.
    // Since A and B are on the circle, we find the midpoint M between them.
    // Then we calculate a radius distance along the line from Center M
    // as the "through" point.
 
    _updateThroughPoint() : void {
        let r = this.radius();
        if(r == 0) { // degenerate circle
            return;
        }
        this._angle = Math.atan2(this._B.y - this._A.y, this._B.x - this._A.x) -
            Math.atan2(this._Center.y - this._A.y, this._Center.x - this._A.x);
        let x = this._Center.x + Math.cos(this._angle/2) * r;
        let y = this._Center.y + Math.sin(this._angle/2) * r;
    }
 
    drawEdge(c: SlateCanvas): void {
        if (this.edgeColor == null || !this.defined()) return;
        let ctx = c.getContext("2d") as CanvasRenderingContext2D;
        ctx.strokeStyle = this.edgeColor;
        ctx.beginPath();
        let r = this.radius();
        let d = 2 * r;
        let startAngle = Math.atan2(
            this._A.y - this._Center.y,
            this._A.x - this._Center.x);
        let arcAngle = this._Center.angle(this._A, this._B, this._P);
        let endAngle = startAngle + arcAngle;
        ctx.arc(this._Center.x, this._Center.y, r, startAngle, endAngle, true);
        ctx.stroke();
    }
 
    drawFace(c: SlateCanvas): void {
        if (this.faceColor == null || !this.defined()) return;
        let ctx = c.getContext("2d") as CanvasRenderingContext2D;
        ctx.fillStyle = this.faceColor;
        ctx.beginPath();
        let r = this.radius();
        let d = 2 * r;
        let startAngle = Math.atan2(
            this._A.y - this._Center.y,
            this._A.x - this._Center.x);
        let arcAngle = this._Center.angle(this._A, this._B, this._P);
        let endAngle = startAngle + arcAngle;
        ctx.arc(this._Center.x, this._Center.y, r, startAngle, endAngle, true);
        ctx.moveTo(this._Center.x, this._Center.y);
        if(arcAngle <= 180.) {
            ctx.lineTo(this._A.x, this._A.y);
            ctx.lineTo(this._B.x, this._B.y);
        } else {
            ctx.lineTo(this._B.x, this._B.y);
            ctx.lineTo(this._A.x, this._A.y);
        }
        ctx.lineTo(this._Center.x, this._Center.y);
        ctx.fill();
    }
 
    drawName(c: SlateCanvas): void {
    }
 
    drawVertex(c: SlateCanvas): void {}
 
    rotate(pivot: PointElement, ac: number, as: number): void {}
 
    translate(dx: number, dy: number): void {}
 
    update(): void {
        this._updateThroughPoint();
    }
 
}