#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]
use use_point::Point2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProjectionKind {
Orthographic,
Perspective,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Projection2 {
kind: ProjectionKind,
scale: f64,
}
impl Projection2 {
#[must_use]
pub const fn new(kind: ProjectionKind, scale: f64) -> Option<Self> {
if scale.is_finite() && scale > 0.0 {
Some(Self { kind, scale })
} else {
None
}
}
#[must_use]
pub const fn kind(self) -> ProjectionKind {
self.kind
}
#[must_use]
pub const fn scale(self) -> f64 {
self.scale
}
#[must_use]
pub fn project_x_axis(self, point: Point2) -> f64 {
point.x() * self.scale
}
}
#[cfg(test)]
mod tests {
use super::{Projection2, ProjectionKind};
use use_point::Point2;
#[test]
fn projects_points_to_axis() {
let projection = Projection2::new(ProjectionKind::Orthographic, 2.0).expect("valid");
assert_eq!(projection.kind(), ProjectionKind::Orthographic);
assert_eq!(projection.scale(), 2.0);
assert_eq!(projection.project_x_axis(Point2::new(3.0, 4.0)), 6.0);
assert_eq!(Projection2::new(ProjectionKind::Perspective, 0.0), None);
}
}