Skip to main content

Circle

The Circle class represents a circle in 2D space.


Constructor

prototype
constructor(position: Point, radius: number, options: CircleOptions = {});

Properties

  • position

    • Type: Point
    • The position of the circle (from the center).
  • radius

    • Type: number
    • The radius of the circle.
  • options (optional)

    • Type: CircleOptions
    • Default value: {}
    • The options for the circle

Methods

setOptions

prototype
setOptions(options: CircleOptions): Circle

Set the options of the circle.

Parameters

  • options
    • Type: CircleOptions
    • The circle options.

Return

Circle The circle.

Example

const circle = new Circle(new Point(10, 10), 10);
circle.setOptions({ fillColor: 'red', weight: 2, strokeColor: 'blue' });

toArray

prototype
toArray(): [number, number, number]

Converts a circle to an array.

Return

[number, number, number] Array with 3 numbers, the two first are the position of the circle and the third is the radius.

Example

const circle = new Circle(new Point(10, 10), 10);
const array = circle.toArray();

equals

prototype
equals(circle: Circle): boolean

Checks if the circle is equal to another circle.

Parameters

  • circle
    • Type: Circle
    • The circle to compare with

Return

boolean Whether the circles are equal.

Example

const circle1 = new Circle(new Point(10, 10), 10);
const circle2 = new Circle(new Point(10, 10), 10);
const circle3 = new Circle(new Point(10, 10), 20);
circle1.equals(circle2); // true
circle1.equals(circle3); // false
circle2.equals(circle3); // false

isContainsPoint

prototype
isContainsPoint(point: Point): boolean

Checks if the circle contains a point.

Parameters

  • point
    • Type: Point
    • The point to check

Return

boolean Whether the circle contains the point.

Example

const circle = new Circle(new Point(10, 10), 10);
circle.isContainsPoint(new Point(12, 12)); // true

isIntersectsWithCircle

prototype
isIntersectsWithCircle(circle: Circle): boolean

Checks if the circle intersects with another circle.

Parameters

  • circle
    • Type: Circle
    • The circle to check.

Return

boolean Whether the circle intersects with the other circle.

Example

const circle1 = new Circle(new Point(10, 10), 10);
const circle2 = new Circle(new Point(10, 10), 10);
const circle3 = new Circle(new Point(10, 10), 20);
circle1.isIntersectsWithCircle(circle2); // true
circle1.isIntersectsWithCircle(circle3); // true
circle2.isIntersectsWithCircle(circle3); // true

isIntersectsWithLine

prototype
isIntersectsWithLine(line: Line): boolean

Check if the circle intersects with a line.

Parameters

  • line
    • Type: Line
    • The line to check.

Return

boolean Whether the circle intersects with the line.

Example

const circle = new Circle(new Point(10, 10), 10);
const line = new Line(new Point(0, 0), new Point(20, 20));
circle.isIntersectsWithLine(line); // true

getIntersectionsWithLine

prototype
getIntersectionsWithLine(line: Line): Point[]

Get the intersections with a line.

Parameters

  • line
    • Type: Line
    • The line to get the intersections with.

Return

Point[] The intersections with the line.

Example

const circle = new Circle(new Point(10, 10), 10);
const line = new Line(new Point(0, 0), new Point(20, 20));
const intersections = circle.getIntersectionsWithLine(line);

isIntersectsWithRectangle

prototype
isIntersectsWithRectangle(rectangle: Rectangle): boolean

Get the intersections with a line.

Parameters

  • rectangle
    • Type: Rectangle
    • The rectangle to check.

Return

boolean Whether the circle intersects with the rectangle.

Example

const circle = new Circle(new Point(10, 10), 10);
const rectangle = new Rectangle(new Point(0, 0), new Size(20, 20));
circle.isIntersectsWithRectangle(rectangle); // true

getIntersectionsWithRectangle

prototype
getIntersectionsWithRectangle(rectangle: Rectangle): Point[]

Get the intersections with a line.

Parameters

  • rectangle
    • Type: Rectangle
    • The rectangle to get the intersections with.

Return

Point[] The intersections with the rectangle.

Example

const circle = new Circle(new Point(10, 10), 10);
const rectangle = new Rectangle(new Point(0, 0), new Size(20, 20));
const intersections = circle.getIntersectionsWithRectangle(rectangle);

clone

prototype
clone(): Circle

Get a copy of circle.

Return

Circle The copy.

Example

const circle = new Circle(new Point(10, 10), 10);
const clonedCircle = circle.clone();

draw

prototype
draw(ctx: CanvasRenderingContext2D): void

Draw the circle.

Parameters

  • ctx
    • Type: CanvasRenderingContext2D
    • The canvas context.

Return

void

Example

const circle = new Circle(new Point(10, 10), 10);
//...
circle.draw(ctx);

Static methods

fromArray

prototype
static fromArray(array: [number, number, number]): Circle

Creates a circle from an array

Parameters

  • array
    • Type: [number, number, number]
    • Array with 3 numbers, the two first are the position of the circle and the third is the radius.

Return

Circle The circle.

Example

const circle = Circle.fromArray([10, 10, 10]);