import { Point } from './point.js'; import { Rectangle } from './rectangle.js'; import { distance } from '../util/math.js'; import { clone, contains, circumferencePoint } from './util/circle.js'; import { GEOM_CIRCLE } from '../core/const.js'; export class Circle { public x: number; public y: number; public _diameter: number; public _radius: number; public type: number; /** * Creates a new Circle instance. * @param {number} x - The x coordinate of the center point (default: 0). * @param {number} y - The y coordinate of the center point (default: 0). * @param {number} diameter - The diameter of the circle (default: 0). */ public constructor(x = 0, y = 0, diameter = 0) { /** @type {number} */ this.x = x; /** @type {number} */ this.y = y; /** @type {number} */ this._diameter = diameter; /** @type {number} */ this._radius = 0; if (diameter > 0) { this._radius = diameter * 0.5; } /** @type {number} */ this.type = GEOM_CIRCLE; } /** * Calculates the circumference of this circle. * @returns {number} The circumference of this circle. */ public circumference(): number { return 2 * (Math.PI * this._radius); } /** * Returns a random point within this circle. * @param {Point} output - The point to store the result in (optional). * @returns {Point} A random point within this circle. */ public random(output: Point | null = null): Point { const result = output ?? new Point(); const t = 2 * Math.PI * Math.random(); const u = Math.random() + Math.random(); const r = u > 1 ? 2 - u : u; const x = r * Math.cos(t); const y = r * Math.sin(t); result.x = this.x + x * this.radius; result.y = this.y + y * this.radius; return result; } /** * Gets the bounding rectangle of this circle. * @returns {Rectangle} The bounding rectangle of this circle. */ public getBounds(): Rectangle { return new Rectangle(this.x - this.radius, this.y - this.radius, this.diameter, this.diameter); } /** * Sets the position and size of this circle to new values. * @param {number} x - The new x coordinate of the center point. * @param {number} y - The new y coordinate of the center point. * @param {number} diameter - The new diameter of the circle. * @returns {Circle} This circle instance for chaining. */ public setTo(x: number, y: number, diameter: number): this { this.x = x; this.y = y; this._diameter = diameter; this._radius = diameter * 0.5; return this; } /** * Copies the values from another circle to this circle. * @param {Circle} source - The circle to copy values from. * @returns {Circle} This circle instance for chaining. */ public copyFrom(source: Circle): this { return this.setTo(source.x, source.y, source.diameter); } /** * Copies the values of this circle to another circle. * @param {Circle} dest - The circle to copy values to. * @returns {Circle} The destination circle. */ public copyTo(dest: Circle): Circle { dest.x = this.x; dest.y = this.y; dest.diameter = this._diameter; return dest; } /** * Calculates the distance between this circle and another circle. * @param {Circle} dest - The other circle to calculate the distance to. * @param {boolean} round - Whether to round the result (default: false). * @returns {number} The distance between the circles. */ public distance(dest: { x: number; y: number }, round = false): number { const d = distance(this.x, this.y, dest.x, dest.y); return round ? Math.round(d) : d; } /** * Creates a clone of this circle. * @returns {Circle} A new circle with the same values as this one. */ public clone(): Circle { return clone(this); } /** * Checks if the specified point is contained within this circle. * @param {number} x - The x coordinate of the point to check. * @param {number} y - The y coordinate of the point to check. * @returns {boolean} True if the point is contained within this circle, false otherwise. */ public contains(x: number, y: number): boolean { return contains(this, x, y); } /** * Gets a point at a specific angle on the circumference of this circle. * @param {number} angle - The angle in radians (or degrees if asDegrees is true) to get the point for. * @param {boolean} asDegrees - Whether the angle is provided in degrees (default: false). * @param {Point} out - The point to store the result in (optional). * @returns {Point} A point at the specified angle on the circumference of this circle. */ public circumferencePoint(angle: number, asDegrees?: boolean, out?: Point): Point { return circumferencePoint(this, angle, asDegrees, out); } /** * Offsets the position of this circle by the specified amounts. * @param {number} dx - The amount to offset the x coordinate by. * @param {number} dy - The amount to offset the y coordinate by. * @returns {Circle} This circle instance for chaining. */ public offset(dx: number, dy: number): this { this.x += dx; this.y += dy; return this; } /** * Offsets the position of this circle by the specified point coordinates. * @param {Point} point - The point to offset the circle by. * @returns {Circle} This circle instance for chaining. */ public offsetPoint(point: Point): this { return this.offset(point.x, point.y); } /** * Returns a string representation of this circle. * @returns {string} A string representation of the circle. */ public toString(): string { return `[{Circle (x=${this.x} y=${this.y} diameter=${this.diameter} radius=${this.radius})}]`; } /** * Gets the diameter of this circle. * @returns {number} The diameter of this circle. */ public get diameter(): number { return this._diameter; } /** * Sets the diameter of this circle. */ public set diameter(value: number) { if (value > 0) { this._diameter = value; this._radius = value * 0.5; } } /** * Gets the radius of this circle. * @returns {number} The radius of this circle. */ public get radius(): number { return this._radius; } /** * Sets the radius of this circle. */ public set radius(value: number) { if (value > 0) { this._radius = value; this._diameter = value * 2; } } /** * Gets the left coordinate of this circle. * @returns {number} The left coordinate of this circle. */ public get left(): number { return this.x - this._radius; } /** * Sets the left coordinate of this circle. */ public set left(value: number) { if (value > this.x) { this._radius = 0; this._diameter = 0; } else { this.radius = this.x - value; } } /** * Gets the right coordinate of this circle. * @returns {number} The right coordinate of this circle. */ public get right(): number { return this.x + this._radius; } /** * Sets the right coordinate of this circle. */ public set right(value: number) { if (value < this.x) { this._radius = 0; this._diameter = 0; } else { this.radius = value - this.x; } } /** * Gets the top coordinate of this circle. * @returns {number} The top coordinate of this circle. */ public get top(): number { return this.y - this._radius; } /** * Sets the top coordinate of this circle. */ public set top(value: number) { if (value > this.y) { this._radius = 0; this._diameter = 0; } else { this.radius = this.y - value; } } /** * Gets the bottom coordinate of this circle. * @returns {number} The bottom coordinate of this circle. */ public get bottom(): number { return this.y + this._radius; } /** * Sets the bottom coordinate of this circle. */ public set bottom(value: number) { if (value < this.y) { this._radius = 0; this._diameter = 0; } else { this.radius = value - this.y; } } /** * Gets the area of this circle. * @returns {number} The area of this circle. */ public get area(): number { if (this._radius > 0) { return Math.PI * this._radius * this._radius; } return 0; } /** * Checks if this circle is empty (has zero diameter). * @returns {boolean} True if the circle is empty, false otherwise. */ public get empty(): boolean { return this._diameter === 0; } /** * Sets whether this circle is empty (zero diameter). */ public set empty(value: boolean) { if (value) { this.setTo(0, 0, 0); } } }