2020-04-25 09:11:44 +00:00
|
|
|
use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, RangeInclusive, Sub, SubAssign};
|
2020-04-21 18:48:31 +00:00
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
#[derive(Clone, Copy, Default, Deserialize, Serialize)]
|
2018-12-26 16:01:46 +00:00
|
|
|
pub struct Vec2 {
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn vec2(x: f32, y: f32) -> Vec2 {
|
|
|
|
Vec2 { x, y }
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 15:23:40 +00:00
|
|
|
impl Vec2 {
|
2020-04-25 12:37:39 +00:00
|
|
|
pub fn zero() -> Self {
|
|
|
|
Self { x: 0.0, y: 0.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn infinity() -> Self {
|
|
|
|
Self {
|
|
|
|
x: f32::INFINITY,
|
|
|
|
y: f32::INFINITY,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn splat(v: impl Into<f32>) -> Self {
|
2020-04-19 09:13:24 +00:00
|
|
|
let v: f32 = v.into();
|
2020-04-21 05:39:23 +00:00
|
|
|
Self { x: v, y: v }
|
2020-04-19 09:13:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 16:07:36 +00:00
|
|
|
#[must_use]
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn normalized(self) -> Self {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn rot90(self) -> Self {
|
2019-01-05 15:23:40 +00:00
|
|
|
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
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn dist(a: Self, b: Self) -> f32 {
|
2019-04-25 16:07:36 +00:00
|
|
|
(a - b).length()
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn dist_sq(a: Self, b: Self) -> f32 {
|
2019-04-25 16:07:36 +00:00
|
|
|
(a - b).length_sq()
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn angled(angle: f32) -> Self {
|
2019-04-25 16:07:36 +00:00
|
|
|
vec2(angle.cos(), angle.sin())
|
|
|
|
}
|
2020-04-19 21:34:34 +00:00
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-19 21:34:34 +00:00
|
|
|
pub fn floor(self) -> Self {
|
|
|
|
vec2(self.x.floor(), self.y.floor())
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-19 21:34:34 +00:00
|
|
|
pub fn round(self) -> Self {
|
|
|
|
vec2(self.x.round(), self.y.round())
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-19 21:34:34 +00:00
|
|
|
pub fn ceil(self) -> Self {
|
|
|
|
vec2(self.x.ceil(), self.y.ceil())
|
|
|
|
}
|
|
|
|
|
2020-04-24 16:47:14 +00:00
|
|
|
pub fn is_finite(self) -> bool {
|
2020-04-20 22:17:02 +00:00
|
|
|
self.x.is_finite() && self.y.is_finite()
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn min(self, other: Self) -> Self {
|
2020-04-19 21:34:34 +00:00
|
|
|
vec2(self.x.min(other.x), self.y.min(other.y))
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn max(self, other: Self) -> Self {
|
2020-04-19 21:34:34 +00:00
|
|
|
vec2(self.x.max(other.x), self.y.max(other.y))
|
|
|
|
}
|
2020-04-25 12:37:39 +00:00
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn clamp(self, range: RangeInclusive<Self>) -> Self {
|
|
|
|
Self {
|
|
|
|
x: clamp(self.x, range.start().x..=range.end().x),
|
|
|
|
y: clamp(self.y, range.start().y..=range.end().y),
|
|
|
|
}
|
|
|
|
}
|
2019-01-05 15:23:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
impl PartialEq for Vec2 {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.x == other.x && self.y == other.y
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl Eq for Vec2 {}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Neg for Vec2 {
|
2019-11-02 08:50:49 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
|
|
|
|
fn neg(self) -> Vec2 {
|
|
|
|
vec2(-self.x, -self.y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl 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
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl SubAssign for Vec2 {
|
|
|
|
fn sub_assign(&mut self, rhs: Vec2) {
|
|
|
|
*self = Vec2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add for Vec2 {
|
2018-12-26 16:01:46 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn add(self, rhs: Vec2) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x + rhs.x,
|
|
|
|
y: self.y + rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Sub for Vec2 {
|
2018-12-26 16:01:46 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn sub(self, rhs: Vec2) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl MulAssign<f32> for Vec2 {
|
2019-01-19 16:10:28 +00:00
|
|
|
fn mul_assign(&mut self, rhs: f32) {
|
|
|
|
self.x *= rhs;
|
|
|
|
self.y *= rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Mul<f32> for Vec2 {
|
2018-12-26 16:01:46 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn mul(self, factor: f32) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x * factor,
|
|
|
|
y: self.y * factor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Mul<Vec2> for f32 {
|
2018-12-27 22:26:05 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn mul(self, vec: Vec2) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self * vec.x,
|
|
|
|
y: self * vec.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Div<f32> for Vec2 {
|
2019-01-05 15:23:40 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn div(self, factor: f32) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x / factor,
|
|
|
|
y: self.y / factor,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
impl std::fmt::Debug for Vec2 {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "[{} {}]", self.x, self.y)
|
|
|
|
}
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 16:09:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
/// Sometimes called a Point. I prefer the shorter Pos2 so it is equal length to Vec2
|
2020-04-21 05:39:23 +00:00
|
|
|
#[derive(Clone, Copy, Default, Deserialize, Serialize)]
|
2020-04-18 23:05:49 +00:00
|
|
|
pub struct Pos2 {
|
|
|
|
pub x: f32,
|
|
|
|
pub y: f32,
|
|
|
|
// implicit w = 1
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn pos2(x: f32, y: f32) -> Pos2 {
|
|
|
|
Pos2 { x, y }
|
2020-04-20 21:33:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
impl Pos2 {
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn dist(self: Self, other: Self) -> f32 {
|
2020-04-19 09:13:24 +00:00
|
|
|
(self - other).length()
|
2020-04-18 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn dist_sq(self: Self, other: Self) -> f32 {
|
2020-04-19 09:13:24 +00:00
|
|
|
(self - other).length_sq()
|
2020-04-18 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 21:51:38 +00:00
|
|
|
pub fn floor(self) -> Self {
|
|
|
|
pos2(self.x.floor(), self.y.floor())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn round(self) -> Self {
|
|
|
|
pos2(self.x.round(), self.y.round())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn ceil(self) -> Self {
|
|
|
|
pos2(self.x.ceil(), self.y.ceil())
|
|
|
|
}
|
2020-04-20 22:17:02 +00:00
|
|
|
|
2020-04-24 16:47:14 +00:00
|
|
|
pub fn is_finite(self) -> bool {
|
2020-04-20 22:17:02 +00:00
|
|
|
self.x.is_finite() && self.y.is_finite()
|
|
|
|
}
|
2020-04-21 05:39:23 +00:00
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn min(self, other: Self) -> Self {
|
|
|
|
pos2(self.x.min(other.x), self.y.min(other.y))
|
|
|
|
}
|
|
|
|
|
2020-04-25 12:37:39 +00:00
|
|
|
#[must_use]
|
2020-04-21 05:39:23 +00:00
|
|
|
pub fn max(self, other: Self) -> Self {
|
|
|
|
pos2(self.x.max(other.x), self.y.max(other.y))
|
|
|
|
}
|
2020-04-25 12:37:39 +00:00
|
|
|
|
|
|
|
#[must_use]
|
|
|
|
pub fn clamp(self, range: RangeInclusive<Self>) -> Self {
|
|
|
|
Self {
|
|
|
|
x: clamp(self.x, range.start().x..=range.end().x),
|
|
|
|
y: clamp(self.y, range.start().y..=range.end().y),
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 05:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for Pos2 {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.x == other.x && self.y == other.y
|
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
}
|
2020-04-21 05:39:23 +00:00
|
|
|
impl Eq for Pos2 {}
|
2020-04-18 23:05:49 +00:00
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl AddAssign<Vec2> for Pos2 {
|
2020-04-18 23:05:49 +00:00
|
|
|
fn add_assign(&mut self, rhs: Vec2) {
|
|
|
|
*self = Pos2 {
|
|
|
|
x: self.x + rhs.x,
|
|
|
|
y: self.y + rhs.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl SubAssign<Vec2> for Pos2 {
|
|
|
|
fn sub_assign(&mut self, rhs: Vec2) {
|
|
|
|
*self = Pos2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<Vec2> for Pos2 {
|
2020-04-18 23:05:49 +00:00
|
|
|
type Output = Pos2;
|
|
|
|
fn add(self, rhs: Vec2) -> Pos2 {
|
|
|
|
Pos2 {
|
|
|
|
x: self.x + rhs.x,
|
|
|
|
y: self.y + rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Sub for Pos2 {
|
2020-04-18 23:05:49 +00:00
|
|
|
type Output = Vec2;
|
|
|
|
fn sub(self, rhs: Pos2) -> Vec2 {
|
|
|
|
Vec2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
impl Sub<Vec2> for Pos2 {
|
2020-04-18 23:05:49 +00:00
|
|
|
type Output = Pos2;
|
|
|
|
fn sub(self, rhs: Vec2) -> Pos2 {
|
|
|
|
Pos2 {
|
|
|
|
x: self.x - rhs.x,
|
|
|
|
y: self.y - rhs.y,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
impl std::fmt::Debug for Pos2 {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "[{} {}]", self.x, self.y)
|
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
#[derive(Clone, Copy, Default, Eq, PartialEq, Deserialize, Serialize)]
|
2018-12-26 16:01:46 +00:00
|
|
|
pub struct Rect {
|
2020-04-25 13:45:38 +00:00
|
|
|
pub min: Pos2,
|
|
|
|
pub max: Pos2,
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Rect {
|
2020-04-20 21:33:16 +00:00
|
|
|
/// Infinite rectangle that contains everything
|
|
|
|
pub fn everything() -> Self {
|
2020-04-25 08:52:20 +00:00
|
|
|
let inf = f32::INFINITY;
|
2020-04-20 21:33:16 +00:00
|
|
|
Self {
|
|
|
|
min: pos2(-inf, -inf),
|
|
|
|
max: pos2(inf, inf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn nothing() -> Self {
|
2020-04-25 08:52:20 +00:00
|
|
|
let inf = f32::INFINITY;
|
2020-04-20 21:33:16 +00:00
|
|
|
Self {
|
|
|
|
min: pos2(inf, inf),
|
|
|
|
max: pos2(-inf, -inf),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn from_min_max(min: Pos2, max: Pos2) -> Self {
|
2020-04-24 16:47:14 +00:00
|
|
|
Rect { min, max }
|
2020-04-15 09:47:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn from_min_size(min: Pos2, size: Vec2) -> Self {
|
2020-04-15 09:47:03 +00:00
|
|
|
Rect {
|
|
|
|
min,
|
|
|
|
max: min + size,
|
|
|
|
}
|
2018-12-26 22:08:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn from_center_size(center: Pos2, size: Vec2) -> Self {
|
2018-12-26 16:01:46 +00:00
|
|
|
Rect {
|
2020-04-15 09:47:03 +00:00
|
|
|
min: center - size * 0.5,
|
|
|
|
max: center + size * 0.5,
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 06:15:20 +00:00
|
|
|
/// Expand by this much in each direction, keeping the center
|
2020-04-21 18:48:31 +00:00
|
|
|
#[must_use]
|
2019-03-16 14:14:22 +00:00
|
|
|
pub fn expand(self, amnt: f32) -> Self {
|
|
|
|
Rect::from_center_size(self.center(), self.size() + 2.0 * vec2(amnt, amnt))
|
|
|
|
}
|
2020-04-19 09:13:24 +00:00
|
|
|
|
2020-05-05 06:15:20 +00:00
|
|
|
/// Expand by this much in each direction, keeping the center
|
|
|
|
#[must_use]
|
|
|
|
pub fn expand2(self, amnt: Vec2) -> Self {
|
|
|
|
Rect::from_center_size(self.center(), self.size() + 2.0 * amnt)
|
|
|
|
}
|
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
#[must_use]
|
2020-04-17 12:26:36 +00:00
|
|
|
pub fn translate(self, amnt: Vec2) -> Self {
|
2020-04-25 13:45:38 +00:00
|
|
|
Rect::from_min_size(self.min + amnt, self.size())
|
2020-04-17 12:26:36 +00:00
|
|
|
}
|
2019-03-16 14:14:22 +00:00
|
|
|
|
2020-04-21 18:48:31 +00:00
|
|
|
#[must_use]
|
2020-04-26 20:30:24 +00:00
|
|
|
pub fn intersect(self, other: &Rect) -> Self {
|
2020-04-21 05:39:23 +00:00
|
|
|
Self {
|
|
|
|
min: self.min.max(other.min),
|
|
|
|
max: self.max.min(other.max),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:17:37 +00:00
|
|
|
/// keep min
|
2020-04-19 22:48:54 +00:00
|
|
|
pub fn set_width(&mut self, w: f32) {
|
|
|
|
self.max.x = self.min.x + w;
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:17:37 +00:00
|
|
|
/// keep min
|
2020-04-19 22:48:54 +00:00
|
|
|
pub fn set_height(&mut self, h: f32) {
|
|
|
|
self.max.y = self.min.y + h;
|
|
|
|
}
|
|
|
|
|
2020-04-22 22:17:37 +00:00
|
|
|
/// Keep size
|
|
|
|
pub fn set_center(&mut self, center: Pos2) {
|
|
|
|
*self = self.translate(center - self.center());
|
|
|
|
}
|
|
|
|
|
2020-04-25 13:46:50 +00:00
|
|
|
#[must_use]
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn contains(&self, p: Pos2) -> bool {
|
2020-04-15 09:47:03 +00:00
|
|
|
self.min.x <= p.x
|
|
|
|
&& p.x <= self.min.x + self.size().x
|
|
|
|
&& self.min.y <= p.y
|
|
|
|
&& p.y <= self.min.y + self.size().y
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 13:46:50 +00:00
|
|
|
pub fn extend_with(&mut self, p: Pos2) {
|
|
|
|
self.min = self.min.min(p);
|
|
|
|
self.max = self.max.max(p);
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn center(&self) -> Pos2 {
|
|
|
|
Pos2 {
|
2020-04-15 09:47:03 +00:00
|
|
|
x: self.min.x + self.size().x / 2.0,
|
|
|
|
y: self.min.y + self.size().y / 2.0,
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 13:26:02 +00:00
|
|
|
pub fn size(&self) -> Vec2 {
|
2020-04-15 09:47:03 +00:00
|
|
|
self.max - self.min
|
2019-01-14 13:26:02 +00:00
|
|
|
}
|
2019-02-10 19:56:59 +00:00
|
|
|
pub fn width(&self) -> f32 {
|
2020-04-15 09:47:03 +00:00
|
|
|
self.max.x - self.min.x
|
2019-02-10 19:56:59 +00:00
|
|
|
}
|
|
|
|
pub fn height(&self) -> f32 {
|
2020-04-15 09:47:03 +00:00
|
|
|
self.max.y - self.min.y
|
2019-02-10 19:56:59 +00:00
|
|
|
}
|
2019-01-19 16:09:00 +00:00
|
|
|
|
2020-04-25 09:11:44 +00:00
|
|
|
pub fn range_x(&self) -> RangeInclusive<f32> {
|
|
|
|
self.min.x..=self.max.x
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn range_y(&self) -> RangeInclusive<f32> {
|
|
|
|
self.min.y..=self.max.y
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:17:02 +00:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.max.x < self.min.x || self.max.y < self.min.y
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_finite(&self) -> bool {
|
|
|
|
self.min.is_finite() && self.max.is_finite()
|
|
|
|
}
|
|
|
|
|
2020-04-15 09:47:03 +00:00
|
|
|
// Convenience functions (assumes origin is towards left top):
|
2020-04-22 17:38:38 +00:00
|
|
|
pub fn left(&self) -> f32 {
|
|
|
|
self.min.x
|
|
|
|
}
|
|
|
|
pub fn right(&self) -> f32 {
|
|
|
|
self.max.x
|
|
|
|
}
|
|
|
|
pub fn top(&self) -> f32 {
|
|
|
|
self.min.y
|
|
|
|
}
|
|
|
|
pub fn bottom(&self) -> f32 {
|
|
|
|
self.max.y
|
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn left_top(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.left(), self.top())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn center_top(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.center().x, self.top())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn right_top(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.right(), self.top())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn left_center(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.left(), self.center().y)
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn right_center(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.right(), self.center().y)
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn left_bottom(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.left(), self.bottom())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn center_bottom(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.center().x, self.bottom())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2020-04-18 23:05:49 +00:00
|
|
|
pub fn right_bottom(&self) -> Pos2 {
|
2020-04-22 17:38:38 +00:00
|
|
|
pos2(self.right(), self.bottom())
|
2019-01-19 16:09:00 +00:00
|
|
|
}
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 05:39:23 +00:00
|
|
|
impl std::fmt::Debug for Rect {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "[{:?} - {:?}]", self.min, self.max)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 16:09:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2020-04-25 09:14:32 +00:00
|
|
|
pub fn lerp<T>(range: RangeInclusive<T>, t: f32) -> T
|
2019-04-25 16:07:36 +00:00
|
|
|
where
|
2020-04-21 18:48:31 +00:00
|
|
|
f32: Mul<T, Output = T>,
|
2020-04-25 09:14:32 +00:00
|
|
|
T: Add<T, Output = T> + Copy,
|
2019-04-25 16:07:36 +00:00
|
|
|
{
|
2020-04-25 09:14:32 +00:00
|
|
|
(1.0 - t) * *range.start() + t * *range.end()
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:11:44 +00:00
|
|
|
pub fn remap(x: f32, from: RangeInclusive<f32>, to: RangeInclusive<f32>) -> f32 {
|
|
|
|
let t = (x - from.start()) / (from.end() - from.start());
|
2020-04-25 09:14:32 +00:00
|
|
|
lerp(to, t)
|
2019-01-05 14:28:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:11:44 +00:00
|
|
|
pub fn remap_clamp(x: f32, from: RangeInclusive<f32>, to: RangeInclusive<f32>) -> f32 {
|
|
|
|
let t = if x <= *from.start() {
|
2018-12-26 16:01:46 +00:00
|
|
|
0.0
|
2020-04-25 09:11:44 +00:00
|
|
|
} else if x >= *from.end() {
|
2018-12-26 16:01:46 +00:00
|
|
|
1.0
|
|
|
|
} else {
|
2020-04-25 09:11:44 +00:00
|
|
|
(x - from.start()) / (from.end() - from.start())
|
2018-12-26 16:01:46 +00:00
|
|
|
};
|
2020-04-25 09:14:32 +00:00
|
|
|
lerp(to, t)
|
2018-12-26 16:01:46 +00:00
|
|
|
}
|
2019-01-05 14:28:07 +00:00
|
|
|
|
2020-04-25 09:14:32 +00:00
|
|
|
pub fn clamp(x: f32, range: RangeInclusive<f32>) -> f32 {
|
|
|
|
if x <= *range.start() {
|
|
|
|
*range.start()
|
|
|
|
} else if x >= *range.end() {
|
|
|
|
*range.end()
|
2019-01-17 23:34:01 +00:00
|
|
|
} 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 {
|
2020-04-24 16:47:14 +00:00
|
|
|
3.0 * t * t - 2.0 * t * t * t
|
2019-04-25 16:07:36 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 14:28:07 +00:00
|
|
|
pub const TAU: f32 = 2.0 * std::f32::consts::PI;
|