All files / src/elements/polygon PolygonElement.ts

99.27% Statements 136/137
91.42% Branches 32/35
100% Functions 10/10
99.27% Lines 136/137

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 133 134 135 136 137 1381x 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 1184x 1184x 1184x 127x 127x 127x 1057x 1057x 1x 1x 80x 80x 80x 80x 80x 140x 140x 80x 80x 1x 1x 11060x 11060x   11060x 11060x 11060x 11060x 11060x 11060x 9541x 9541x 9541x 9541x 9541x 9541x 9541x 9541x 9541x 9541x 11060x 27031x 27031x 27031x 9541x 9541x 9541x 9541x 9541x 11060x 1x 1x 24197x 24197x 86056x 86056x 23427x 23427x 1x 1x 14160x 11556x 11556x 11556x 11556x 11556x 11556x 11556x 11556x 11556x 11556x 11556x 14160x 1x 1x 11060x 364x 364x 364x 1328x 1328x 1328x 364x 364x 364x 364x 364x 364x 364x 364x 364x 11060x 1x 1x 11060x 541x 1827x 1827x 541x 11060x 1x 1x 3x 1x 1x 7x 1x 1x 3821x 1x  
/*----------------------------------------------------------------------+
|    Title:	LineElement.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 {PointElement} from "../point/PointElement";
import {GeomElement} from "../GeomElement";
import {SlateCanvas} from "../../Slate";
 
export class PolygonElement extends GeomElement {
 
    public V : PointElement[];
 
    constructor(ps?: PointElement[]) {
        super();
        this.dimension = 2;
        if (ps == null) {
            this.V = [];
            return;
        }
        this.V = ps;
    }
 
    public area() : number {
        // Ported from PolygonElement.java area() — 2026-04-12
        // Compute the area of this polygon (assuming it's planar & convex).
        // Uses fan triangulation from V[0].
        let sum = 0.0;
        for (let i = 0; i < this.V.length - 2; ++i) {
            sum += PointElement.area(this.V[0], this.V[i + 1], this.V[i + 2]);
        }
        return sum;
    }
 
    public drawEdge(c: HTMLCanvasElement, color?: string): void {
        if (color == null) {
            if (this.shouldHighlight) {
                color = this.edgeHighlightColor;
            } else {
                color = this.edgeColor;
            }
        }
 
        if (color == null) return;
        if (this.V.length <= 1) return;
 
        let ctx = c.getContext("2d");
        ctx.beginPath();
        ctx.strokeStyle = color;
        let firstPoint = this.V[0];
        let rest = this.V.slice(1);
 
        ctx.moveTo(firstPoint.x, firstPoint.y);
        
        for(let vertex of rest) {
            ctx.lineTo(vertex.x, vertex.y);
            ctx.stroke();
        }
 
        if (this.V.length > 2) {
            ctx.lineTo(firstPoint.x, firstPoint.y);
            ctx.stroke();
        }
    }
 
    public defined() : boolean {
        if (this.V.length == 0) return false;
        for(let v of this.V) {
            if (!v.defined()) return false;
        }
        return true;
    }
 
    public drawFace(c: SlateCanvas): void {
        if(this.faceColor != null && this.defined() && this.V.length > 2) {
            let ctx : CanvasRenderingContext2D = c.getContext("2d") as CanvasRenderingContext2D;
            ctx.beginPath();
            ctx.fillStyle = this.faceColor;
            let firstPoint = this.V[0];
            let rest = this.V.slice(1);
            ctx.moveTo(firstPoint.x, firstPoint.y);
            for(let vertex of rest)
                ctx.lineTo(vertex.x, vertex.y);
            ctx.closePath();
            ctx.fill();
        }
    }
 
    public drawName(c: SlateCanvas): void {
        if (this.nameColor != null && this.name != null && this.defined()) {
            let x : number = 0;
            let y : number = 0;
            for(let v of this.V) {
                x += v.x;
                y += v.y;
            }
            let n : number = this.V.length;
            x = x / n;
            y = y / n;
            let ctx : CanvasRenderingContext2D = c.getContext("2d") as CanvasRenderingContext2D;
            let [w, h] = this._getTextMetrics(ctx, this._name);
            ctx.beginPath();
            ctx.fillStyle = this.nameColor;
            ctx.fillText(this._name, x - w/2., y + h/4.);
        }
    }
 
    public drawVertex(c: SlateCanvas): void {
        if (this.vertexColor != null && this.defined() ) {
            for(let v of this.V) {
                v.drawVertex(c, this.vertexColor);
            }
        }
    }
 
    public rotate(pivot: PointElement, ac: number, as: number): void {
    }
 
    public translate(dx: number, dy: number): void {
    }
 
    public update(): void {
    }
}