summaryrefslogtreecommitdiffstats
path: root/core/src/point.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2023-12-05 01:03:09 +0100
committerLibravatar GitHub <noreply@github.com>2023-12-05 01:03:09 +0100
commitfc285d3e461626408c56bbc1605fcf0c974b2f69 (patch)
tree8aca292516d9aa43b78a14f51dd90caf60c691d7 /core/src/point.rs
parent8727b3fc50ec251d9c117c51ca1289be5ba9b117 (diff)
parent5c5e7653bed248ba63faa6563e4d673e4441415e (diff)
downloadiced-fc285d3e461626408c56bbc1605fcf0c974b2f69.tar.gz
iced-fc285d3e461626408c56bbc1605fcf0c974b2f69.tar.bz2
iced-fc285d3e461626408c56bbc1605fcf0c974b2f69.zip
Merge pull request #1964 from bungoboingo/feat/multi-window-support
[Feature] 🪟 Multi Window 🪟 .. redux!
Diffstat (limited to 'core/src/point.rs')
-rw-r--r--core/src/point.rs58
1 files changed, 42 insertions, 16 deletions
diff --git a/core/src/point.rs b/core/src/point.rs
index 9bf7726b..ef42852f 100644
--- a/core/src/point.rs
+++ b/core/src/point.rs
@@ -1,26 +1,34 @@
use crate::Vector;
+use num_traits::{Float, Num};
+use std::fmt;
+
/// A 2D point.
-#[derive(Debug, Clone, Copy, PartialEq, Default)]
-pub struct Point {
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
+pub struct Point<T = f32> {
/// The X coordinate.
- pub x: f32,
+ pub x: T,
/// The Y coordinate.
- pub y: f32,
+ pub y: T,
}
impl Point {
/// The origin (i.e. a [`Point`] at (0, 0)).
- pub const ORIGIN: Point = Point::new(0.0, 0.0);
+ pub const ORIGIN: Self = Self::new(0.0, 0.0);
+}
+impl<T: Num> Point<T> {
/// Creates a new [`Point`] with the given coordinates.
- pub const fn new(x: f32, y: f32) -> Self {
+ pub const fn new(x: T, y: T) -> Self {
Self { x, y }
}
/// Computes the distance to another [`Point`].
- pub fn distance(&self, to: Point) -> f32 {
+ pub fn distance(&self, to: Self) -> T
+ where
+ T: Float,
+ {
let a = self.x - to.x;
let b = self.y - to.y;
@@ -34,9 +42,9 @@ impl From<[f32; 2]> for Point {
}
}
-impl From<[u16; 2]> for Point {
+impl From<[u16; 2]> for Point<u16> {
fn from([x, y]: [u16; 2]) -> Self {
- Point::new(x.into(), y.into())
+ Point::new(x, y)
}
}
@@ -46,10 +54,13 @@ impl From<Point> for [f32; 2] {
}
}
-impl std::ops::Add<Vector> for Point {
+impl<T> std::ops::Add<Vector<T>> for Point<T>
+where
+ T: std::ops::Add<Output = T>,
+{
type Output = Self;
- fn add(self, vector: Vector) -> Self {
+ fn add(self, vector: Vector<T>) -> Self {
Self {
x: self.x + vector.x,
y: self.y + vector.y,
@@ -57,10 +68,13 @@ impl std::ops::Add<Vector> for Point {
}
}
-impl std::ops::Sub<Vector> for Point {
+impl<T> std::ops::Sub<Vector<T>> for Point<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
type Output = Self;
- fn sub(self, vector: Vector) -> Self {
+ fn sub(self, vector: Vector<T>) -> Self {
Self {
x: self.x - vector.x,
y: self.y - vector.y,
@@ -68,10 +82,22 @@ impl std::ops::Sub<Vector> for Point {
}
}
-impl std::ops::Sub<Point> for Point {
- type Output = Vector;
+impl<T> std::ops::Sub<Point<T>> for Point<T>
+where
+ T: std::ops::Sub<Output = T>,
+{
+ type Output = Vector<T>;
- fn sub(self, point: Point) -> Vector {
+ fn sub(self, point: Self) -> Vector<T> {
Vector::new(self.x - point.x, self.y - point.y)
}
}
+
+impl<T> fmt::Display for Point<T>
+where
+ T: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "Point {{ x: {}, y: {} }}", self.x, self.y)
+ }
+}