Rust · 1720 bytes Raw Blame History
1 //! garcalc-geometry: Dynamic geometry system
2 //!
3 //! Provides geometric primitives, constructions, constraints,
4 //! and measurements for interactive geometry.
5 //!
6 //! This is a stub for Sprint 5 implementation.
7
8 use serde::{Deserialize, Serialize};
9
10 /// A 2D point
11 #[derive(Debug, Clone, Copy, Serialize, Deserialize)]
12 pub struct Point2D {
13 pub x: f64,
14 pub y: f64,
15 }
16
17 /// A unique identifier for shapes
18 pub type ShapeId = u64;
19
20 /// Geometric shapes
21 #[derive(Debug, Clone, Serialize, Deserialize)]
22 pub enum Shape {
23 Point(Point2D),
24 Line {
25 p1: Point2D,
26 p2: Point2D,
27 },
28 Segment {
29 p1: Point2D,
30 p2: Point2D,
31 },
32 Ray {
33 origin: Point2D,
34 direction: Point2D,
35 },
36 Circle {
37 center: Point2D,
38 radius: f64,
39 },
40 Arc {
41 center: Point2D,
42 radius: f64,
43 start_angle: f64,
44 end_angle: f64,
45 },
46 Polygon(Vec<Point2D>),
47 }
48
49 /// Geometric constraints for dynamic geometry
50 #[derive(Debug, Clone, Serialize, Deserialize)]
51 pub enum Constraint {
52 PointOnLine(ShapeId, ShapeId),
53 PointOnCircle(ShapeId, ShapeId),
54 Perpendicular(ShapeId, ShapeId),
55 Parallel(ShapeId, ShapeId),
56 Tangent(ShapeId, ShapeId),
57 Coincident(ShapeId, ShapeId),
58 Fixed(ShapeId),
59 }
60
61 /// Geometry canvas state
62 #[derive(Default)]
63 pub struct GeometryCanvas {
64 pub shapes: Vec<(ShapeId, Shape)>,
65 pub constraints: Vec<Constraint>,
66 next_id: ShapeId,
67 }
68
69 impl GeometryCanvas {
70 pub fn new() -> Self {
71 Self::default()
72 }
73
74 pub fn add_shape(&mut self, shape: Shape) -> ShapeId {
75 let id = self.next_id;
76 self.next_id += 1;
77 self.shapes.push((id, shape));
78 id
79 }
80 }
81