@itwin/core-react
Version:
A react component library of iTwin.js UI general purpose components
75 lines • 3.45 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module RadialMenu
*/
import { Point } from "../utils/Point.js";
/** 2D Line consisting of a start point, and an end point
* @internal
*/
export class Line {
constructor(p1, p2) {
/** checks for equality with the components of this, and line parameter */
this.equals = (line) => this.p1.equals(line.p1) && this.p2.equals(line.p2);
this.p1 = p1 || new Point();
this.p2 = p2 || new Point();
}
}
/** 2D Circle.
* @internal
*/
export class Circle {
constructor(center, radius) {
this.center = center || new Point();
this.radius = radius || 0;
}
}
/** 2D Annulus (2D doughnut shape/flattened torus) defined by an inner and outer circle with a shared center point.
* @internal
*/
export class Annulus {
constructor(center, innerRadius, outerRadius) {
this.center = center || new Point();
this.inner = new Circle(center, innerRadius);
this.outer = new Circle(center, outerRadius);
}
}
/** 2D Sector of an Annulus, defined by both a parent annulus, a startAngle, and an endAngle.
* @internal
*/
export class AnnularSector {
/**
* initialize AnnularSector on parent annulus, and generate SVG Path to store in this.path
* @param parent parent annulus to initialize sector on.
* @param startAngle angle to begin the annular sector on.
* @param endAngle angle to end the annular sector on.
*/
constructor(parent, startAngle, endAngle) {
this.parent = parent;
this.startAngle = startAngle;
this.endAngle = endAngle;
// adapted from https://gist.github.com/buschtoens/4190516
const { x: cx, y: cy } = parent.center;
const inner = parent.inner.radius;
const outer = parent.outer.radius;
this.innerStart = new Point(cx + inner * Math.cos(startAngle), cy + inner * Math.sin(startAngle));
this.outerStart = new Point(cx + outer * Math.cos(startAngle), cy + outer * Math.sin(startAngle));
this.start = new Line(this.innerStart, this.outerStart);
this.outerEnd = new Point(cx + outer * Math.cos(endAngle), cy + outer * Math.sin(endAngle));
this.innerEnd = new Point(cx + inner * Math.cos(endAngle), cy + inner * Math.sin(endAngle));
this.end = new Line(this.outerEnd, this.innerEnd);
const angleDiff = endAngle - startAngle;
const largeArc = angleDiff % (Math.PI * 2) > Math.PI ? 1 : 0;
const sectorCommands = [];
sectorCommands.push(`M${this.innerStart.x},${this.innerStart.y}`); // moveTo
sectorCommands.push(`L${this.outerStart.x},${this.outerStart.y}`); // lineTo
sectorCommands.push(`A${outer},${outer} 0 ${largeArc} 1 ${this.outerEnd.x},${this.outerEnd.y}`); // arcTo
sectorCommands.push(`L${this.innerEnd.x},${this.innerEnd.y}`); // lineTo
sectorCommands.push(`A${inner},${inner} 0 ${largeArc} 0 ${this.innerStart.x},${this.innerStart.y}`); // arcTo
sectorCommands.push(`z`); // closePath
this.path = sectorCommands.join(" ");
}
}
//# sourceMappingURL=Annulus.js.map