1use std::f64::consts::PI;
26
27use crate::*;
28
29pub fn rectangle<P>(center: &P, width: Positive, height: Positive) -> PointCloud2D<P>
33where
34 P: IsBuildable2D,
35{
36 let mut pc = PointCloud2D::with_capacity(4);
37 let w = width.get();
38 let h = height.get();
39 pc.push(P::new(center.x() - w / 2.0, center.y() - h / 2.0));
40 pc.push(P::new(center.x() + w / 2.0, center.y() - h / 2.0));
41 pc.push(P::new(center.x() + w / 2.0, center.y() + h / 2.0));
42 pc.push(P::new(center.x() - w / 2.0, center.y() + h / 2.0));
43 pc
44}
45
46pub fn involut_circle<P>(
48 center: &P,
49 n_points: usize,
50 diameter: Positive,
51 start: Rad,
52 end: Rad,
53) -> PointCloud2D<P>
54where
55 P: IsBuildable2D,
56{
57 let mut pc = PointCloud2D::with_capacity(n_points);
58 let d = diameter.get();
59 let p_dist = (end.val - start.val).abs() / (n_points - 1) as f64;
60
61 for i in 0..n_points {
62 let current = (i as f64) * p_dist;
63 pc.push(P::new(
64 center.x() + d / 2.0 * (current.cos() + current * current.sin()),
65 center.y() + d / 2.0 * (current.sin() - current * current.cos()),
66 ));
67 }
68 pc
69}
70
71pub fn arc<P>(
73 center: &P,
74 n_points: usize,
75 diameter: Positive,
76 start: Rad,
77 end: Rad,
78) -> PointCloud2D<P>
79where
80 P: IsBuildable2D,
81{
82 let mut pc = PointCloud2D::with_capacity(n_points);
83 let d = diameter.get();
84 let p_dist = (end.val - start.val).abs() / (n_points - 1) as f64;
85
86 for i in 0..n_points {
87 let radians = start.val + (i as f64) * p_dist;
88 pc.push(P::new(
89 center.x() + d / 2.0 * radians.cos(),
90 center.y() + d / 2.0 * radians.sin(),
91 ));
92 }
93 pc
94}
95
96pub fn ellipse<P>(center: &P, n_points: usize, ap: Positive, bp: Positive) -> PointCloud2D<P>
98where
99 P: IsBuildable2D,
100{
101 let mut pc = PointCloud2D::with_capacity(n_points);
102 let p_dist = PI / (n_points - 1) as f64;
103 let a = ap.get();
104 let b = bp.get();
105 let angle: f64 = 0.0;
106
107 for i in 0..n_points {
108 let radians = (i as f64) * p_dist;
109 pc.push(P::new(
110 center.x() + a * radians.cos() * angle.cos() - b * radians.sin() * angle.sin(),
111 center.y() + a * radians.cos() * angle.sin() + b * radians.sin() * angle.cos(),
112 ));
113 }
114 pc
115}