Skip to main content

rscad_csg_bsp/primitives/
square.rs

1use crate::*;
2use nalgebra::Point;
3
4/// Create a square (uniform rectangle) centered at the origin in the XZ plane.
5pub fn square<T: Number>(size: T) -> Csg<T> {
6    rect(size, size)
7}
8
9/// Create a rectangle centered at the origin in the XZ plane.
10///
11/// `x` is the width along the X axis, `z` is the depth along the Z axis.
12pub fn rect<T: Number>(x: T, z: T) -> Csg<T> {
13    let two = T::one() + T::one();
14    let hx = x / two;
15    let hz = z / two;
16
17    Csg::from_polygons([Polygon::from_points(vec![
18        Point::from([-hx, T::zero(), -hz]),
19        Point::from([hx, T::zero(), -hz]),
20        Point::from([hx, T::zero(), hz]),
21        Point::from([-hx, T::zero(), hz]),
22    ])
23    .expect("Impossible")])
24}