rust_3d/
factory_2d.rs

1/*
2Copyright 2016 Martin Buck
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"),
6to deal in the Software without restriction, including without limitation the
7rights to use, copy, modify, merge, publish, distribute, sublicense,
8and/or sell copies of the Software, and to permit persons to whom the Software
9is furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall
12be included all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21*/
22
23//! functions used for the creation of 2D shapes and geometries
24
25use std::f64::consts::PI;
26
27use crate::*;
28
29//------------------------------------------------------------------------------
30
31/// Creates a 2D rectangle from given center width and height
32pub 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
46/// Creates a involut circle with the given center, diameter, resolution and start and end angles in radians
47pub 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
71/// Creates an arc with the given center, diameter, resolution and start and end angles in radians
72pub 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
96/// Creates an ellipse with the given center, a, b and resolution
97pub 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}