-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathpoint.rs
38 lines (31 loc) · 858 Bytes
/
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
use std::ops::Sub;
#[derive(Clone, Debug, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
// Returns the orientation of consecutive segments ab and bc.
pub fn consecutive_orientation(&self, b: &Point, c: &Point) -> f64 {
let p1 = b - self;
let p2 = c - self;
p1.cross_prod(&p2)
}
pub fn cross_prod(&self, other: &Point) -> f64 {
self.x * other.y - self.y * other.x
}
pub fn euclidean_distance(&self, other: &Point) -> f64 {
((self.x - other.x).powi(2) + (self.y - other.y).powi(2)).sqrt()
}
}
impl Sub for &Point {
type Output = Point;
fn sub(self, other: Self) -> Point {
let x = self.x - other.x;
let y = self.y - other.y;
Point::new(x, y)
}
}