All files / src/elements GeomElement.ts

93.69% Statements 327/349
82.45% Branches 47/57
90.47% Functions 38/42
93.69% Lines 327/349

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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 3501x 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 58997x 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 1x 1x 1x 1x 1x 1x 1x 719x 719x 719x 1x 1x 1x 123867x 123867x 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 124227x 124227x 124227x 124227x 124227x 124227x 124227x 124227x 1x 1x 123765x 123765x 123765x 123765x 123765x 123765x 123765x 123765x         123765x         123765x         123765x         123765x 123765x 123765x 123765x 123765x 123765x 123765x 123765x 65736x 65736x 31508x 31508x 31508x 31508x 34228x 34228x 34228x 34228x 34228x 34228x 65736x 58029x 58029x 29110x 29110x 29110x 58029x 28919x 28919x 28919x 28919x 58029x 123765x 1x 1x 1327140x 1327140x 1x 1x 21110x 21110x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20805x 20805x 1x 1x 15251x 15251x 1x 1x 25672x 25672x 1x 1x     1x 1x 4369x 4369x 1x 1x 3x 3x 1x 1x 4368x 4368x 1x 1x 6889x 6889x 1x 1x 70588x 70588x 1x 1x 14941x 14941x 1x 1x 188x 188x 1x 1x 367x 367x 1x 1x 87x 87x 1x 1x 9x 9x 1x 1x 399x 399x 399x 399x 399x 1x 1x 478667x 478667x 1x 1x 471748x 471748x 1x 1x 19858x 19858x 1x 1x 129097x 129097x 1x 1x 70230x 70230x 1x 1x     1x 1x 3x 3x 1x 1x 22x 22x 1x 1x 2x 2x 1x 1x 32437x 32437x 1x 1x 14934x 14934x 1x 1x     1x 1x 1513384x 1513384x 1x 1x 1039296x 1039296x 1x 1x 1229383x 1229383x 1x 1x 284043x 284043x 1x 1x 1115001x 1115001x 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;
    // #182 — true when this element's face is the dim-2 default
    // (lighten(bgcolor), opaque) rather than a face the author asked for.
    // Harmless at rest; #140 promotes a highlighted or animated element to
    // draw last within its pass, at which point an opaque near-white face
    // paints over whatever is beneath it. Recorded at construction because
    // the default is a computed colour string, so comparing colours later
    // could not tell "author asked for this" from "we filled it in".
    public faceIsDefault : boolean = false;
 
    // Highlight defaults pick high-contrast values that read on the typical
    // cream / white backgrounds the library is used against. The previous
    // edge/vertex default of white was invisible on the Elements site's
    // theorem-box background (HSB 35,19,100 ≈ #FFE9CD). Web "gold"
    // (#FFD700) is brighter / more saturated than Material amber and
    // still reads against both cream and white.
    private _nameHighlightColor   : string = '#FFD700';
    private _vertexHighlightColor : string = '#FFD700';
    private _edgeHighlightColor   : string = '#FFD700';
    private _faceHighlightColor   : string = '#FFD700';
 
    // Default matches Java: Font("TimesRoman", Font.ITALIC, 18)
    protected static _fontSize : number = 18;
    protected static _fontName : string = "Times New Roman";
 
    // Style scale (#71): when the figure is fit-scaled down to a narrow
    // display (view scale F < 1), decorations — label font, vertex dots,
    // line widths — are multiplied by 1/F so they keep a constant on-screen
    // size and stay readable while the geometry shrinks. Slate.drawElements
    // sets it from the view scale before each frame; 1 (the default, and
    // F=1 desktop/headless) leaves rendering bit-for-bit unchanged.
    static styleScale : number = 1;
 
    static setFont(fontName: string, fontSize: number): void {
        GeomElement._fontName = fontName;
        GeomElement._fontSize = fontSize;
    }
 
    // The label font, scaled by the current styleScale.
    static fontString(): string {
        return `italic ${GeomElement._fontSize * GeomElement.styleScale}px ${GeomElement._fontName}`;
    }
 
    private _draggable : boolean;
    private _dimension : number;
    private _align : Align;
 
    private _shouldHighlight : boolean = false;
    // Per-element visibility flag honoured by every draw method. Default
    // true preserves existing consumer behaviour; flipping false makes
    // drawFace / drawEdge / drawVertex / drawName early-return so the
    // element disappears without being removed from _elements (so
    // dependents that read its coordinates still work). Used by
    // Slate.setVisibleNames + the slideshow Presentation overlay.
    private _visible : boolean = true;
    // One step above shouldHighlight: when the slideshow caption's
    // {NAME} ref is hovered or clicked, the matching element flips to
    // emphasized. drawEdge / drawVertex paint with the highlight colour
    // at an even thicker stroke / larger dot so the element pops out
    // from any other already-highlighted ones on the slide. Released on
    // mouseleave (or click-to-toggle for sticky).
    // Emphasis amount in [0, 1]. Boolean emphasized is a thin shim
    // that toggles between 0 and 1; the SlateAnimator fades the
    // amount smoothly from 1 → 0 after an animation finishes so the
    // visible "shrink back to highlight thickness" feels less jarring
    // when the underlying element is also slide-highlighted (e.g.
    // ABC polygon edges over the highlighted AB / AC / BC).
    private _emphasisAmount : number = 0;
    // Animation progress for the next slide-transition render. 1 means
    // "fully drawn" (the default and the existing behaviour). Values in
    // [0, 1) tell per-type draw methods to render a partial geometry —
    // a half-traced line, a 30° sweep of an arc, the first edge of a
    // polygon plus a fraction of the next. Driven by Animation
    // subclasses through SlateAnimator during slide transitions; flips
    // back to 1 on each animation's finalise() so the canvas returns
    // to the fully-rendered state when the transition completes or is
    // cancelled.
    private _drawProgress : number = 1;
    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.fontString();
        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;
    }
 
    set visible(value: boolean) {
        this._visible = value;
    }
 
    set emphasized(value: boolean) {
        this._emphasisAmount = value ? 1 : 0;
    }
 
    set emphasisAmount(value: number) {
        this._emphasisAmount = value < 0 ? 0 : (value > 1 ? 1 : value);
    }
 
    set drawProgress(value: number) {
        // Clamp to [0, 1] — out-of-range values would push the partial
        // geometry past either endpoint, which is never what callers
        // mean. Animations tick this in [0, 1] and finalise to 1.
        this._drawProgress = value < 0 ? 0 : (value > 1 ? 1 : 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;
    }
 
    get visible(): boolean {
        return this._visible;
    }
 
    get emphasized(): boolean {
        return this._emphasisAmount > 0;
    }
 
    get emphasisAmount(): number {
        return this._emphasisAmount;
    }
 
    get drawProgress(): number {
        return this._drawProgress;
    }
}