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
//! A shape type representing lines used for drawing.
//!
//! # Examples
//!
//! You can create a [Line] using [`Line::new`]:
//!
//! ```
//! use pix_engine::prelude::*;
//!
//! // 2D
//! let line = Line::new([10, 20], [30, 10]);
//!
//! let p1 = point![10, 20];
//! let p2 = point![30, 10];
//! let line = Line::new(p1, p2);
//!
//! // 3D
//! let line = Line::new([10, 20, 5], [30, 10, 5]);
//! ```
use crate::{error::Result, prelude::*};
#[cfg(feature = "serde")]
use serde::{de::DeserializeOwned, Deserialize, Serialize};
/// A `Line` with start and end [Point]s.
///
/// Please see the [module-level documentation] for examples.
///
/// [module-level documentation]: crate::shape::line
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(transparent)]
#[must_use]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(bound = "T: Serialize + DeserializeOwned"))]
pub struct Line<T = i32, const N: usize = 2>(pub(crate) [Point<T, N>; 2]);
/// Constructs a [Line] with two points.
///
/// ```
/// # use pix_engine::prelude::*;
///
/// let l = line_!([10, 20], [30, 10]);
/// assert_eq!(l.points(), [
/// point!(10, 20),
/// point!(30, 10),
/// ]);
///
/// let l = line_!([10, 20, 10], [30, 10, 40]);
/// assert_eq!(l.points(), [
/// point!(10, 20, 10),
/// point!(30, 10, 40),
/// ]);
/// ```
#[macro_export]
macro_rules! line_ {
($p1:expr, $p2:expr$(,)?) => {
$crate::prelude::Line::new($p1, $p2)
};
($x1:expr, $y1:expr, $x2:expr, $y2:expr$(,)?) => {
$crate::prelude::Line::from_xy($x1, $y1, $x2, $y2)
};
($x1:expr, $y1:expr, $z1:expr, $x2:expr, $y2:expr, $z2:expr$(,)?) => {
$crate::prelude::Line::from_xyz($x1, $y1, $z2, $x2, $y2, $z2)
};
}
impl<T, const N: usize> Line<T, N> {
/// Constructs a `Line` from `start` to `end` [Point]s.
///
/// # Example
/// ```
/// # use pix_engine::prelude::*;
/// // 2D
/// let line = Line::new([10, 20], [30, 10]);
///
/// let p1 = point![10, 20];
/// let p2 = point![30, 10];
/// let line = Line::new(p1, p2);
///
/// // 3D
/// let line: Line<i32, 3> = Line::new([10, 20, 5], [30, 10, 5]);
/// ```
pub fn new<P1, P2>(start: P1, end: P2) -> Self
where
P1: Into<Point<T, N>>,
P2: Into<Point<T, N>>,
{
Self([start.into(), end.into()])
}
}
impl<T> Line<T> {
/// Constructs a `Line` from individual x/y coordinates.
#[inline]
pub const fn from_xy(x1: T, y1: T, x2: T, y2: T) -> Self {
Self([point!(x1, y1), point!(x2, y2)])
}
}
impl<T: Copy> Line<T> {
/// Returns `Line` coordinates as `[x1, y1, x2, y2]`.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// let p1 = point!(5, 10);
/// let p2 = point!(100, 100);
/// let l = Line::new(p1, p2);
/// assert_eq!(l.coords(), [5, 10, 100, 100]);
/// ```
#[inline]
pub fn coords(&self) -> [T; 4] {
let [p1, p2] = self.points();
let [x1, y1] = p1.coords();
let [x2, y2] = p2.coords();
[x1, y1, x2, y2]
}
}
impl<T> Line<T, 3> {
/// Constructs a `Line` from individual x/y/z coordinates.
#[inline]
pub const fn from_xyz(x1: T, y1: T, z1: T, x2: T, y2: T, z2: T) -> Self {
Self([point!(x1, y1, z1), point!(x2, y2, z2)])
}
}
impl<T: Copy> Line<T, 3> {
/// Returns `Line` coordinates as `[x1, y1, z1, x2, y2, z2]`.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// let p1 = point!(5, 10);
/// let p2 = point!(100, 100);
/// let l = Line::new(p1, p2);
/// assert_eq!(l.coords(), [5, 10, 100, 100]);
/// ```
#[inline]
pub fn coords(&self) -> [T; 6] {
let [p1, p2] = self.points();
let [x1, y1, z1] = p1.coords();
let [x2, y2, z2] = p2.coords();
[x1, y1, z1, x2, y2, z2]
}
}
impl<T: Copy, const N: usize> Line<T, N> {
/// Returns the starting point of the line.
#[inline]
pub fn start(&self) -> Point<T, N> {
self.0[0]
}
/// Sets the starting point of the line.
#[inline]
pub fn set_start<P: Into<Point<T, N>>>(&mut self, start: P) {
self.0[0] = start.into();
}
/// Returns the ending point of the line.
#[inline]
pub fn end(&self) -> Point<T, N> {
self.0[1]
}
/// Sets the ending point of the line.
#[inline]
pub fn set_end<P: Into<Point<T, N>>>(&mut self, end: P) {
self.0[1] = end.into();
}
/// Returns `Line` points as `[Point<T, N>; 3]`.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// let p1 = point!(5, 10);
/// let p2 = point!(100, 100);
/// let l = Line::new(p1, p2);
/// assert_eq!(l.points(), [point!(5, 10), point!(100, 100)]);
/// ```
#[inline]
pub fn points(&self) -> [Point<T, N>; 2] {
self.0
}
/// Returns `Line` points as a mutable slice `&mut [Point<T, N>; 3]`.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// let p1 = point!(5, 10);
/// let p2 = point!(100, 100);
/// let mut l = Line::new(p1, p2);
/// for p in l.points_mut() {
/// *p += 5;
/// }
/// assert_eq!(l.points(), [point!(10, 15), point!(105, 105)]);
/// ```
#[inline]
pub fn points_mut(&mut self) -> &mut [Point<T, N>; 2] {
&mut self.0
}
/// Returns `Line` as a [Vec].
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// let p1 = point!(5, 10);
/// let p2 = point!(100, 100);
/// let l = Line::new(p1, p2);
/// assert_eq!(l.to_vec(), vec![[5, 10], [100, 100]]);
/// ```
pub fn to_vec(self) -> Vec<Vec<T>> {
let start = self.start().to_vec();
let end = self.end().to_vec();
vec![start, end]
}
}
impl<T: Float> Intersects<Line<T>> for Line<T> {
type Result = (Point<T>, T);
/// Returns the closest intersection point with a given line and distance along the line or
/// `None` if there is no intersection.
#[allow(clippy::many_single_char_names)]
fn intersects(&self, line: Line<T>) -> Option<Self::Result> {
let [x1, y1, x2, y2] = self.coords();
let [x3, y3, x4, y4] = line.coords();
let d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if d == T::zero() {
return None;
}
let t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / d;
let u = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / d;
if (T::zero()..).contains(&t) && (T::zero()..=T::one()).contains(&u) {
let x = x1 + t * (x2 - x1);
let y = y1 + t * (y2 - y1);
Some((point!(x, y), t))
} else {
None
}
}
}
impl Draw for Line<i32> {
/// Draw `Line` to the current [`PixState`] canvas.
fn draw(&self, s: &mut PixState) -> Result<()> {
s.line(*self)
}
}
impl<T: Copy> From<[T; 4]> for Line<T> {
/// Converts `[T; 4]` into `Line<T>`.
#[inline]
fn from([x1, y1, x2, y2]: [T; 4]) -> Self {
Self::from_xy(x1, y1, x2, y2)
}
}
impl<T: Copy> From<[T; 6]> for Line<T, 3> {
/// Converts `[T; 6]` into `Line<T, 3>`.
#[inline]
fn from([x1, y1, z1, x2, y2, z2]: [T; 6]) -> Self {
Self::from_xyz(x1, y1, z1, x2, y2, z2)
}
}
impl<T: Copy> From<[[T; 2]; 2]> for Line<T> {
/// Converts `[[T; 2]; 2]` into `Line<T>`.
#[inline]
fn from([[x1, y1], [x2, y2]]: [[T; 2]; 2]) -> Self {
Self::from_xy(x1, y1, x2, y2)
}
}
impl<T: Copy> From<[[T; 3]; 2]> for Line<T, 3> {
/// Converts `[[T; 3]; 2]` into `Line<T, 3>`.
#[inline]
fn from([[x1, y1, z1], [x2, y2, z2]]: [[T; 3]; 2]) -> Self {
Self::from_xyz(x1, y1, z1, x2, y2, z2)
}
}