use std::f64::consts::TAU;
use crate::geom::ASPECT;
use crate::trig::Trig;
use crate::{Canvas, Glyph, GlyphMode, Ink, Size};
const MAX_ORBIT_SAMPLES: usize = 20_000;
pub fn orbit(canvas: &mut Canvas, cx: f64, cy: f64, r: f64, glyph: Glyph, ink: Ink) {
if !(cx.is_finite() && cy.is_finite() && r.is_finite()) || r <= 0.0 {
return;
}
let samples = ((TAU * r * ASPECT * 2.0).ceil() as usize).clamp(8, MAX_ORBIT_SAMPLES);
let mut last = None;
for i in 0..samples {
let theta = TAU * i as f64 / samples as f64;
let x = (cx + r * theta.cos_det() * ASPECT).round() as i32;
let y = (cy + r * theta.sin_det()).round() as i32;
if last != Some((x, y)) {
canvas.put(x, y, glyph, ink);
last = Some((x, y));
}
}
}
pub fn disc(
canvas: &mut Canvas,
cx: f64,
cy: f64,
r: f64,
mut shader: impl FnMut(f64, f64, f64) -> Option<(Glyph, Ink)>,
) {
if !(cx.is_finite() && cy.is_finite() && r.is_finite()) || r <= 0.0 {
return;
}
let (w, h) = (
f64::from(canvas.size().width()),
f64::from(canvas.size().height()),
);
if r < 0.5 {
let (x, y) = (cx.round(), cy.round());
if (0.0..w).contains(&x)
&& (0.0..h).contains(&y)
&& let Some((g, ink)) = shader(0.0, 0.0, 0.0)
{
canvas.put(x as i32, y as i32, g, ink);
}
return;
}
let rx = r * ASPECT;
let (x0, x1) = ((cx - rx).floor().max(0.0), (cx + rx).ceil().min(w - 1.0));
let (y0, y1) = ((cy - r).floor().max(0.0), (cy + r).ceil().min(h - 1.0));
if x0 > x1 || y0 > y1 {
return;
}
for y in y0 as i32..=y1 as i32 {
for x in x0 as i32..=x1 as i32 {
let (d, nx, ny) = disc_offset(cx, cy, r, x, y);
if d <= 1.0
&& let Some((g, ink)) = shader(d, nx, ny)
{
canvas.put(x, y, g, ink);
}
}
}
}
#[inline]
fn disc_offset(cx: f64, cy: f64, r: f64, x: i32, y: i32) -> (f64, f64, f64) {
let nx = (f64::from(x) - cx) / (r * ASPECT);
let ny = (f64::from(y) - cy) / r;
((nx * nx + ny * ny).sqrt(), nx, ny)
}
pub fn disc_covers(cx: f64, cy: f64, r: f64, x: i32, y: i32) -> bool {
if !(cx.is_finite() && cy.is_finite() && r.is_finite()) || r <= 0.0 {
return false;
}
if r < 0.5 {
return f64::from(x) == cx.round() && f64::from(y) == cy.round();
}
disc_offset(cx, cy, r, x, y).0 <= 1.0
}
pub fn field(canvas: &mut Canvas, mut shader: impl FnMut(i32, i32) -> Option<(Glyph, Ink)>) {
let (w, h) = (
i32::from(canvas.size().width()),
i32::from(canvas.size().height()),
);
for y in 0..h {
for x in 0..w {
if let Some((g, ink)) = shader(x, y) {
canvas.put(x, y, g, ink);
}
}
}
}
pub fn ramp(v: f64, glyphs: &[Glyph]) -> Glyph {
assert!(!glyphs.is_empty(), "ramp needs at least one glyph");
let v = if v.is_nan() { 0.0 } else { v.clamp(0.0, 1.0) };
glyphs[(v * (glyphs.len() - 1) as f64).round() as usize]
}
pub fn text_centered(canvas: &mut Canvas, y: i32, s: &str, ink: Ink) {
let len = s.chars().count() as i32;
let x = (i32::from(canvas.size().width()) - len) / 2;
canvas.text(x, y, s, ink);
}
pub fn text_mode(canvas: &mut Canvas, x: i32, y: i32, unicode: &str, ascii: &str, ink: Ink) -> i32 {
let s = match canvas.mode() {
GlyphMode::Unicode => unicode,
GlyphMode::Ascii => ascii,
};
canvas.text(x, y, s, ink);
let n = i32::try_from(s.chars().count()).unwrap_or(i32::MAX);
x.saturating_add(n)
}
pub fn label(canvas: &mut Canvas, x: i32, y: i32, s: &str, ink: Ink) -> i32 {
let gap = Glyph::ascii(' ');
let start = x.saturating_add(1);
let end = start.saturating_add(i32::try_from(s.chars().count()).unwrap_or(i32::MAX));
canvas.put(x, y, gap, Ink::BLANK);
canvas.text(start, y, s, ink);
canvas.put(end, y, gap, Ink::BLANK);
end.saturating_add(1)
}
pub fn label_right(canvas: &mut Canvas, y: i32, s: &str, ink: Ink) -> i32 {
let gap = Glyph::ascii(' ');
let last = i32::from(canvas.size().width()) - 1;
let start = last.saturating_sub(i32::try_from(s.chars().count()).unwrap_or(i32::MAX));
let lead = start.saturating_sub(1);
canvas.put(last, y, gap, Ink::BLANK);
canvas.text(start, y, s, ink);
canvas.put(lead, y, gap, Ink::BLANK);
lead
}
pub fn step_glyph(dx: i32, dy: i32) -> Glyph {
match (dx != 0, dy != 0) {
(_, false) => Glyph::new('─', '-'),
(false, true) => Glyph::new('│', '|'),
_ if (dx > 0) == (dy > 0) => Glyph::new('╲', '\\'),
_ => Glyph::new('╱', '/'),
}
}
pub fn fit_radius(size: Size, frac: f64) -> f64 {
let limit = (f64::from(size.width()) / (2.0 * ASPECT)).min(f64::from(size.height()) / 2.0);
(frac.max(0.0) * limit).max(0.0)
}