all files / common/core/graphics/ circle.ts

100% Statements 31/31
100% Branches 5/5
100% Functions 9/9
100% Lines 29/29
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                                                                                           
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { Position } from './position';
import { Rect } from './rect';
 
export class Circle {
    /**
     * Return a circle from his outer rectangle.
     *
     * @param left      The left position of the circle center or a rectangle object.
     * @param top       The top position of the circle center
     * @param width     The width of the outer rectangle
     * @param height    The height of the outer rectangle
     * @return A circle contained end centered inside the passed ractangle
     */
    public static fromOuterRect(left?: number | Object, top?: number, width?: number, height?: number) {
        if (typeof left === 'object') {
            const bounds = left as any;
            left = bounds.left;
            top = bounds.top;
            width = bounds.width;
            height = bounds.height;
        }
        const radius = Math.min(width, height) / 2;
 
        const center = new Position(+left + (width / 2), top + (height / 2));
        return new Circle(center, radius);
    }
 
    /** Return a boolean indicating if the two circle are equals */
    public static equals(c1: Circle, c2: Circle) {
        return !c1 === !c2 && Position.equals(c1.center, c2.center) && c1.radius === c2.radius;
    }
 
    /** Create e new circle instance from the center position and the radius */
    constructor(public center: Position, public radius: number) {
 
    }
 
    /** Return the outer rectangle of the circle */
    public get outerRect(): Rect {
        return Rect.fromLTRB(this.center.left - this.radius, this.center.top - this.radius, this.center.left + this.radius, this.center.top + this.radius);
    }
 
    /** Return a boolean indicate if the circle contains the passed circle */
    public contains(circle: Circle) {
        return this.outerRect.contains(circle.outerRect);
    }
 
    /** Return a boolean indicate if the passed point is inside the circle */
    public containsPoint(point: Position): boolean {
        const dx = Math.abs(point.left - this.center.left);
        const dy = Math.abs(point.top - this.center.top);
 
        return dx * dx + dy * dy <= this.radius * this.radius;
    }
 
    /** Return a the cloned of the cirlce */
    public clone() {
        return new Circle(this.center, this.radius);
    }
 
    /** Inflate the circle radius with the specified value */
    public inflate(radius: number) {
        return new Circle(this.center, this.radius + radius);
    }
}