All files / src/elements/polyhedron PolyhedronElement.ts

100% Statements 99/99
96% Branches 24/25
77.77% Functions 7/9
100% Lines 99/99

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 1001x 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 56x 56x 56x 56x 1x 1x 1634x 8027x 8027x 1634x 1634x 1x 1x 717x 64x 64x 384x 1536x 1536x 1536x 1536x 384x 64x 64x 64x 64x 717x 1x 1x 718x 1x 6x 24x 24x 6x 1x 718x 1x 1x 717x 708x 708x 708x 3462x 11908x 11908x 11908x 11908x 11908x 11908x 11908x 3462x 708x 717x 1x 1x 717x 642x 3100x 3100x 3100x 3100x 3100x 3100x 3100x 642x 717x 1x 1x 1x 1x 1x  
/*----------------------------------------------------------------------+
|    Title:	PolyhedronElement.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:    May, 1997.                                                |
|    TypeScript Port: 2026, Nelson Brown, brownnrl@gmail.com            |
|                           https://www.nelsonbrown.net/                |
+----------------------------------------------------------------------*/
 
import {GeomElement} from "../GeomElement";
import {PolygonElement} from "../polygon/PolygonElement";
import {PointElement} from "../point/PointElement";
import {SlateCanvas} from "../../Slate";
 
export class PolyhedronElement extends GeomElement {
 
    public P: PolygonElement[];   // array of face polygons
 
    constructor() {
        super();
        this.dimension = 2;
        this.P = [];
    }
 
    public defined(): boolean {
        for (let i = 0; i < this.P.length; ++i) {
            if (!this.P[i].defined()) return false;
        }
        return true;
    }
 
    public drawName(c: SlateCanvas): void {
        if (this.nameColor != null && this.name != null && this.defined()) {
            let x = 0, y = 0, ct = 0;
            for (let j = 0; j < this.P.length; ++j) {
                for (let i = 0; i < this.P[j].V.length; ++i) {
                    x += this.P[j].V[i].x;
                    y += this.P[j].V[i].y;
                    ++ct;
                }
            }
            x /= ct;
            y /= ct;
            this.drawString(Math.round(x), Math.round(y), c);
        }
    }
 
    public drawVertex(c: SlateCanvas): void {
        if (this.vertexColor != null && this.defined()) {
            for (let j = 0; j < this.P.length; ++j) {
                for (let i = 0; i < this.P[j].V.length; ++i) {
                    this.P[j].V[i].drawVertex(c, this.vertexColor);
                }
            }
        }
    }
 
    public drawEdge(c: SlateCanvas): void {
        if (this.edgeColor != null && this.defined()) {
            let ctx = c.getContext("2d") as CanvasRenderingContext2D;
            ctx.strokeStyle = this.edgeColor;
            for (let j = 0; j < this.P.length; ++j) {
                for (let i = 0; i < this.P[j].V.length; ++i) {
                    let a = this.P[j].V[i];
                    let b = this.P[j].V[(i + 1) % this.P[j].V.length];
                    ctx.beginPath();
                    ctx.moveTo(a.x, a.y);
                    ctx.lineTo(b.x, b.y);
                    ctx.stroke();
                }
            }
        }
    }
 
    public drawFace(c: SlateCanvas): void {
        if (this.faceColor != null && this.defined()) {
            for (let j = 0; j < this.P.length; ++j) {
                // Pass the polyhedron's faceColor to each face polygon,
                // matching Java's P[j].drawFace(g, faceColor)
                let savedColor = this.P[j].faceColor;
                this.P[j].faceColor = this.faceColor;
                this.P[j].drawFace(c);
                this.P[j].faceColor = savedColor;
            }
        }
    }
 
    public update(): void {}
    public translate(dx: number, dy: number): void {}
    public rotate(pivot: PointElement, ac: number, as: number): void {}
}