This document provides comprehensive documentation for the Geo-Playground API, focusing on the shape services and utilities that form the core of the application.
The application supports the following shape types:
interface Circle extends Shape {
type: 'circle';
radius: number;
center: Point;
}A circle is defined by its center point and radius.
interface Rectangle extends Shape {
type: 'rectangle';
width: number;
height: number;
position: Point;
rotation: number;
}A rectangle is defined by its position (top-left corner), width, height, and rotation angle.
interface Triangle extends Shape {
type: 'triangle';
points: [Point, Point, Point];
rotation: number;
}A triangle is defined by its three points and rotation angle.
interface Line extends Shape {
type: 'line';
start: Point;
end: Point;
}A line is defined by its start and end points.
The application uses a service-based architecture for managing shapes. Each shape type has its own service implementation.
interface ShapeService<T extends Shape> {
create(params: ShapeCreationParams): T;
update(shape: T, updates: Partial<T>): T;
move(shape: T, dx: number, dy: number): T;
resize(shape: T, scale: number, anchor?: Point): T;
rotate(shape: T, angle: number, center?: Point): T;
getArea(shape: T): number;
getPerimeter(shape: T): number;
containsPoint(shape: T, point: Point): boolean;
}The ShapeServiceFactory provides a way to get the appropriate service for a given shape type:
const shapeService = ShapeServiceFactory.getService(shape.type);The CircleService provides operations specific to circles:
// Create a circle
const circle = circleService.create({ center: { x: 100, y: 100 }, radius: 50 });
// Resize a circle
const resizedCircle = circleService.resize(circle, 1.5);
// Get the area of a circle
const area = circleService.getArea(circle);The RectangleService provides operations specific to rectangles:
// Create a rectangle
const rectangle = rectangleService.create({
position: { x: 100, y: 100 },
width: 200,
height: 100
});
// Rotate a rectangle
const rotatedRectangle = rectangleService.rotate(rectangle, 45);
// Check if a point is inside a rectangle
const isInside = rectangleService.containsPoint(rectangle, { x: 150, y: 150 });The TriangleService provides operations specific to triangles:
// Create a triangle
const triangle = triangleService.create({
points: [
{ x: 100, y: 100 },
{ x: 200, y: 100 },
{ x: 150, y: 50 }
]
});
// Get the perimeter of a triangle
const perimeter = triangleService.getPerimeter(triangle);The LineService provides operations specific to lines:
// Create a line
const line = lineService.create({
start: { x: 100, y: 100 },
end: { x: 200, y: 200 }
});
// Get the length of a line
const length = lineService.getLength(line);The application includes several utility modules for working with shapes:
The shapeCreation.ts module provides functions for creating different types of shapes:
// Create a circle
const circle = createCircle({ center: { x: 100, y: 100 }, radius: 50 });
// Create a rectangle
const rectangle = createRectangle({
position: { x: 100, y: 100 },
width: 200,
height: 100
});The shapeOperations.ts module provides functions for common shape operations:
// Select a shape
const selectedShapes = selectShape(shapes, shape.id);
// Move a shape
const movedShape = moveShape(shape, 10, 20);The shapeUpdates.ts module provides functions for updating shape properties:
// Update a shape's properties
const updatedShape = updateShape(shape, { fill: 'red', stroke: 'black' });The measurements.ts module provides functions for calculating shape measurements:
// Calculate the area of a shape
const area = calculateArea(shape);
// Calculate the perimeter of a shape
const perimeter = calculatePerimeter(shape);The rotation.ts module provides functions for rotating shapes:
// Rotate a point around a center
const rotatedPoint = rotatePoint(point, center, angle);
// Rotate a shape
const rotatedShape = rotateShape(shape, angle);To create a shape, use the appropriate service:
const circleService = ShapeServiceFactory.getService('circle');
const circle = circleService.create({ center: { x: 100, y: 100 }, radius: 50 });To move a shape, use the move method of the appropriate service:
const movedShape = shapeService.move(shape, dx, dy);To resize a shape, use the resize method of the appropriate service:
const resizedShape = shapeService.resize(shape, scale, anchor);To rotate a shape, use the rotate method of the appropriate service:
const rotatedShape = shapeService.rotate(shape, angle, center);The shape services and utilities include error handling for common issues:
- Invalid shape parameters (e.g., negative radius)
- Missing required properties
- Invalid operations (e.g., rotating a circle around an external point)
Errors are thrown with descriptive messages to help identify the issue.
Example:
try {
const circle = circleService.create({ center: { x: 100, y: 100 }, radius: -50 });
} catch (error) {
console.error(error.message); // "Radius must be a positive number"
}