@@ -34,6 +34,26 @@ pub struct Circle {
34
34
}
35
35
impl Primitive2d for Circle { }
36
36
37
+ /// An ellipse primitive
38
+ #[ derive( Clone , Copy , Debug ) ]
39
+ pub struct Ellipse {
40
+ /// The half "width" of the ellipse
41
+ pub half_width : f32 ,
42
+ /// The half "height" of the ellipse
43
+ pub half_height : f32 ,
44
+ }
45
+ impl Primitive2d for Ellipse { }
46
+
47
+ impl Ellipse {
48
+ /// Create a new `Ellipse` from a "width" and a "height"
49
+ pub fn new ( width : f32 , height : f32 ) -> Self {
50
+ Self {
51
+ half_width : width / 2.0 ,
52
+ half_height : height / 2.0 ,
53
+ }
54
+ }
55
+ }
56
+
37
57
/// An unbounded plane in 2D space. It forms a separating surface through the origin,
38
58
/// stretching infinitely far
39
59
#[ derive( Clone , Copy , Debug ) ]
@@ -108,6 +128,24 @@ pub struct Polyline2d<const N: usize> {
108
128
}
109
129
impl < const N : usize > Primitive2d for Polyline2d < N > { }
110
130
131
+ impl < const N : usize > FromIterator < Vec2 > for Polyline2d < N > {
132
+ fn from_iter < I : IntoIterator < Item = Vec2 > > ( iter : I ) -> Self {
133
+ let mut vertices: [ Vec2 ; N ] = [ Vec2 :: ZERO ; N ] ;
134
+
135
+ for ( index, i) in iter. into_iter ( ) . take ( N ) . enumerate ( ) {
136
+ vertices[ index] = i;
137
+ }
138
+ Self { vertices }
139
+ }
140
+ }
141
+
142
+ impl < const N : usize > Polyline2d < N > {
143
+ /// Create a new `Polyline2d` from its vertices
144
+ pub fn new ( vertices : impl IntoIterator < Item = Vec2 > ) -> Self {
145
+ Self :: from_iter ( vertices)
146
+ }
147
+ }
148
+
111
149
/// A series of connected line segments in 2D space, allocated on the heap
112
150
/// in a `Box<[Vec2]>`.
113
151
///
@@ -119,6 +157,22 @@ pub struct BoxedPolyline2d {
119
157
}
120
158
impl Primitive2d for BoxedPolyline2d { }
121
159
160
+ impl FromIterator < Vec2 > for BoxedPolyline2d {
161
+ fn from_iter < I : IntoIterator < Item = Vec2 > > ( iter : I ) -> Self {
162
+ let vertices: Vec < Vec2 > = iter. into_iter ( ) . collect ( ) ;
163
+ Self {
164
+ vertices : vertices. into_boxed_slice ( ) ,
165
+ }
166
+ }
167
+ }
168
+
169
+ impl BoxedPolyline2d {
170
+ /// Create a new `BoxedPolyline2d` from its vertices
171
+ pub fn new ( vertices : impl IntoIterator < Item = Vec2 > ) -> Self {
172
+ Self :: from_iter ( vertices)
173
+ }
174
+ }
175
+
122
176
/// A triangle in 2D space
123
177
#[ derive( Clone , Debug ) ]
124
178
pub struct Triangle2d {
@@ -127,6 +181,15 @@ pub struct Triangle2d {
127
181
}
128
182
impl Primitive2d for Triangle2d { }
129
183
184
+ impl Triangle2d {
185
+ /// Create a new `Triangle2d` from `a`, `b`, and `c`,
186
+ pub fn new ( a : Vec2 , b : Vec2 , c : Vec2 ) -> Self {
187
+ Self {
188
+ vertices : [ a, b, c] ,
189
+ }
190
+ }
191
+ }
192
+
130
193
/// A rectangle primitive
131
194
#[ doc( alias = "Quad" ) ]
132
195
#[ derive( Clone , Copy , Debug ) ]
@@ -158,22 +221,56 @@ impl Rectangle {
158
221
/// For a version without generics: [`BoxedPolygon`]
159
222
#[ derive( Clone , Debug ) ]
160
223
pub struct Polygon < const N : usize > {
161
- /// The vertices of the polygon
224
+ /// The vertices of the `Polygon`
162
225
pub vertices : [ Vec2 ; N ] ,
163
226
}
164
227
impl < const N : usize > Primitive2d for Polygon < N > { }
165
228
229
+ impl < const N : usize > FromIterator < Vec2 > for Polygon < N > {
230
+ fn from_iter < I : IntoIterator < Item = Vec2 > > ( iter : I ) -> Self {
231
+ let mut vertices: [ Vec2 ; N ] = [ Vec2 :: ZERO ; N ] ;
232
+
233
+ for ( index, i) in iter. into_iter ( ) . take ( N ) . enumerate ( ) {
234
+ vertices[ index] = i;
235
+ }
236
+ Self { vertices }
237
+ }
238
+ }
239
+
240
+ impl < const N : usize > Polygon < N > {
241
+ /// Create a new `Polygon` from its vertices
242
+ pub fn new ( vertices : impl IntoIterator < Item = Vec2 > ) -> Self {
243
+ Self :: from_iter ( vertices)
244
+ }
245
+ }
246
+
166
247
/// A polygon with a variable number of vertices, allocated on the heap
167
248
/// in a `Box<[Vec2]>`.
168
249
///
169
250
/// For a version without alloc: [`Polygon`]
170
251
#[ derive( Clone , Debug ) ]
171
252
pub struct BoxedPolygon {
172
- /// The vertices of the polygon
253
+ /// The vertices of the `BoxedPolygon`
173
254
pub vertices : Box < [ Vec2 ] > ,
174
255
}
175
256
impl Primitive2d for BoxedPolygon { }
176
257
258
+ impl FromIterator < Vec2 > for BoxedPolygon {
259
+ fn from_iter < I : IntoIterator < Item = Vec2 > > ( iter : I ) -> Self {
260
+ let vertices: Vec < Vec2 > = iter. into_iter ( ) . collect ( ) ;
261
+ Self {
262
+ vertices : vertices. into_boxed_slice ( ) ,
263
+ }
264
+ }
265
+ }
266
+
267
+ impl BoxedPolygon {
268
+ /// Create a new `BoxedPolygon` from its vertices
269
+ pub fn new ( vertices : impl IntoIterator < Item = Vec2 > ) -> Self {
270
+ Self :: from_iter ( vertices)
271
+ }
272
+ }
273
+
177
274
/// A polygon where all vertices lie on a circle, equally far apart
178
275
#[ derive( Clone , Copy , Debug ) ]
179
276
pub struct RegularPolygon {
@@ -183,3 +280,21 @@ pub struct RegularPolygon {
183
280
pub sides : usize ,
184
281
}
185
282
impl Primitive2d for RegularPolygon { }
283
+
284
+ impl RegularPolygon {
285
+ /// Create a new `RegularPolygon`
286
+ /// from the radius of the circumcircle and number of sides
287
+ ///
288
+ /// # Panics
289
+ ///
290
+ /// Panics if `circumcircle_radius` is non-positive
291
+ pub fn new ( circumcircle_radius : f32 , sides : usize ) -> Self {
292
+ assert ! ( circumcircle_radius > 0.0 ) ;
293
+ Self {
294
+ circumcircle : Circle {
295
+ radius : circumcircle_radius,
296
+ } ,
297
+ sides,
298
+ }
299
+ }
300
+ }
0 commit comments