All files / src/elements GeomElement.ts

87.45% Statements 223/255
83.78% Branches 31/37
72.72% Functions 24/33
87.45% Lines 223/255

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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 2561x 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 55269x 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 679x 679x 1x 1x 1x 1x 1x 1x 1x 1x 1x 82144x 82144x 82144x 82144x 82144x 82144x 82144x 82144x 1x 1x 81682x 81682x 81682x 81682x 81682x 81682x 81682x 81682x         81682x         81682x         81682x         81682x 81682x 81682x 81682x 81682x 81682x 81682x 81682x 41335x 41335x 21909x 21909x 21909x 21909x 19426x 19426x 19426x 19426x 19426x 19426x 41335x 40347x 40347x 21435x 21435x 21435x 40347x 18912x 18912x 18912x 18912x 40347x 81682x 1x 1x 589505x 589505x 1x 1x 18411x 18411x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 18571x 18571x 1x 1x 14737x 14737x 1x 1x 24004x 24004x 1x 1x     1x 1x 3068x 3068x 1x 1x     1x 1x 3068x 3068x 1x 1x 5977x 5977x 1x 1x 65883x 65883x 1x 1x 14736x 14736x 1x 1x     1x 1x 413441x 413441x 1x 1x 493569x 493569x 1x 1x 17932x 17932x 1x 1x 143695x 143695x 1x 1x 70085x 70085x 1x 1x     1x 1x     1x 1x     1x 1x     1x 1x 32306x 32306x 1x 1x 14736x 14736x 1x 1x     1x 1x 466415x 466415x 1x  
/*----------------------------------------------------------------------+
|    Title:	GeomElement.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 {SlateCanvas} from "../Slate";
 
export enum Align {
    CENTRAL,
    LEFT,
    RIGHT,
    ABOVE,
    BELOW
}
 
export abstract class GeomElement {
 
    constructor() {
    }
 
    protected _name: string;
    // True for elements that are references to existing elements (e.g.,
    // point;first returns P[0], point;vertex returns polygon.V[n-1]).
    // Preexisting elements are skipped during rotate/translate to avoid
    // double-movement. Matches Java's preexists[] array in Slate.java.
    public preexists: boolean = false;
    // All colors default to null (transparent) — matching Java's Element.java.
    // Named elements get their colors assigned by init() in index.ts, which
    // computes type-appropriate defaults. Internal/intermediate elements
    // created inside constructions stay null (invisible) unless explicitly set.
    private _nameColor: string = null;
    protected _vertexColor : string = null;
    private _edgeColor : string = null;
    private _faceColor : string = null;
 
    private _nameHighlightColor   : string = '#FF0000';
    private _vertexHighlightColor : string = '#FFFFFF';
    private _edgeHighlightColor   : string = '#FFFFFF';
    private _faceHighlightColor   : string = '#00FFFF';
 
    // Default matches Java: Font("TimesRoman", Font.ITALIC, 18)
    protected static _font : string = "italic 18px Times New Roman";
 
    static setFont(fontName: string, fontSize: number): void {
        GeomElement._font = `italic ${fontSize}px ${fontName}`;
    }
 
    private _draggable : boolean;
    private _dimension : number;
    private _align : Align;
 
    private _shouldHighlight : boolean = false;
    protected _pixelTolerance : number = 50;
 
    protected _getTextMetrics(ctx: CanvasRenderingContext2D, txt: string) : [number, number] {
        let textMetrics = ctx.measureText(txt);
        let w = textMetrics.width;
        // Use actual font metrics for height; fall back to ascent-based estimate
        let h = (textMetrics.actualBoundingBoxAscent != null)
            ? textMetrics.actualBoundingBoxAscent + textMetrics.actualBoundingBoxDescent
            : 16;
        return [w, h];
    }
 
    drawString(ix : number, iy : number,  c: SlateCanvas, overrideAlign? : Align) {
        let ctx = c.getContext("2d") as CanvasRenderingContext2D;
        if(this._nameColor == null) return;
        ctx.font = GeomElement._font;
        ctx.fillStyle = this._nameColor;
        let [w, h] = this._getTextMetrics(ctx, this._name);
 
        switch (overrideAlign || this._align) {
            case Align.LEFT:
                ix = ix - w - 6;
                iy = iy + h/2 - 4;
                ctx.fillText(this._name, ix, iy);
                return;
            case Align.RIGHT:
                ix += 2;
                iy += h/2 - 4;
                ctx.fillText(this._name, ix, iy);
                return;
            case Align.ABOVE:
                ix -= w/2;
                iy += -h/2 + 4;
                ctx.fillText(this._name, ix, iy);
                return;
            case Align.BELOW:
                ix -= w/2;
                iy += h/2 + 6;
                ctx.fillText(this._name, ix, iy);
                return;
        }
        // compute (dx,dy) coordinates relative to center of canvas
        // and normalized
        let cw = c.width;
        let ch = c.height;
        let dx = (ix - cw/2) * ch;
        let dy = (iy - ch/2) * cw;
        if (dy > dx) {
            if (dy >= -dx)	// put name below
            {
                ix -= w/2;
                iy += h/2 + 6;
                ctx.fillText(this._name, ix, iy);
            }
            else 		// put name left
            {
                ix = ix - w - 6;
                iy += h/2 - 4;
                ctx.fillText(this._name, ix, iy);
            }
        }
        else {
            if (dy >= -dx) {	// put name right
                    ix += 2;
                    iy += h/2 - 4;
                    ctx.fillText(this._name, ix, iy);
            } else { 		// put name above
                ix -= w/2;
                iy += -h/2 + 4;
                ctx.fillText(this._name, ix, iy);
            }
        }
    }
 
    public get name() {
        return this._name;
    }
 
    public set name(n : string) {
        this._name = n;
    }
 
    public abstract update() : void;
    public reset() { this.update(); }
    public defined() : boolean { return false; }
    public drag(tox: number, toy: number) : boolean { return false; }
    // drag returns true when the element is actually dragged
    public abstract translate(dx: number, dy: number) : void;
    public abstract rotate(pivot : PointElement, ac : number, as: number) : void;
    public abstract drawName(c: SlateCanvas) : void;
    public abstract drawFace(c: SlateCanvas) : void;
    public abstract drawEdge(c: SlateCanvas) : void;
    public abstract drawVertex(c: SlateCanvas) : void;
 
    set nameColor(value: string) {
        this._nameColor = value;
    }
 
    set edgeColor(value: string) {
        this._edgeColor = value;
    }
 
    set faceColor(value: string) {
        this._faceColor = value;
    }
 
    set nameHighlightColor(value: string) {
        this._nameHighlightColor = value;
    }
 
    set vertexHighlightColor(value: string) {
        this._vertexHighlightColor = value;
    }
 
    set edgeHighlightColor(value: string) {
        this._edgeHighlightColor = value;
    }
 
    set faceHighlightColor(value: string) {
        this._faceHighlightColor = value;
    }
 
    set draggable(value: boolean) {
        this._draggable = value;
    }
 
    set dimension(value: number) {
        this._dimension = value;
    }
 
    set align(value: Align) {
        this._align = value;
    }
 
    set shouldHighlight(value: boolean) {
        this._shouldHighlight = value;
    }
 
    get nameColor(): string {
        return this._nameColor;
    }
 
    get vertexColor(): string {
        return this._vertexColor;
    }
 
    set vertexColor(c : string) {
        this._vertexColor = c;
    }
 
    get edgeColor(): string {
        return this._edgeColor;
    }
 
    get faceColor(): string {
        return this._faceColor;
    }
 
    get nameHighlightColor(): string {
        return this._nameHighlightColor;
    }
 
    get vertexHighlightColor(): string {
        return this._vertexHighlightColor;
    }
 
    get edgeHighlightColor(): string {
        return this._edgeHighlightColor;
    }
 
    get faceHighlightColor(): string {
        return this._faceHighlightColor;
    }
 
    get draggable(): boolean {
        return this._draggable;
    }
 
    get dimension(): number {
        return this._dimension;
    }
 
    get align(): Align {
        return this._align;
    }
 
    get shouldHighlight(): boolean {
        return this._shouldHighlight;
    }
}