use std::f64::consts::{PI, TAU};
use crate::trig::Trig;
pub const ASPECT: f64 = 2.0;
pub fn polar(cx: f64, cy: f64, r: f64, theta: f64) -> (i32, i32) {
let x = cx + r * theta.cos_det() * ASPECT;
let y = cy + r * theta.sin_det();
(x.round() as i32, y.round() as i32)
}
pub fn wrap(v: f64, max: f64) -> f64 {
let w = v.rem_euclid(max);
if w >= max { 0.0 } else { w }
}
#[inline]
pub fn wrap_angle(a: f64) -> f64 {
wrap(a + PI, TAU) - PI
}
pub mod v3 {
pub type V3 = [f64; 3];
#[inline]
pub fn add(a: V3, b: V3) -> V3 {
[a[0] + b[0], a[1] + b[1], a[2] + b[2]]
}
#[inline]
pub fn sub(a: V3, b: V3) -> V3 {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
#[inline]
pub fn scale(a: V3, k: f64) -> V3 {
[a[0] * k, a[1] * k, a[2] * k]
}
#[inline]
pub fn axpy(k: f64, a: V3, b: V3) -> V3 {
[k * a[0] + b[0], k * a[1] + b[1], k * a[2] + b[2]]
}
#[inline]
pub fn dot(a: V3, b: V3) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline]
pub fn cross(a: V3, b: V3) -> V3 {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
#[inline]
pub fn norm(a: V3) -> f64 {
dot(a, a).sqrt()
}
#[inline]
pub fn unit(a: V3) -> V3 {
let n = norm(a);
a.map(|c| c / n)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Camera {
pub yaw: f64,
pub pitch: f64,
pub distance: f64,
}
impl Default for Camera {
fn default() -> Self {
Self {
yaw: 0.0,
pitch: 0.0,
distance: f64::INFINITY,
}
}
}
impl Camera {
pub fn project(&self, p: [f64; 3], centre: (f64, f64), scale: f64) -> Option<(f64, f64, f64)> {
self.projector().project(p, centre, scale)
}
pub fn projector(&self) -> Projector {
let (sy, cy) = self.yaw.sin_cos_det();
let (sp, cp) = self.pitch.sin_cos_det();
Projector {
sy,
cy,
sp,
cp,
distance: self.distance,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Projector {
sy: f64,
cy: f64,
sp: f64,
cp: f64,
distance: f64,
}
impl Projector {
#[inline]
pub fn rotate(&self, p: [f64; 3]) -> [f64; 3] {
let [x, y, z] = p;
let (x1, z1) = (x * self.cy + z * self.sy, z * self.cy - x * self.sy);
let (y2, z2) = (y * self.cp - z1 * self.sp, y * self.sp + z1 * self.cp);
[x1, y2, z2]
}
#[inline]
pub fn magnification(&self, depth: f64) -> f64 {
if self.distance.is_finite() {
self.distance / (self.distance - depth)
} else {
1.0
}
}
#[inline]
pub fn project(&self, p: [f64; 3], centre: (f64, f64), scale: f64) -> Option<(f64, f64, f64)> {
let [x1, y2, z2] = self.rotate(p);
let in_front = z2 < self.distance - 1e-9;
if !in_front {
return None;
}
let k = if self.distance.is_finite() {
scale * self.distance / (self.distance - z2)
} else {
scale
};
let out = (centre.0 + x1 * k * ASPECT, centre.1 - y2 * k, z2);
(out.0.is_finite() && out.1.is_finite() && out.2.is_finite()).then_some(out)
}
}
pub fn line(x0: i32, y0: i32, x1: i32, y1: i32) -> impl Iterator<Item = (i32, i32)> {
let dx = (x1 - x0).abs();
let dy = -(y1 - y0).abs();
let sx = if x0 < x1 { 1 } else { -1 };
let sy = if y0 < y1 { 1 } else { -1 };
let (mut x, mut y, mut err, mut done) = (x0, y0, dx + dy, false);
std::iter::from_fn(move || {
if done {
return None;
}
let p = (x, y);
if x == x1 && y == y1 {
done = true;
} else {
let e2 = 2 * err;
if e2 >= dy {
err += dy;
x += sx;
}
if e2 <= dx {
err += dx;
y += sy;
}
}
Some(p)
})
}
pub fn stroked_line(
x0: i32,
y0: i32,
x1: i32,
y1: i32,
mut f: impl FnMut(i32, i32, i32, (i32, i32)),
) {
let mut cells = line(x0, y0, x1, y1).peekable();
let mut prev: Option<(i32, i32)> = None;
let mut k = 0;
while let Some((x, y)) = cells.next() {
let step = match (prev, cells.peek()) {
(Some((px, py)), _) => (x - px, y - py),
(None, Some(&(nx, ny))) => (nx - x, ny - y),
(None, None) => (0, 1),
};
f(k, x, y, step);
prev = Some((x, y));
k += 1;
}
}