All files / src/elements/sector SectorElement.ts

96.61% Statements 257/266
87.09% Branches 54/62
100% Functions 20/20
96.61% Lines 257/266

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 2671x 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 5x 5x 1x 1x 1x 188x 188x 188x 101x 101x 101x 101x 101x 188x 1x 1x 1874x 1874x 1x 1x 2086x 2086x 1x 1x 1x 1x 1x 1x 1x 1x 294x 294x     294x 294x 294x 294x 294x 1x 1x 1407x 1370x 1370x 1370x 1370x 1370x 1407x 1407x 1407x 1407x 1170x 1170x 1170x 1407x 1407x 1407x 1170x 1170x 1x 1x 1x 1x 1x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1159x 1138x 1159x 12x 21x 9x 9x 9x 9x 9x 9x 1159x 1159x 1159x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1609x 1609x 1x 1x 1x 1x 1x 1x 1609x 1609x 1x 1x 1x 1x 1x 10x 10x 10x 10x 1x 1x 1x 1x 1x 485x 485x 485x 485x 485x 485x 485x 485x 485x 485x 485x 485x       485x 485x 1x 1x 1x 1x 1x 452x 452x 1x 1x 1360x 1360x 485x 485x 485x 485x 485x 485x 485x 485x 485x 485x 1360x         1360x 485x 485x 485x 485x 485x 485x 485x 1360x 1x 1x 1x 1x 1x 1x 1x 1360x 1x 1x 1356x 1x 1x 1x 1x 1x 1x 1x 1x 294x 294x 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;
 
    // Face fill alpha — drives drawFace's globalAlpha independent of
    // drawProgress (which drives the arc sweep). Default 1 keeps every
    // existing render path identical. Mirrors Circle/Polygon (#86).
    private _faceAlpha : number = 1;
 
    set faceAlpha(value: number) {
        this._faceAlpha = value < 0 ? 0 : (value > 1 ? 1 : value);
    }
    get faceAlpha(): number { return this._faceAlpha; }
 
    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.visible && !this.shouldHighlight && this.emphasisAmount <= 0) return;
        // Pick the highlight color BEFORE the null bail, matching the
        // other element types (#81 / #86) — a zero-color sector can
        // still render in the highlight stroke while emphasized or
        // slide-highlighted, which is what lets invisible angle
        // markers light up on demand.
        const color = (this.emphasized || this.shouldHighlight)
            ? this.edgeHighlightColor
            : this.edgeColor;
        if (color == null || !this.defined()) return;
        let ctx = c.getContext("2d") as CanvasRenderingContext2D;
        ctx.strokeStyle = color;
        {
            const baseW = this.shouldHighlight ? 3 : 1;
            ctx.lineWidth = (baseW + this.emphasisAmount * (6 - baseW)) * GeomElement.styleScale;
        }
        this._traceEdge(ctx, this.radius());
    }
 
    // Trace and stroke the edge at radius r. The sector's edge is its arc;
    // a right-angle marker (#201) overrides this to draw the square corner
    // instead, keeping the colour / width / visibility preamble above.
    protected _traceEdge(ctx: CanvasRenderingContext2D, r: number): void {
        ctx.beginPath();
        let startAngle = Math.atan2(
            this._A.y - this._Center.y,
            this._A.x - this._Center.x);
        let arcAngle = this._arcAngle();
        // Partial sweep for slide-transition animation: the arc grows
        // from the A arm toward the B arm as drawProgress goes 0 → 1.
        // Default progress = 1 reproduces the full arc bit-for-bit — the
        // original expression is kept for it so no golden can move.
        //
        // #172 — mid-sweep, scale the sweep ctx.arc actually traces, not
        // the signed angle. For the arm order whose signed angle opposes
        // the fixed direction, the drawn arc is the complement, and scaling
        // the signed angle made it open as a full circle and shrink.
        const acw = this._anticlockwise();
        let from = startAngle, flag = acw, endAngle: number;
        if (this.drawProgress >= 1) {
            endAngle = startAngle + arcAngle;
        } else if (!this.sweepReverse) {
            endAngle = startAngle + (acw ? -1 : 1) * this._drawnSweep() * this.drawProgress;
        } else {
            // Grow from the B arm back toward A: the same arc, traced the
            // other way. Start at B and go against the drawing direction.
            from = Math.atan2(this._B.y - this._Center.y, this._B.x - this._Center.x);
            flag = !acw;
            endAngle = from + (acw ? 1 : -1) * this._drawnSweep() * this.drawProgress;
        }
        ctx.arc(this._Center.x, this._Center.y, r, from, endAngle, flag);
        ctx.stroke();
    }
 
    // Mid-sweep only: grow the arc from the B arm toward A instead of from
    // A toward B (#172). Set by A.Sector.sweep's `reverse` arg for the run
    // and cleared on finalise; the finished arc is the same either way.
    public sweepReverse: boolean = false;
 
    // The sweep ctx.arc actually traces from the A arm to the B arm: a
    // non-negative magnitude in the _anticlockwise() direction (#172).
    //
    // The direction is fixed and the angle is signed, so for one arm order
    // the arc drawn is the COMPLEMENT of the signed angle. That is the
    // original applet's contract — SectorElement.java normalises a negative
    // sweep by adding 360° — and the author orders the arms to pick the
    // side. Static renders are identical either way; this only decides how
    // the animation gets there.
    protected _drawnSweep(): number {
        const a = this._arcAngle();
        let s = this._anticlockwise() ? -a : a;
        if (s < 0) s += 2 * Math.PI;
        return s;
    }
 
    // The signed arc to sweep from the A arm to the B arm, in radians.
    // Default is the minor signed angle (−π..π). AngleMarkerElement
    // overrides this to optionally sweep the major (reflex) arc.
    protected _arcAngle(): number {
        return this._Center.angle(this._A, this._B, this._P);
    }
 
    // Sweep direction passed to ctx.arc. Fixed true for plain sectors /
    // arcs (their authors order _A/_B for that). AngleMarkerElement
    // flips it to draw the major arc between the same two rays for a
    // reflex marker.
    protected _anticlockwise(): boolean {
        return true;
    }
 
    // Magnitude of the swept arc — used by A.Sector.sweep to size the
    // animation duration to what's actually drawn (so a reflex marker
    // doesn't sweep a big arc in a tiny time).
    public arcSpan(): number {
        // #172 — the DRAWN sweep, so the complement case (a 270° arc from
        // a 90° signed angle) is timed for what it traces.
        return this._drawnSweep();
    }
 
    // Trace the closed wedge path (arc + the two radii back to centre)
    // into the current ctx path. Shared by the face fill and the flash
    // overlay so they cover exactly the same region.
    protected _traceWedge(ctx: CanvasRenderingContext2D, r: number): void {
        let startAngle = Math.atan2(
            this._A.y - this._Center.y,
            this._A.x - this._Center.x);
        let arcAngle = this._arcAngle();
        let endAngle = startAngle + arcAngle;
        ctx.beginPath();
        ctx.arc(this._Center.x, this._Center.y, r, startAngle, endAngle, this._anticlockwise());
        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);
    }
 
    // Whether the whole wedge should flash (fill with the highlight
    // color) while emphasised. False for plain sectors/arcs — only
    // angle markers light up their fill during a slide transition.
    protected _flashFace(): boolean {
        return false;
    }
 
    drawFace(c: SlateCanvas): void {
        if (!this.visible && !this.shouldHighlight && this.emphasisAmount <= 0) return;
        if (this.faceColor == null || !this.defined()) return;
        // Face is independent of the edge sweep — always the full
        // sector wedge, with alpha driven by the dedicated faceAlpha
        // field (mirrors Circle/Polygon, #86). Default 1 preserves
        // the fully-opaque behaviour for every non-animating consumer.
        let a = this.faceAlpha;
        if (a <= 0) return;
        let ctx = c.getContext("2d") as CanvasRenderingContext2D;
        let r = this.radius();
        ctx.fillStyle = this.faceColor;
        this._traceWedge(ctx, r);
        if (a < 1) {
            ctx.save();
            ctx.globalAlpha = a;
            ctx.fill();
            ctx.restore();
        } else {
            ctx.fill();
        }
        // Transition flash: while emphasised (the animator bumps
        // emphasisAmount to 1 during a sweep and fades it 1 → 0
        // afterward), light the ENTIRE wedge with the highlight color,
        // fading with the emphasis. Edge-only emphasis (stroke width /
        // gold) already happens in drawEdge; this fills the area too.
        if (this._flashFace() && this.emphasisAmount > 0) {
            ctx.save();
            ctx.fillStyle = this.faceHighlightColor;
            ctx.globalAlpha = 0.55 * this.emphasisAmount;
            this._traceWedge(ctx, r);
            ctx.fill();
            ctx.restore();
        }
    }
 
    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();
    }
 
}