1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-28 07:23:13 -04:00

[emath] RectTransform: transforms Pos2 from one Rect to another

Very useful for transforming coordinate systems, e.g. for painting
This commit is contained in:
Emil Ernerfeldt
2021-02-14 10:33:44 +01:00
parent dbc6a620cd
commit c376d0bb7e
8 changed files with 142 additions and 24 deletions

View File

@@ -60,6 +60,7 @@ use std::ops::{Add, Div, Mul, RangeInclusive, Sub};
pub mod align;
mod pos2;
mod rect;
mod rect_transform;
mod rot2;
pub mod smart_aim;
mod vec2;
@@ -68,6 +69,7 @@ pub use {
align::{Align, Align2},
pos2::*,
rect::*,
rect_transform::*,
rot2::*,
vec2::*,
};

View File

@@ -202,6 +202,29 @@ impl Rect {
pub fn height(&self) -> f32 {
self.max.y - self.min.y
}
/// Width / height
///
/// * `aspect_ratio < 1`: portrait / high
/// * `aspect_ratio = 1`: square
/// * `aspect_ratio > 1`: landscape / wide
pub fn aspect_ratio(&self) -> f32 {
self.width() / self.height()
}
/// `[2, 1]` for wide screen, and `[1, 2]` for portrait, etc.
/// At least one dimension = 1, the other >= 1
/// Returns the proportions required to letter-box a square view area.
pub fn square_proportions(&self) -> Vec2 {
let w = self.width();
let h = self.height();
if w > h {
vec2(w / h, 1.0)
} else {
vec2(1.0, h / w)
}
}
pub fn area(&self) -> f32 {
self.width() * self.height()
}

View File

@@ -0,0 +1,73 @@
use crate::*;
/// Linearly transforms positions from one [`Rect`] to another.
///
/// `RectTransform` stores the rectangles, and therefore supports clamping and culling.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct RectTransform {
from: Rect,
to: Rect,
}
impl RectTransform {
pub fn identity(from_and_to: Rect) -> Self {
Self::from_to(from_and_to, from_and_to)
}
pub fn from_to(from: Rect, to: Rect) -> Self {
Self { from, to }
}
pub fn from(&self) -> &Rect {
&self.from
}
pub fn to(&self) -> &Rect {
&self.to
}
pub fn inverse(&self) -> RectTransform {
Self::from_to(self.to, self.from)
}
/// Transforms the given coordinate in the `from` space to the `to` space.
pub fn transform_pos(&self, pos: Pos2) -> Pos2 {
pos2(
remap(pos.x, self.from.x_range(), self.to.x_range()),
remap(pos.y, self.from.y_range(), self.to.y_range()),
)
}
/// Transforms the given rectangle in the `in`-space to a rectangle in the `out`-space.
pub fn transform_rect(&self, rect: Rect) -> Rect {
Rect {
min: self.transform_pos(rect.min),
max: self.transform_pos(rect.max),
}
}
/// Transforms the given coordinate in the `from` space to the `to` space,
/// clamping if necessary.
pub fn transform_pos_clamped(&self, pos: Pos2) -> Pos2 {
pos2(
remap_clamp(pos.x, self.from.x_range(), self.to.x_range()),
remap_clamp(pos.y, self.from.y_range(), self.to.y_range()),
)
}
}
/// Transforms the position.
impl std::ops::Mul<Pos2> for RectTransform {
type Output = Pos2;
fn mul(self, pos: Pos2) -> Pos2 {
self.transform_pos(pos)
}
}
/// Transforms the position.
impl std::ops::Mul<Pos2> for &RectTransform {
type Output = Pos2;
fn mul(self, pos: Pos2) -> Pos2 {
self.transform_pos(pos)
}
}

View File

@@ -8,7 +8,9 @@ use super::Vec2;
// `vec2(c,s)` represents where the X axis will end up after rotation.
//
/// Represents a rotation in the 2D plane.
//
/// A rotation of 𝞃/4 = 90° rotates the X axis to the Y axis.
//
/// Normally a `Rot2` is normalized (unit-length).
/// If not, it will also scale vectors.
#[derive(Clone, Copy, PartialEq)]
@@ -104,6 +106,7 @@ impl std::ops::Mul<Rot2> for Rot2 {
}
}
/// Rotates (and maybe scales) the vector.
impl std::ops::Mul<Vec2> for Rot2 {
type Output = Vec2;
fn mul(self, v: Vec2) -> Vec2 {
@@ -114,6 +117,7 @@ impl std::ops::Mul<Vec2> for Rot2 {
}
}
/// Scales the rotor.
impl std::ops::Mul<Rot2> for f32 {
type Output = Rot2;
fn mul(self, r: Rot2) -> Rot2 {
@@ -124,6 +128,7 @@ impl std::ops::Mul<Rot2> for f32 {
}
}
/// Scales the rotor.
impl std::ops::Mul<f32> for Rot2 {
type Output = Rot2;
fn mul(self, r: f32) -> Rot2 {
@@ -134,6 +139,7 @@ impl std::ops::Mul<f32> for Rot2 {
}
}
/// Scales the rotor.
impl std::ops::Div<f32> for Rot2 {
type Output = Rot2;
fn div(self, r: f32) -> Rot2 {