1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 13:20:05 -04:00

A simple 2D plot library

This commit is contained in:
Emil Ernerfeldt
2021-02-14 21:39:04 +01:00
parent 7dad76b913
commit a19140ec67
14 changed files with 1039 additions and 11 deletions

View File

@@ -8,7 +8,7 @@ use crate::*;
///
/// Mathematically this is known as a "point", but the term position was chosen so not to
/// conflict with the unit (one point = X physical pixels).
#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Pos2 {
pub x: f32,
@@ -145,11 +145,27 @@ impl Pos2 {
}
}
impl PartialEq for Pos2 {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
impl std::ops::Index<usize> for Pos2 {
type Output = f32;
fn index(&self, index: usize) -> &f32 {
match index {
0 => &self.x,
1 => &self.y,
_ => panic!("Pos2 index out of bounds: {}", index),
}
}
}
impl std::ops::IndexMut<usize> for Pos2 {
fn index_mut(&mut self, index: usize) -> &mut f32 {
match index {
0 => &mut self.x,
1 => &mut self.y,
_ => panic!("Pos2 index out of bounds: {}", index),
}
}
}
impl Eq for Pos2 {}
impl AddAssign<Vec2> for Pos2 {

View File

@@ -180,6 +180,18 @@ impl Rect {
self.max = self.max.max(p);
}
/// Expand to include the given x coordinate
pub fn extend_with_x(&mut self, x: f32) {
self.min.x = self.min.x.min(x);
self.max.x = self.max.x.max(x);
}
/// Expand to include the given y coordinate
pub fn extend_with_y(&mut self, y: f32) {
self.min.y = self.min.y.min(y);
self.max.y = self.max.y.max(y);
}
pub fn union(self, other: Rect) -> Rect {
Rect {
min: self.min.min(other.min),

View File

@@ -26,6 +26,11 @@ impl RectTransform {
&self.to
}
/// The scale factors.
pub fn scale(&self) -> Vec2 {
self.to.size() / self.from.size()
}
pub fn inverse(&self) -> RectTransform {
Self::from_to(self.to, self.from)
}

View File

@@ -8,7 +8,7 @@ use crate::*;
/// emath represents positions using [`Pos2`].
///
/// Normally the units are points (logical pixels).
#[derive(Clone, Copy, Default)]
#[derive(Clone, Copy, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Vec2 {
pub x: f32,
@@ -186,11 +186,27 @@ impl Vec2 {
}
}
impl PartialEq for Vec2 {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
impl std::ops::Index<usize> for Vec2 {
type Output = f32;
fn index(&self, index: usize) -> &f32 {
match index {
0 => &self.x,
1 => &self.y,
_ => panic!("Vec2 index out of bounds: {}", index),
}
}
}
impl std::ops::IndexMut<usize> for Vec2 {
fn index_mut(&mut self, index: usize) -> &mut f32 {
match index {
0 => &mut self.x,
1 => &mut self.y,
_ => panic!("Vec2 index out of bounds: {}", index),
}
}
}
impl Eq for Vec2 {}
impl Neg for Vec2 {
@@ -239,6 +255,28 @@ impl Sub for Vec2 {
}
}
/// Element-wise multiplication
impl Mul<Vec2> for Vec2 {
type Output = Vec2;
fn mul(self, vec: Vec2) -> Vec2 {
Vec2 {
x: self.x * vec.x,
y: self.y * vec.y,
}
}
}
/// Element-wise division
impl Div<Vec2> for Vec2 {
type Output = Vec2;
fn div(self, rhs: Vec2) -> Vec2 {
Vec2 {
x: self.x / rhs.x,
y: self.y / rhs.y,
}
}
}
impl MulAssign<f32> for Vec2 {
fn mul_assign(&mut self, rhs: f32) {
self.x *= rhs;