egui/emigui/src/math.rs

247 lines
5.2 KiB
Rust
Raw Normal View History

2018-12-26 16:01:46 +00:00
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
pub struct Vec2 {
pub x: f32,
pub y: f32,
}
2019-01-05 15:23:40 +00:00
impl Vec2 {
2019-04-25 16:07:36 +00:00
#[must_use]
2019-01-05 15:23:40 +00:00
pub fn normalized(self) -> Vec2 {
2019-04-25 16:07:36 +00:00
let len = self.length();
2019-01-05 15:23:40 +00:00
if len <= 0.0 {
self
} else {
self / len
}
}
pub fn rot90(self) -> Vec2 {
vec2(self.y, -self.x)
}
pub fn length(self) -> f32 {
self.x.hypot(self.y)
}
pub fn length_sq(self) -> f32 {
self.x * self.x + self.y * self.y
}
2019-04-25 16:07:36 +00:00
pub fn dist(a: Vec2, b: Vec2) -> f32 {
(a - b).length()
}
pub fn dist_sq(a: Vec2, b: Vec2) -> f32 {
(a - b).length_sq()
}
pub fn angled(angle: f32) -> Vec2 {
vec2(angle.cos(), angle.sin())
}
2019-01-05 15:23:40 +00:00
}
2019-11-02 08:50:49 +00:00
impl std::ops::Neg for Vec2 {
type Output = Vec2;
fn neg(self) -> Vec2 {
vec2(-self.x, -self.y)
}
}
2019-01-05 15:23:40 +00:00
impl std::ops::AddAssign for Vec2 {
2019-01-19 16:10:28 +00:00
fn add_assign(&mut self, rhs: Vec2) {
2019-01-05 15:23:40 +00:00
*self = Vec2 {
2019-01-19 16:10:28 +00:00
x: self.x + rhs.x,
y: self.y + rhs.y,
2019-01-05 15:23:40 +00:00
};
}
}
2018-12-26 16:01:46 +00:00
impl std::ops::Add for Vec2 {
type Output = Vec2;
fn add(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x + rhs.x,
y: self.y + rhs.y,
}
}
}
impl std::ops::Sub for Vec2 {
type Output = Vec2;
fn sub(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x - rhs.x,
y: self.y - rhs.y,
}
}
}
2019-01-19 16:10:28 +00:00
impl std::ops::MulAssign<f32> for Vec2 {
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
}
}
2018-12-26 16:01:46 +00:00
impl std::ops::Mul<f32> for Vec2 {
type Output = Vec2;
fn mul(self, factor: f32) -> Vec2 {
Vec2 {
x: self.x * factor,
y: self.y * factor,
}
}
}
2018-12-27 22:26:05 +00:00
impl std::ops::Mul<Vec2> for f32 {
type Output = Vec2;
fn mul(self, vec: Vec2) -> Vec2 {
Vec2 {
x: self * vec.x,
y: self * vec.y,
}
}
}
2019-01-05 15:23:40 +00:00
impl std::ops::Div<f32> for Vec2 {
type Output = Vec2;
fn div(self, factor: f32) -> Vec2 {
Vec2 {
x: self.x / factor,
y: self.y / factor,
}
}
}
2018-12-26 16:01:46 +00:00
pub fn vec2(x: f32, y: f32) -> Vec2 {
Vec2 { x, y }
}
2019-01-19 16:09:00 +00:00
// ----------------------------------------------------------------------------
2018-12-26 16:01:46 +00:00
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
pub struct Rect {
pub pos: Vec2,
pub size: Vec2,
}
impl Rect {
2018-12-26 22:08:50 +00:00
pub fn from_min_size(min: Vec2, size: Vec2) -> Self {
Rect { pos: min, size }
}
2018-12-26 16:01:46 +00:00
pub fn from_center_size(center: Vec2, size: Vec2) -> Self {
Rect {
pos: center - size * 0.5,
size,
}
}
2019-03-16 14:14:22 +00:00
/// Expand by this much in each direction
pub fn expand(self, amnt: f32) -> Self {
Rect::from_center_size(self.center(), self.size() + 2.0 * vec2(amnt, amnt))
}
2018-12-26 16:01:46 +00:00
pub fn contains(&self, p: Vec2) -> bool {
self.pos.x <= p.x
&& p.x <= self.pos.x + self.size.x
&& self.pos.y <= p.y
&& p.y <= self.pos.y + self.size.y
}
pub fn center(&self) -> Vec2 {
Vec2 {
x: self.pos.x + self.size.x / 2.0,
y: self.pos.y + self.size.y / 2.0,
}
}
pub fn min(&self) -> Vec2 {
self.pos
}
2019-02-10 19:56:59 +00:00
2018-12-26 16:01:46 +00:00
pub fn max(&self) -> Vec2 {
self.pos + self.size
}
2019-01-14 13:26:02 +00:00
pub fn size(&self) -> Vec2 {
self.size
}
2019-02-10 19:56:59 +00:00
pub fn width(&self) -> f32 {
self.size.x
}
pub fn height(&self) -> f32 {
self.size.y
}
2019-01-19 16:09:00 +00:00
// Convenience functions:
pub fn left_top(&self) -> Vec2 {
vec2(self.min().x, self.min().y)
}
pub fn center_top(&self) -> Vec2 {
vec2(self.center().x, self.min().y)
}
pub fn right_top(&self) -> Vec2 {
vec2(self.max().x, self.min().y)
}
pub fn left_center(&self) -> Vec2 {
vec2(self.min().x, self.center().y)
}
pub fn right_center(&self) -> Vec2 {
vec2(self.max().x, self.center().y)
}
pub fn left_bottom(&self) -> Vec2 {
vec2(self.min().x, self.max().y)
}
pub fn center_bottom(&self) -> Vec2 {
vec2(self.center().x, self.max().y)
}
pub fn right_bottom(&self) -> Vec2 {
vec2(self.max().x, self.max().y)
}
2018-12-26 16:01:46 +00:00
}
2019-01-19 16:09:00 +00:00
// ----------------------------------------------------------------------------
2019-04-25 16:07:36 +00:00
pub fn lerp<T>(min: T, max: T, t: f32) -> T
where
f32: std::ops::Mul<T, Output = T>,
T: std::ops::Add<T, Output = T>,
{
2018-12-26 16:01:46 +00:00
(1.0 - t) * min + t * max
}
2019-01-05 14:28:07 +00:00
pub fn remap(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 {
let t = (from - from_min) / (from_max - from_min);
lerp(to_min, to_max, t)
}
2018-12-26 16:01:46 +00:00
pub fn remap_clamp(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 {
let t = if from <= from_min {
0.0
} else if from >= from_max {
1.0
} else {
(from - from_min) / (from_max - from_min)
};
2018-12-26 22:08:50 +00:00
lerp(to_min, to_max, t)
2018-12-26 16:01:46 +00:00
}
2019-01-05 14:28:07 +00:00
2019-01-17 23:34:01 +00:00
pub fn clamp(x: f32, min: f32, max: f32) -> f32 {
if x <= min {
min
} else if x >= max {
max
} else {
x
}
}
2019-04-25 16:07:36 +00:00
/// For t=[0,1], returns [0,1] with a derivate of zero at both ends
pub fn ease_in_ease_out(t: f32) -> f32 {
return 3.0 * t * t - 2.0 * t * t * t;
}
2019-01-05 14:28:07 +00:00
pub const TAU: f32 = 2.0 * std::f32::consts::PI;