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
extern crate euclid;
use std::f64;
#[inline]
pub(crate) fn f64_bound(min: f64, val: f64, max: f64) -> f64 {
if val > max {
return max;
} else if val < min {
return min;
}
val
}
#[allow(missing_docs)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub(crate) struct Line {
pub x1: f64,
pub y1: f64,
pub x2: f64,
pub y2: f64,
}
impl Line {
pub fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> Line {
Line { x1, y1, x2, y2 }
}
pub fn length(&self) -> f64 {
let x = self.x2 - self.x1;
let y = self.y2 - self.y1;
(x*x + y*y).sqrt()
}
pub fn set_length(&mut self, len: f64) {
let x = self.x2 - self.x1;
let y = self.y2 - self.y1;
let len2 = (x*x + y*y).sqrt();
let line = Line {
x1: self.x1, y1: self.y1,
x2: self.x1 + x/len2, y2: self.y1 + y/len2
};
self.x2 = self.x1 + (line.x2 - line.x1) * len;
self.y2 = self.y1 + (line.y2 - line.y1) * len;
}
}
pub(crate) type Point = euclid::Point2D<f64>;
pub type Size = euclid::Size2D<f64>;
pub type Rect = euclid::Rect<f64>;
pub(crate) use self::euclid::rect;