1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
#[cfg(all(feature = "libm", not(feature = "std")))]
use crate::nostd_float::FloatExt;
/// An (x, y) coordinate.
///
/// # Example
/// ```
/// use ab_glyph_rasterizer::{point, Point};
/// let p: Point = point(0.1, 23.2);
/// ```
#[derive(Clone, Copy, Default, PartialEq, PartialOrd)]
pub struct Point {
pub x: f32,
pub y: f32,
}
impl core::fmt::Debug for Point {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "point({:?}, {:?})", self.x, self.y)
}
}
impl Point {
#[inline]
pub(crate) fn distance_to(self, other: Point) -> f32 {
let d = other - self;
(d.x * d.x + d.y * d.y).sqrt()
}
}
/// [`Point`](struct.Point.html) constructor.
///
/// # Example
/// ```
/// # use ab_glyph_rasterizer::{point, Point};
/// let p = point(0.1, 23.2);
/// ```
#[inline]
pub fn point(x: f32, y: f32) -> Point {
Point { x, y }
}
/// Linear interpolation between points.
#[inline]
pub(crate) fn lerp(t: f32, p0: Point, p1: Point) -> Point {
point(p0.x + t * (p1.x - p0.x), p0.y + t * (p1.y - p0.y))
}
impl core::ops::Sub for Point {
type Output = Point;
/// Subtract rhs.x from x, rhs.y from y.
///
/// ```
/// # use ab_glyph_rasterizer::*;
/// let p1 = point(1.0, 2.0) - point(2.0, 1.5);
///
/// assert!((p1.x - -1.0).abs() <= core::f32::EPSILON);
/// assert!((p1.y - 0.5).abs() <= core::f32::EPSILON);
/// ```
#[inline]
fn sub(self, rhs: Point) -> Point {
point(self.x - rhs.x, self.y - rhs.y)
}
}
impl core::ops::Add for Point {
type Output = Point;
/// Add rhs.x to x, rhs.y to y.
///
/// ```
/// # use ab_glyph_rasterizer::*;
/// let p1 = point(1.0, 2.0) + point(2.0, 1.5);
///
/// assert!((p1.x - 3.0).abs() <= core::f32::EPSILON);
/// assert!((p1.y - 3.5).abs() <= core::f32::EPSILON);
/// ```
#[inline]
fn add(self, rhs: Point) -> Point {
point(self.x + rhs.x, self.y + rhs.y)
}
}
impl core::ops::AddAssign for Point {
/// ```
/// # use ab_glyph_rasterizer::*;
/// let mut p1 = point(1.0, 2.0);
/// p1 += point(2.0, 1.5);
///
/// assert!((p1.x - 3.0).abs() <= core::f32::EPSILON);
/// assert!((p1.y - 3.5).abs() <= core::f32::EPSILON);
/// ```
#[inline]
fn add_assign(&mut self, other: Self) {
self.x += other.x;
self.y += other.y;
}
}
impl core::ops::SubAssign for Point {
/// ```
/// # use ab_glyph_rasterizer::*;
/// let mut p1 = point(1.0, 2.0);
/// p1 -= point(2.0, 1.5);
///
/// assert!((p1.x - -1.0).abs() <= core::f32::EPSILON);
/// assert!((p1.y - 0.5).abs() <= core::f32::EPSILON);
/// ```
#[inline]
fn sub_assign(&mut self, other: Self) {
self.x -= other.x;
self.y -= other.y;
}
}
impl<F: Into<f32>> From<(F, F)> for Point {
/// ```
/// # use ab_glyph_rasterizer::*;
/// let p: Point = (23_f32, 34.5_f32).into();
/// let p2: Point = (5u8, 44u8).into();
/// ```
#[inline]
fn from((x, y): (F, F)) -> Self {
point(x.into(), y.into())
}
}
impl<F: Into<f32>> From<[F; 2]> for Point {
/// ```
/// # use ab_glyph_rasterizer::*;
/// let p: Point = [23_f32, 34.5].into();
/// let p2: Point = [5u8, 44].into();
/// ```
#[inline]
fn from([x, y]: [F; 2]) -> Self {
point(x.into(), y.into())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn distance_to() {
let distance = point(0.0, 0.0).distance_to(point(3.0, 4.0));
assert!((distance - 5.0).abs() <= core::f32::EPSILON);
}
}