From a694d5c54bef87c29e5938ef5b1bc9ba71fff757 Mon Sep 17 00:00:00 2001 From: Adam Chalmers Date: Tue, 24 Oct 2023 12:37:50 -0700 Subject: [PATCH] Convert between 2D points and 2-arrays --- kittycad/src/methods.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/kittycad/src/methods.rs b/kittycad/src/methods.rs index c256bbc6..ea20cf32 100644 --- a/kittycad/src/methods.rs +++ b/kittycad/src/methods.rs @@ -42,6 +42,25 @@ impl Angle { } } +impl From<[f64; 2]> for Point2D { + fn from([x, y]: [f64; 2]) -> Self { + Self { x, y } + } +} + +impl From<&[f64; 2]> for Point2D { + fn from(p: &[f64; 2]) -> Self { + let [x, y] = *p; + Self { x, y } + } +} + +impl From for [f64; 2] { + fn from(Point2D { x, y }: Point2D) -> Self { + [x, y] + } +} + impl Add for Point2D { type Output = Self; @@ -129,6 +148,24 @@ impl Div for Point2D { } } +impl From<[f64; 3]> for Point3D { + fn from([x, y, z]: [f64; 3]) -> Self { + Self { x, y, z } + } +} + +impl From<&[f64; 3]> for Point3D { + fn from(p: &[f64; 3]) -> Self { + let [x, y, z] = *p; + Self { x, y, z } + } +} + +impl From for [f64; 3] { + fn from(Point3D { x, y, z }: Point3D) -> Self { + [x, y, z] + } +} impl Add for Point3D { type Output = Self;