restructure vertices to use x and y fields instead of being tuple

structs
This commit is contained in:
2026-01-12 11:07:24 +11:00
parent 7efb97fa5a
commit 81f2a0591b
4 changed files with 80 additions and 45 deletions
+21 -15
View File
@@ -1,7 +1,11 @@
use crate::consts::*;
#[derive(Debug, Clone, Copy)]
pub struct Point2D(pub f64, pub f64);
pub struct Point2D {
pub x: f64,
pub y: f64
}
impl Point2D {
pub fn to_canvas_coordinates(&self) -> Point3D {
todo!();
@@ -12,19 +16,21 @@ impl Point2D {
// Pythagorean formula
// TODO: use hypot function
f64::sqrt(
(f64::powf(p2.0.max(p1.0) - p1.0.min(p2.0), 2.0)
+ f64::powf(p2.1.max(p1.1) - p1.1.min(p2.1), 2.0)) as f64,
(f64::powf(p2.x.max(p1.x) - p1.x.min(p2.x), 2.0)
+ f64::powf(p2.y.max(p1.y) - p1.y.min(p2.y), 2.0)) as f64,
)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Point3D(pub f64, pub f64, pub f64);
pub struct Point3D {
pub x: f64, pub y: f64, pub z: f64
}
impl Point3D {
pub fn to_screen_coordinates(&self) -> Point2D {
let Point3D(x, y, z) = *self;
let Point3D {x, y, z} = *self;
if self.2 == 0.0 {
if z == 0.0 {
// Normalize to the interval [0, 1]
let x = (x / f64::MAX + 1.0) / 2.0;
let y = (-y / f64::MAX + 1.0) / 2.0;
@@ -32,10 +38,10 @@ impl Point3D {
assert!(x <= 1.0 && y <= 1.0 && z <= 1.0);
Point2D(
x * (SCREEN_WIDTH - 1) as f64, // Scale to the interval [0, SCREEN_WIDTH)
y * (SCREEN_HEIGHT - 1) as f64, // Scale to the interval [0, SCREEN_HEIGHT)
)
Point2D {
x: x * (SCREEN_WIDTH - 1) as f64, // Scale to the interval [0, SCREEN_WIDTH)
y: y * (SCREEN_HEIGHT - 1) as f64, // Scale to the interval [0, SCREEN_HEIGHT)
}
} else {
let x = x / z;
let y = y / z;
@@ -47,10 +53,10 @@ impl Point3D {
assert!(x <= 1.0 && y <= 1.0 && z <= 1.0);
Point2D(
x * (SCREEN_WIDTH - 1) as f64,
y * (SCREEN_HEIGHT - 1) as f64,
)
Point2D {
x: x * (SCREEN_WIDTH - 1) as f64,
y: y * (SCREEN_HEIGHT - 1) as f64,
}
}
}
@@ -58,7 +64,7 @@ impl Point3D {
let p1 = self;
// Pythagorean formula
f64::sqrt(
f64::powf(p2.0 - p1.0, 2.0) + f64::powf(p2.1 - p1.1, 2.0) + f64::powf(p2.2 - p1.2, 2.0) // Fix to use max and min
f64::powf(p2.x - p1.x, 2.0) + f64::powf(p2.y - p1.y, 2.0) + f64::powf(p2.z - p1.z, 2.0) // Fix to use max and min
)
}
}