lj_rust/src/point.rs

93 lines
2.0 KiB
Rust
Raw Permalink Normal View History

2023-07-16 18:28:06 +00:00
use ether_dream::protocol::DacPoint;
2023-07-09 20:16:01 +00:00
#[derive(Debug, Clone, Copy, Default, PartialEq)]
2023-06-04 19:31:32 +00:00
pub struct Point {
2023-08-23 11:13:53 +00:00
pub x: f32,
pub y: f32,
pub color: Color,
2023-06-04 19:31:32 +00:00
}
2023-07-09 20:16:01 +00:00
#[derive(Debug, Clone, Copy, Default, PartialEq)]
2023-06-04 19:31:32 +00:00
pub struct Color {
2023-08-23 11:13:53 +00:00
pub r: u8,
pub g: u8,
pub b: u8,
2023-06-04 19:31:32 +00:00
}
2023-08-23 11:13:53 +00:00
impl Point {
pub fn diff(p1: &Self, p2: &Self) -> Self {
Point {
x: p1.x - p2.x,
y: p1.y - p2.y,
color: Color { r: 0, g: 0, b: 0 },
}
}
pub fn normalize(&mut self) -> Self {
let length = (self.x * self.x + self.y * self.y).sqrt();
self.x = self.x / length;
self.y = self.y / length;
*self
}
pub fn cross(&self, pt: &Self) -> f32 {
self.x * pt.x + self.y * pt.y
}
}
// normalaize
// diff
2023-07-09 20:16:01 +00:00
impl From<Color> for u32 {
2023-08-23 11:13:53 +00:00
fn from(value: Color) -> Self {
let r = value.r as u32;
let g = value.g as u32;
let b = (value.b) as u32;
(r << 16) + (g << 8) + b
}
2023-07-09 20:16:01 +00:00
}
impl From<(f32, f32, u32)> for Point {
2023-08-23 11:13:53 +00:00
fn from((x, y, color): (f32, f32, u32)) -> Point {
let r = (color >> 16) as u8;
let g = ((color >> 8) & 255) as u8;
let b = (color & 255) as u8;
Point {
x,
y,
color: Color { r, g, b },
}
}
2023-06-04 19:31:32 +00:00
}
impl From<Point> for helios_dac::Point {
2023-08-23 11:13:53 +00:00
fn from(pt: Point) -> helios_dac::Point {
let x = pt.x.clamp(0.0, 4095.0) as u16;
let y = pt.y.clamp(0.0, 4095.0) as u16;
helios_dac::Point {
coordinate: (x, y).into(),
color: helios_dac::Color::new(pt.color.r, pt.color.g, pt.color.b),
intensity: 0xFF,
}
}
2023-06-04 19:31:32 +00:00
}
2023-07-16 18:28:06 +00:00
impl From<Point> for DacPoint {
2023-08-23 11:13:53 +00:00
fn from(pt: Point) -> DacPoint {
let control = 0;
let (u1, u2) = (0, 0);
let i = 255;
DacPoint {
control,
x: pt.x as i16,
y: pt.y as i16,
i,
r: pt.color.r.into(),
g: pt.color.g.into(),
b: pt.color.b.into(),
u1,
u2,
}
}
2023-07-16 18:28:06 +00:00
}