use super::options::{Anchor, Shape};
use super::Point;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Border {
pub shape: Shape,
pub half: (f64, f64),
pub outer: f64,
}
impl Default for Border {
fn default() -> Border {
Border {
shape: Shape::Coordinate,
half: (0.0, 0.0),
outer: 0.0,
}
}
}
impl Border {
pub fn of(
shape: Shape,
text_half: (f64, f64),
minimum: (f64, f64),
outer: f64,
aspect: f64,
) -> Border {
let (tx, ty) = text_half;
let half = match shape {
Shape::Coordinate => (0.0, 0.0),
Shape::Rectangle => (
tx.max(minimum.0 / 2.0) + outer,
ty.max(minimum.1 / 2.0) + outer,
),
Shape::Circle => {
let radius = circle_radius(tx, ty)
.max(minimum.0 / 2.0)
.max(minimum.1 / 2.0)
+ outer;
(radius, radius)
}
Shape::Ellipse => (
(ROOT_TWO * tx).max(minimum.0 / 2.0) + outer,
(ROOT_TWO * ty).max(minimum.1 / 2.0) + outer,
),
Shape::Diamond => {
let x = tx + aspect * ty;
let y = match aspect == 0.0 {
true => ty,
false => tx / aspect + ty,
};
(
x.max(minimum.0 / 2.0) + outer,
y.max(minimum.1 / 2.0) + outer,
)
}
};
Border { shape, half, outer }
}
pub fn drawn(&self) -> (f64, f64) {
let (hx, hy) = self.half;
match self.shape {
Shape::Coordinate => (0.0, 0.0),
Shape::Rectangle | Shape::Circle | Shape::Ellipse => (hx - self.outer, hy - self.outer),
Shape::Diamond => (
hx - ROOT_TWO_PGF * self.outer,
hy - ROOT_TWO_PGF * self.outer,
),
}
}
pub fn anchor(&self, anchor: Anchor) -> Point {
let (hx, hy) = self.half;
let (dx, dy) = anchor.offset();
match self.shape {
Shape::Circle | Shape::Ellipse if dx != 0.0 && dy != 0.0 => {
(dx * COS45 * hx, dy * COS45 * hy)
}
Shape::Diamond if dx != 0.0 && dy != 0.0 => (dx * hx / 2.0, dy * hy / 2.0),
_ => (dx * hx, dy * hy),
}
}
pub fn border_at(&self, degrees: f64) -> Point {
let (sin, cos) = degrees.to_radians().sin_cos();
self.toward((cos, sin))
}
pub fn toward(&self, (dx, dy): Point) -> Point {
let (hx, hy) = self.half;
if (dx == 0.0 && dy == 0.0) || (hx == 0.0 && hy == 0.0) {
return (0.0, 0.0);
}
match self.shape {
Shape::Coordinate => (0.0, 0.0),
Shape::Rectangle => match (dy / dx).abs() * hx <= hy {
true => (dx.signum() * hx, dy / dx.abs() * hx),
false => (dx / dy.abs() * hy, dy.signum() * hy),
},
Shape::Circle | Shape::Ellipse => {
let (ux, uy) = (dx / hx, dy / hy);
let length = ux.hypot(uy);
(hx * ux / length, hy * uy / length)
}
Shape::Diamond => {
let (sx, sy) = (dx.signum(), dy.signum());
let scale = 1.0 / (dx.abs() / hx + dy.abs() / hy);
(sx * dx.abs() * scale, sy * dy.abs() * scale)
}
}
}
}
#[allow(clippy::approx_constant)]
const ROOT_TWO: f64 = 1.414_213_6;
#[allow(clippy::approx_constant)]
const ROOT_TWO_PGF: f64 = 1.414_213;
#[allow(clippy::approx_constant)]
const COS45: f64 = 0.707_107;
fn circle_radius(half_width: f64, half_height: f64) -> f64 {
let length = half_width.hypot(half_height);
if length == 0.0 {
return 0.0;
}
let unit = |value: f64| (value / length * 65536.0).trunc();
let (nx, ny) = (unit(half_width), unit(half_height));
let (bigger, along) = match nx > ny {
true => (nx, half_width),
false => (ny, half_height),
};
let divisor = (bigger / 255.0).trunc();
if divisor == 0.0 {
return length;
}
let sp = (along * 65536.0).trunc();
(16.0 * ((16.0 * sp) / divisor).trunc()) / 65536.0
}
#[cfg(test)]
mod tests {
use super::*;
fn hi() -> ((f64, f64), f64) {
((8.44142, 6.72284), 0.19925)
}
#[test]
fn a_rectangles_anchors_stand_off_by_the_outer_separation() {
let (half, outer) = hi();
let border = Border::of(Shape::Rectangle, half, (0.0, 0.0), outer, 1.0);
let (x, y) = border.anchor(Anchor::North);
assert!(
(x - 0.0).abs() < 1e-9 && (y - 6.92209).abs() < 1e-5,
"{x} {y}"
);
let (x, y) = border.anchor(Anchor::NorthEast);
assert!(
(x - 8.64067).abs() < 1e-5 && (y - 6.92209).abs() < 1e-5,
"{x} {y}"
);
let (dx, dy) = border.drawn();
assert!((dx - 8.44142).abs() < 1e-9 && (dy - 6.72284).abs() < 1e-9);
}
#[test]
fn a_rectangles_border_at_an_angle_is_where_the_ray_leaves_it() {
let (half, outer) = hi();
let border = Border::of(Shape::Rectangle, half, (0.0, 0.0), outer, 1.0);
let (x, y) = border.border_at(30.0);
assert!((x - 8.64067).abs() < 1e-4, "{x}");
assert!((y - 4.98892).abs() < 4e-3, "{y}");
}
#[test]
fn a_circles_radius_is_pgfs_arithmetic_and_not_the_hypotenuse() {
let (half, outer) = hi();
let border = Border::of(Shape::Circle, half, (0.0, 0.0), outer, 1.0);
let (drawn, _) = border.drawn();
assert!((drawn - 10.75107).abs() < 4e-3, "{drawn}");
assert!(
(drawn - half.0.hypot(half.1)).abs() > 0.03,
"and not the hypotenuse"
);
let (_, y) = border.anchor(Anchor::South);
assert!((y + 10.95033).abs() < 4e-3, "{y}");
}
#[test]
fn a_circles_border_at_an_angle_is_on_the_circle() {
let (half, outer) = hi();
let border = Border::of(Shape::Circle, half, (0.0, 0.0), outer, 1.0);
let (x, y) = border.border_at(60.0);
assert!((x - 5.47482).abs() < 4e-3, "{x}");
assert!((y - 9.48328).abs() < 4e-3, "{y}");
}
#[test]
fn a_diamonds_corner_anchors_are_the_middle_of_its_sides() {
let border = Border::of(Shape::Diamond, (10.0, 5.0), (0.0, 0.0), 0.0, 1.0);
assert_eq!(border.half, (15.0, 15.0));
assert_eq!(border.anchor(Anchor::East), (15.0, 0.0));
assert_eq!(border.anchor(Anchor::NorthEast), (7.5, 7.5));
let (x, y) = border.border_at(45.0);
assert!((x - 7.5).abs() < 1e-9 && (y - 7.5).abs() < 1e-9, "{x} {y}");
}
#[test]
fn a_coordinate_has_no_extent_and_every_anchor_is_the_point() {
let border = Border::default();
assert_eq!(border.anchor(Anchor::North), (0.0, 0.0));
assert_eq!(border.border_at(30.0), (0.0, 0.0));
}
}