Skip to content

Commit

Permalink
Create macro for generating points (#60)
Browse files Browse the repository at this point in the history
* Create point! macro

* Fixed formating
  • Loading branch information
BradenEverson committed Feb 23, 2024
1 parent 6954d33 commit c9b6f59
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@ pub struct Point {
pub z: f64,
}

#[macro_export]
///Generates a point in the xyz plane based on the following pattern:
///```
///use dxf::point;
///use dxf::Point;
/////Generate a point at (1,2,3)
///let p1 = point!(1.0,2.0,3.0);
/////Generate a point at (1,2,0)
///let p2 = point!(1.0,2.0);
/////Generate a point at (1,0,0)
///let p3 = point!(1.0);
///```
macro_rules! point {
($x:expr, $y:expr, $z:expr) => {
Point {
x: $x as f64,
y: $y as f64,
z: $z as f64,
}
};
($x:expr, $y:expr) => {
Point {
x: $x as f64,
y: $y as f64,
z: 0.0,
}
};
($x:expr) => {
Point {
x: $x as f64,
y: 0.0,
z: 0.0,
}
};
}

impl Point {
/// Creates a new `Point` with the specified values.
pub fn new(x: f64, y: f64, z: f64) -> Point {
Expand Down Expand Up @@ -56,4 +92,11 @@ mod tests {
dbg!(&t);
assert_eq!(t, p.tuple())
}
#[test]
fn test_point_macro() {
let p = point!(1, 2, 3.5);
assert_eq!(p.x, 1.0);
assert_eq!(p.y, 2.0);
assert_eq!(p.z, 3.5);
}
}

0 comments on commit c9b6f59

Please sign in to comment.