kurbo/point.rs
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
// Copyright 2019 the Kurbo Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! A 2D point.
use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
use crate::common::FloatExt;
use crate::Vec2;
#[cfg(not(feature = "std"))]
use crate::common::FloatFuncs;
/// A 2D point.
///
/// This type represents a point in 2D space. It has the same layout as [`Vec2`][crate::Vec2], but
/// its meaning is different: `Vec2` represents a change in location (for example velocity).
///
/// In general, `kurbo` overloads math operators where it makes sense, for example implementing
/// `Affine * Point` as the point under the affine transformation. However `Point + Point` and
/// `f64 * Point` are not implemented, because the operations do not make geometric sense. If you
/// need to apply these operations, then 1) check what you're doing makes geometric sense, then 2)
/// use [`Point::to_vec2`] to convert the point to a `Vec2`.
#[derive(Clone, Copy, Default, PartialEq)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point {
/// The x coordinate.
pub x: f64,
/// The y coordinate.
pub y: f64,
}
impl Point {
/// The point (0, 0).
pub const ZERO: Point = Point::new(0., 0.);
/// The point at the origin; (0, 0).
pub const ORIGIN: Point = Point::new(0., 0.);
/// Create a new `Point` with the provided `x` and `y` coordinates.
#[inline]
pub const fn new(x: f64, y: f64) -> Self {
Point { x, y }
}
/// Convert this point into a `Vec2`.
#[inline]
pub const fn to_vec2(self) -> Vec2 {
Vec2::new(self.x, self.y)
}
/// Linearly interpolate between two points.
#[inline]
pub fn lerp(self, other: Point, t: f64) -> Point {
self.to_vec2().lerp(other.to_vec2(), t).to_point()
}
/// Determine the midpoint of two points.
#[inline]
pub fn midpoint(self, other: Point) -> Point {
Point::new(0.5 * (self.x + other.x), 0.5 * (self.y + other.y))
}
/// Euclidean distance.
///
/// See [`Vec2::hypot`] for the same operation on [`Vec2`].
#[inline]
pub fn distance(self, other: Point) -> f64 {
(self - other).hypot()
}
/// Squared Euclidean distance.
///
/// See [`Vec2::hypot2`] for the same operation on [`Vec2`].
#[inline]
pub fn distance_squared(self, other: Point) -> f64 {
(self - other).hypot2()
}
/// Returns a new `Point`, with `x` and `y` [rounded] to the nearest integer.
///
/// # Examples
///
/// ```
/// use kurbo::Point;
/// let a = Point::new(3.3, 3.6).round();
/// let b = Point::new(3.0, -3.1).round();
/// assert_eq!(a.x, 3.0);
/// assert_eq!(a.y, 4.0);
/// assert_eq!(b.x, 3.0);
/// assert_eq!(b.y, -3.0);
/// ```
///
/// [rounded]: f64::round
#[inline]
pub fn round(self) -> Point {
Point::new(self.x.round(), self.y.round())
}
/// Returns a new `Point`,
/// with `x` and `y` [rounded up] to the nearest integer,
/// unless they are already an integer.
///
/// # Examples
///
/// ```
/// use kurbo::Point;
/// let a = Point::new(3.3, 3.6).ceil();
/// let b = Point::new(3.0, -3.1).ceil();
/// assert_eq!(a.x, 4.0);
/// assert_eq!(a.y, 4.0);
/// assert_eq!(b.x, 3.0);
/// assert_eq!(b.y, -3.0);
/// ```
///
/// [rounded up]: f64::ceil
#[inline]
pub fn ceil(self) -> Point {
Point::new(self.x.ceil(), self.y.ceil())
}
/// Returns a new `Point`,
/// with `x` and `y` [rounded down] to the nearest integer,
/// unless they are already an integer.
///
/// # Examples
///
/// ```
/// use kurbo::Point;
/// let a = Point::new(3.3, 3.6).floor();
/// let b = Point::new(3.0, -3.1).floor();
/// assert_eq!(a.x, 3.0);
/// assert_eq!(a.y, 3.0);
/// assert_eq!(b.x, 3.0);
/// assert_eq!(b.y, -4.0);
/// ```
///
/// [rounded down]: f64::floor
#[inline]
pub fn floor(self) -> Point {
Point::new(self.x.floor(), self.y.floor())
}
/// Returns a new `Point`,
/// with `x` and `y` [rounded away] from zero to the nearest integer,
/// unless they are already an integer.
///
/// # Examples
///
/// ```
/// use kurbo::Point;
/// let a = Point::new(3.3, 3.6).expand();
/// let b = Point::new(3.0, -3.1).expand();
/// assert_eq!(a.x, 4.0);
/// assert_eq!(a.y, 4.0);
/// assert_eq!(b.x, 3.0);
/// assert_eq!(b.y, -4.0);
/// ```
///
/// [rounded away]: FloatExt::expand
#[inline]
pub fn expand(self) -> Point {
Point::new(self.x.expand(), self.y.expand())
}
/// Returns a new `Point`,
/// with `x` and `y` [rounded towards] zero to the nearest integer,
/// unless they are already an integer.
///
/// # Examples
///
/// ```
/// use kurbo::Point;
/// let a = Point::new(3.3, 3.6).trunc();
/// let b = Point::new(3.0, -3.1).trunc();
/// assert_eq!(a.x, 3.0);
/// assert_eq!(a.y, 3.0);
/// assert_eq!(b.x, 3.0);
/// assert_eq!(b.y, -3.0);
/// ```
///
/// [rounded towards]: f64::trunc
#[inline]
pub fn trunc(self) -> Point {
Point::new(self.x.trunc(), self.y.trunc())
}
/// Is this point [finite]?
///
/// [finite]: f64::is_finite
#[inline]
pub fn is_finite(self) -> bool {
self.x.is_finite() && self.y.is_finite()
}
/// Is this point [`NaN`]?
///
/// [`NaN`]: f64::is_nan
#[inline]
pub fn is_nan(self) -> bool {
self.x.is_nan() || self.y.is_nan()
}
}
impl From<(f32, f32)> for Point {
#[inline]
fn from(v: (f32, f32)) -> Point {
Point {
x: v.0 as f64,
y: v.1 as f64,
}
}
}
impl From<(f64, f64)> for Point {
#[inline]
fn from(v: (f64, f64)) -> Point {
Point { x: v.0, y: v.1 }
}
}
impl From<Point> for (f64, f64) {
#[inline]
fn from(v: Point) -> (f64, f64) {
(v.x, v.y)
}
}
impl Add<Vec2> for Point {
type Output = Point;
#[inline]
fn add(self, other: Vec2) -> Self {
Point::new(self.x + other.x, self.y + other.y)
}
}
impl AddAssign<Vec2> for Point {
#[inline]
fn add_assign(&mut self, other: Vec2) {
*self = Point::new(self.x + other.x, self.y + other.y);
}
}
impl Sub<Vec2> for Point {
type Output = Point;
#[inline]
fn sub(self, other: Vec2) -> Self {
Point::new(self.x - other.x, self.y - other.y)
}
}
impl SubAssign<Vec2> for Point {
#[inline]
fn sub_assign(&mut self, other: Vec2) {
*self = Point::new(self.x - other.x, self.y - other.y);
}
}
impl Add<(f64, f64)> for Point {
type Output = Point;
#[inline]
fn add(self, (x, y): (f64, f64)) -> Self {
Point::new(self.x + x, self.y + y)
}
}
impl AddAssign<(f64, f64)> for Point {
#[inline]
fn add_assign(&mut self, (x, y): (f64, f64)) {
*self = Point::new(self.x + x, self.y + y);
}
}
impl Sub<(f64, f64)> for Point {
type Output = Point;
#[inline]
fn sub(self, (x, y): (f64, f64)) -> Self {
Point::new(self.x - x, self.y - y)
}
}
impl SubAssign<(f64, f64)> for Point {
#[inline]
fn sub_assign(&mut self, (x, y): (f64, f64)) {
*self = Point::new(self.x - x, self.y - y);
}
}
impl Sub<Point> for Point {
type Output = Vec2;
#[inline]
fn sub(self, other: Point) -> Vec2 {
Vec2::new(self.x - other.x, self.y - other.y)
}
}
impl fmt::Debug for Point {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({:?}, {:?})", self.x, self.y)
}
}
impl fmt::Display for Point {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "(")?;
fmt::Display::fmt(&self.x, formatter)?;
write!(formatter, ", ")?;
fmt::Display::fmt(&self.y, formatter)?;
write!(formatter, ")")
}
}
#[cfg(feature = "mint")]
impl From<Point> for mint::Point2<f64> {
#[inline]
fn from(p: Point) -> mint::Point2<f64> {
mint::Point2 { x: p.x, y: p.y }
}
}
#[cfg(feature = "mint")]
impl From<mint::Point2<f64>> for Point {
#[inline]
fn from(p: mint::Point2<f64>) -> Point {
Point { x: p.x, y: p.y }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn point_arithmetic() {
assert_eq!(
Point::new(0., 0.) - Vec2::new(10., 0.),
Point::new(-10., 0.)
);
assert_eq!(
Point::new(0., 0.) - Point::new(-5., 101.),
Vec2::new(5., -101.)
);
}
#[test]
#[allow(clippy::float_cmp)]
fn distance() {
let p1 = Point::new(0., 10.);
let p2 = Point::new(0., 5.);
assert_eq!(p1.distance(p2), 5.);
let p1 = Point::new(-11., 1.);
let p2 = Point::new(-7., -2.);
assert_eq!(p1.distance(p2), 5.);
}
#[test]
fn display() {
let p = Point::new(0.12345, 9.87654);
assert_eq!(format!("{p}"), "(0.12345, 9.87654)");
let p = Point::new(0.12345, 9.87654);
assert_eq!(format!("{p:.2}"), "(0.12, 9.88)");
}
}