1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -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::*;
/// 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;