97 lines
1.7 KiB
Rust
97 lines
1.7 KiB
Rust
use std::clone::Clone;
|
|
use std::cmp::Eq;
|
|
use std::cmp::PartialEq;
|
|
use std::fmt::Display;
|
|
use std::marker::Copy;
|
|
use std::ops::Add;
|
|
use std::ops::Div;
|
|
use std::ops::Mul;
|
|
use std::ops::Neg;
|
|
use std::ops::Sub;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Point {
|
|
x: f64,
|
|
y: f64,
|
|
}
|
|
|
|
impl Point {
|
|
pub fn x(&self) -> f64 {
|
|
self.x
|
|
}
|
|
|
|
pub fn y(&self) -> f64 {
|
|
self.y
|
|
}
|
|
|
|
pub fn new(x: f64, y: f64) -> Point {
|
|
Point { x, y }
|
|
}
|
|
}
|
|
|
|
impl Add<&Point> for &Point {
|
|
fn add(self, rhs: &Point) -> Self::Output {
|
|
Point {
|
|
x: self.x() + rhs.x(),
|
|
y: self.y() + rhs.y(),
|
|
}
|
|
}
|
|
type Output = Point;
|
|
}
|
|
|
|
impl Sub<&Point> for &Point {
|
|
fn sub(self, rhs: &Point) -> Self::Output {
|
|
Point {
|
|
x: self.x() - rhs.x(),
|
|
y: self.y() - rhs.y(),
|
|
}
|
|
}
|
|
type Output = Point;
|
|
}
|
|
|
|
impl Mul<f64> for &Point {
|
|
fn mul(self, rhs: f64) -> Self::Output {
|
|
Point::new(self.x * rhs, self.y * rhs)
|
|
}
|
|
type Output = Point;
|
|
}
|
|
|
|
impl Div<f64> for &Point {
|
|
fn div(self, rhs: f64) -> Self::Output {
|
|
Point::new(self.x / rhs, self.y / rhs)
|
|
}
|
|
type Output = Point;
|
|
}
|
|
|
|
impl Neg for &Point {
|
|
fn neg(self) -> Self::Output {
|
|
Point::new(-self.x(), -self.y())
|
|
}
|
|
|
|
type Output = Point;
|
|
}
|
|
|
|
impl PartialEq for Point {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.x.eq(&other.x) && self.y.eq(&other.y)
|
|
}
|
|
}
|
|
|
|
impl Eq for Point {}
|
|
|
|
impl Clone for Point {
|
|
fn clone(&self) -> Self {
|
|
*self
|
|
}
|
|
}
|
|
|
|
impl Copy for Point {}
|
|
|
|
impl Display for Point {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(f, "Point({}, {})", self.x, self.y)
|
|
}
|
|
}
|
|
|
|
impl Point {}
|