Circle Entity (CircleEnt)
Circle Entity (CircleEnt)
CircleEnt represents a circle entity in CAD, fully defined by a center point and a radius.
Online Examples
| Example | Description | Link |
|---|---|---|
| Create Circle | Basic CircleEnt creation example | Online Demo{target="_blank"} |
| Circle Properties | Get circle radius, area, circumference | Online Demo{target="_blank"} |
| Circle Transform | Move, scale, mirror operations | Online Demo{target="_blank"} |
Overview
The circle is one of the most fundamental curved shapes, widely used in mechanical, architectural, and other design fields. CircleEnt provides comprehensive circle operation capabilities, including geometric calculations, transformations, and editing.
Constructor
import { CircleEnt, Point2D } from 'vjcad';
// Shorthand syntax (recommended)
const circle1 = new CircleEnt([50, 50], 25);
// Point2D syntax
const circle2 = new CircleEnt(new Point2D(50, 50), 25);
// Constructor with color
const circle3 = new CircleEnt([100, 100], 30, 3); // Color index (3=green)Shorthand Syntax
Both the constructor and the center property support [x, y] array-style coordinates, so you don't have to write new Point2D(...) every time.
Properties
Basic Properties
| Property | Type | Access | Description |
|---|---|---|---|
center | Point2D | Read/Write | Center point coordinates |
radius | number | Read/Write | Circle radius |
Computed Properties (Read-only)
| Property | Type | Description |
|---|---|---|
area | number | Area of the circle (πr²) |
circumference | number | Circumference of the circle (2πr) |
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Get properties
console.log('Center:', circle.center); // Point2D(50, 50)
console.log('Radius:', circle.radius); // 25
console.log('Area:', circle.area); // 1963.49...
console.log('Circumference:', circle.circumference); // 157.08...
// Modify properties
circle.center = new Point2D(100, 100);
circle.radius = 50;
// Directly modify center coordinates
circle.center.x = 75;
circle.center.y = 75;Methods
Geometric Calculations
getDiameter() - Get Diameter
const circle = new CircleEnt(new Point2D(50, 50), 25);
console.log('Diameter:', circle.getDiameter()); // 50setDiameter() - Set Radius via Diameter
const circle = new CircleEnt(new Point2D(50, 50), 25);
circle.setDiameter(100);
console.log('New radius:', circle.radius); // 50setArea() - Set Radius via Area
Calculates and sets the circle's radius based on a specified area. Uses the formula: radius = √(area / π)
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Original area
console.log('Original area:', circle.area); // 1963.49...
console.log('Original radius:', circle.radius); // 25
// Set area to 1000
circle.setArea(1000);
console.log('New radius:', circle.radius); // 17.84...
console.log('New area:', circle.area); // 1000 (verification)
// Practical use: create a circle with a specified area
const targetArea = 500; // Target area of 500 square units
const circle2 = new CircleEnt(new Point2D(100, 100), 1);
circle2.setArea(targetArea);
console.log('Radius of target area circle:', circle2.radius.toFixed(2)); // 12.62getCircumference() - Get Circumference (Method)
In addition to the circumference property, you can also use the getCircumference() method to get the circumference.
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Option 1: Using the property
console.log('Circumference:', circle.circumference); // 157.08...
// Option 2: Using the method
console.log('Circumference:', circle.getCircumference()); // 157.08...setCircumference() - Set Radius via Circumference
Calculates and sets the circle's radius based on a specified circumference. Uses the formula: radius = circumference / (2π)
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Original circumference
console.log('Original circumference:', circle.circumference); // 157.08...
console.log('Original radius:', circle.radius); // 25
// Set circumference to 100
circle.setCircumference(100);
console.log('New radius:', circle.radius); // 15.91...
console.log('New circumference:', circle.circumference); // 100 (verification)
// Practical use: create a circle with a specified circumference
const targetCircumference = 200; // Target circumference of 200 units
const circle2 = new CircleEnt(new Point2D(100, 100), 1);
circle2.setCircumference(targetCircumference);
console.log('Radius of target circumference circle:', circle2.radius.toFixed(2)); // 31.83Difference Between Property and Method
- The
circumferenceproperty andgetCircumference()method are functionally identical, both returning the circumference - Property syntax is more concise:
circle.circumference - Method syntax pairs with other setters:
circle.getCircumference()/circle.setCircumference()
getQuadPoints() - Get Quadrant Points
Returns the four quadrant points of the circle (right, top, left, bottom).
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Get standard quadrant points (no rotation)
const quadPoints = circle.getQuadPoints();
console.log('Right:', quadPoints[0]); // Point2D(75, 50)
console.log('Top:', quadPoints[1]); // Point2D(50, 75)
console.log('Left:', quadPoints[2]); // Point2D(25, 50)
console.log('Bottom:', quadPoints[3]); // Point2D(50, 25)
// Get rotated quadrant points
const rotatedQuadPoints = circle.getQuadPoints(Math.PI / 4); // Rotate 45°Geometric Transformations
All geometric transformation methods support the PointInput type parameter, accepting either [x, y] arrays or Point2D objects.
move() - Move
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt([50, 50], 25);
// Shorthand syntax (recommended)
circle.move([0, 0], [100, 100]);
console.log('New center:', circle.center); // Point2D(150, 150)
// Radius unchanged
console.log('Radius:', circle.radius); // 25rotate() - Rotate
When a circle is rotated around a specified point, only the center position changes; the shape remains the same.
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt([100, 0], 25);
// Shorthand syntax (recommended)
circle.rotate([0, 0], Math.PI / 2); // Rotate 90° around the origin
console.log('New center:', circle.center); // Point2D(0, 100)
console.log('Radius:', circle.radius); // 25 (unchanged)scale() - Scale
Scaling changes both the center position (relative to the scale center) and the radius.
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt([100, 100], 25);
// Shorthand syntax (recommended)
circle.scale([0, 0], 2); // Scale up 2x from the origin
console.log('New center:', circle.center); // Point2D(200, 200)
console.log('New radius:', circle.radius); // 50
// Scale from the circle's own center
const circle2 = new CircleEnt([50, 50], 25);
circle2.scale(circle2.center, 2);
console.log('Center:', circle2.center); // Point2D(50, 50) (unchanged)
console.log('Radius:', circle2.radius); // 50mirror() - Mirror
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt([100, 50], 25);
// Shorthand syntax (recommended)
circle.mirror([0, 0], [0, 100]); // Mirror along Y-axis (X=0)
console.log('New center:', circle.center); // Point2D(-100, 50)
console.log('Radius:', circle.radius); // 25 (unchanged)Grip Editing
The gripEdit() method handles grip-drag editing.
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Grip types: "center" | "quad"
// Move the center
circle.gripEdit(new Point2D(100, 100), "center");
console.log('New center:', circle.center); // Point2D(100, 100)
// Adjust radius via quadrant point
circle.gripEdit(new Point2D(150, 100), "quad");
// New radius = distance from center to grip point
console.log('New radius:', circle.radius); // 50Bounding Box
import { CircleEnt, Point2D, CoordinateSystemType } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
// Get WCS bounding box
const bbox = circle.boundingBox();
console.log('Min point:', bbox.pt1); // Point2D(25, 25)
console.log('Max point:', bbox.pt2); // Point2D(75, 75)
// Bounding box is a square with side length = diameter
console.log('Width:', bbox.width); // 50
console.log('Height:', bbox.height); // 50Clone
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
circle.setDefaults();
circle.color = 1;
// Create a deep copy
const circleCopy = circle.clone();
// Modify the copy
circleCopy.radius = 50;
circleCopy.color = 3;
console.log(circle.radius); // 25
console.log(circleCopy.radius); // 50Serialization
import { CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 25);
circle.setDefaults();
// Export
const dbData = circle.toDb();
console.log(dbData);
// {
// type: "CIRCLE",
// layerId: "0",
// color: 256,
// center: [50, 50], // Coordinate format: [x, y] or [x, y, z]
// radius: 25
// }
// Import
const newCircle = new CircleEnt(new Point2D(0, 0), 1);
newCircle.fromDb(dbData);Complete Examples
Create Circles by Geometric Properties
import { Engine, CircleEnt, Point2D } from 'vjcad';
// Create a circle by area
function createCircleByArea(center: Point2D, area: number): CircleEnt {
const circle = new CircleEnt(center, 1); // Create with default radius first
circle.setArea(area); // Then set the target area
circle.setDefaults();
return circle;
}
// Create a circle by circumference
function createCircleByCircumference(center: Point2D, circumference: number): CircleEnt {
const circle = new CircleEnt(center, 1);
circle.setCircumference(circumference);
circle.setDefaults();
return circle;
}
// Create a circle by diameter
function createCircleByDiameter(center: Point2D, diameter: number): CircleEnt {
const circle = new CircleEnt(center, 1);
circle.setDiameter(diameter);
circle.setDefaults();
return circle;
}
// Usage example
const c1 = createCircleByArea(new Point2D(0, 0), 1000);
const c2 = createCircleByCircumference(new Point2D(100, 0), 200);
const c3 = createCircleByDiameter(new Point2D(200, 0), 50);
Engine.addEntities([c1, c2, c3]);
// Output circle information
console.log('Area circle:', c1.radius.toFixed(2), 'radius,', c1.area.toFixed(2), 'area');
console.log('Circumference circle:', c2.radius.toFixed(2), 'radius,', c2.circumference.toFixed(2), 'circumference');
console.log('Diameter circle:', c3.radius.toFixed(2), 'radius,', c3.getDiameter().toFixed(2), 'diameter');Draw Concentric Circles
import { Engine, CircleEnt, Point2D } from 'vjcad';
function drawConcentricCircles(center: Point2D, radii: number[]) {
const circles: CircleEnt[] = [];
for (const radius of radii) {
const circle = new CircleEnt(center.clone(), radius);
circle.setDefaults();
circles.push(circle);
}
Engine.addEntities(circles);
}
// Draw concentric circles
drawConcentricCircles(new Point2D(100, 100), [20, 40, 60, 80, 100]);Draw Circular Array
import { Engine, CircleEnt, Point2D } from 'vjcad';
function drawCircleArray(
center: Point2D,
radius: number,
count: number,
distance: number
) {
const circles: CircleEnt[] = [];
const angleStep = (2 * Math.PI) / count;
for (let i = 0; i < count; i++) {
const angle = i * angleStep;
const circleCenter = new Point2D(
center.x + distance * Math.cos(angle),
center.y + distance * Math.sin(angle)
);
const circle = new CircleEnt(circleCenter, radius);
circle.setDefaults();
circles.push(circle);
}
Engine.addEntities(circles);
}
// Draw a circular array of 8 circles
drawCircleArray(new Point2D(200, 200), 15, 8, 80);Interactive Circle Drawing
import {
Engine,
CircleEnt,
Point2D,
PointInputOptions,
RealInputOptions,
InputStatusEnum
} from 'vjcad';
async function drawCircleCommand() {
// Get the center point
const opt1 = new PointInputOptions("Specify center point:");
const result1 = await Engine.getPoint(opt1);
if (result1.status !== InputStatusEnum.OK) return;
const center = result1.value as Point2D;
// Get the radius
const opt2 = new RealInputOptions("Specify radius:");
opt2.defaultValue = 25;
const result2 = await Engine.getReal(opt2);
if (result2.status !== InputStatusEnum.OK) return;
const radius = result2.value as number;
// Create the circle
const circle = new CircleEnt(center, radius);
circle.setDefaults();
Engine.addEntities(circle);
Engine.writeMessage(`Circle created, area: ${circle.area.toFixed(2)}`);
}Draw Circle Through Three Points
import { Engine, CircleEnt, Point2D } from 'vjcad';
function circleFrom3Points(p1: Point2D, p2: Point2D, p3: Point2D): CircleEnt | null {
// Calculate the circumscribed circle
const ax = p1.x, ay = p1.y;
const bx = p2.x, by = p2.y;
const cx = p3.x, cy = p3.y;
const d = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by));
if (Math.abs(d) < 1e-10) {
// Three points are collinear, cannot create a circle
return null;
}
const ux = ((ax * ax + ay * ay) * (by - cy) +
(bx * bx + by * by) * (cy - ay) +
(cx * cx + cy * cy) * (ay - by)) / d;
const uy = ((ax * ax + ay * ay) * (cx - bx) +
(bx * bx + by * by) * (ax - cx) +
(cx * cx + cy * cy) * (bx - ax)) / d;
const center = new Point2D(ux, uy);
const radius = Math.sqrt((ax - ux) * (ax - ux) + (ay - uy) * (ay - uy));
const circle = new CircleEnt(center, radius);
circle.setDefaults();
return circle;
}
// Usage example
const circle = circleFrom3Points(
new Point2D(0, 0),
new Point2D(100, 0),
new Point2D(50, 50)
);
if (circle) {
Engine.addEntities(circle);
}FAQ
Q: How to check if a point is inside a circle?
import { CircleEnt, Point2D } from 'vjcad';
function isPointInCircle(circle: CircleEnt, point: Point2D): boolean {
const dx = point.x - circle.center.x;
const dy = point.y - circle.center.y;
const distanceSquared = dx * dx + dy * dy;
return distanceSquared <= circle.radius * circle.radius;
}
function isPointOnCircle(circle: CircleEnt, point: Point2D, tolerance: number = 0.001): boolean {
const dx = point.x - circle.center.x;
const dy = point.y - circle.center.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return Math.abs(distance - circle.radius) < tolerance;
}Q: How to get a point on the circle at a specified angle?
import { CircleEnt, Point2D } from 'vjcad';
function getPointOnCircle(circle: CircleEnt, angle: number): Point2D {
return new Point2D(
circle.center.x + circle.radius * Math.cos(angle),
circle.center.y + circle.radius * Math.sin(angle)
);
}
// Get the point at 45°
const circle = new CircleEnt(new Point2D(50, 50), 25);
const point = getPointOnCircle(circle, Math.PI / 4);Q: How to calculate the intersection points of two circles?
import { CircleEnt, Point2D } from 'vjcad';
function circleCircleIntersection(c1: CircleEnt, c2: CircleEnt): Point2D[] {
const dx = c2.center.x - c1.center.x;
const dy = c2.center.y - c1.center.y;
const d = Math.sqrt(dx * dx + dy * dy);
// Check if they intersect
if (d > c1.radius + c2.radius || d < Math.abs(c1.radius - c2.radius)) {
return []; // No intersection
}
const a = (c1.radius * c1.radius - c2.radius * c2.radius + d * d) / (2 * d);
const h = Math.sqrt(c1.radius * c1.radius - a * a);
const px = c1.center.x + a * dx / d;
const py = c1.center.y + a * dy / d;
const p1 = new Point2D(px + h * dy / d, py - h * dx / d);
const p2 = new Point2D(px - h * dy / d, py + h * dx / d);
if (Math.abs(h) < 1e-10) {
return [p1]; // Tangent, only one intersection point
}
return [p1, p2];
}Next Steps
- Arc Entity (ArcEnt) - Detailed guide on arc entities
- Ellipse Entity (EllipseEnt) - Detailed guide on ellipse entities
- Polyline Entity (PolylineEnt) - Detailed guide on polyline entities