Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for bar graphs. Helps with issue #98 #111

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 57 additions & 3 deletions src/coord/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::ops::Range;
use std::rc::Rc;

use super::{AsRangedCoord, Ranged};
use super::{AsRangedCoord, DiscreteRanged, Ranged};

/// The category coordinate
pub struct Category<T: PartialEq> {
Expand All @@ -22,10 +22,33 @@ impl<T: PartialEq> Clone for Category<T> {
}
}

impl<T: PartialEq> std::cmp::PartialEq for Category<T> {
fn eq(&self, other: &Self) -> bool {
self.name == other.name && self.elements == other.elements && self.idx == other.idx
}
}

impl<T: std::hash::Hash + Eq> std::hash::Hash for Category<T> {
fn hash<H>(&self, state: &mut H)
where
H: std::hash::Hasher,
{
self.name.hash(state);
self.idx.hash(state);
self.elements.iter().for_each(|ele| ele.hash(state));
}
}

impl<T: Eq> std::cmp::Eq for Category<T> {}

impl<T: PartialEq + fmt::Display> fmt::Debug for Category<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let element = &self.elements[self.idx as usize];
write!(f, "{}", element)
if self.idx < 0 {
write!(f, "{}", "NO VALUE")
} else {
let element = &self.elements[self.idx as usize];
write!(f, "{}", element)
}
}
}

Expand Down Expand Up @@ -172,6 +195,37 @@ impl<T: PartialEq> Ranged for Category<T> {
}
}

impl<T: PartialEq> DiscreteRanged for Category<T> {
type RangeParameter = ();

fn get_range_parameter(&self) {}

/// Get the smallest value that is larger than the `this` value
fn next_value(this: &Self::ValueType, _param: &()) -> Self::ValueType {
let mut out = this.clone();
out.idx = match out.idx {
// Any negative number (ie. -1) should return 0 (the start)
idx if idx < 0 => 0,
// If we've hit the end of the range, start again
idx if idx as usize >= out.elements.len() => 0,
// Otherwise just increase idx
idx => idx + 1,
};
out
}

/// Get the largest value that is smaller than `this` value
fn previous_value(this: &Self::ValueType, _param: &()) -> Self::ValueType {
let mut out = this.clone();
out.idx -= 1;
if out.idx < 0 {
// If we need to wrap around
out.idx = (out.elements.len() - 1) as i32;
}
out
}
}

impl<T: PartialEq> AsRangedCoord for Category<T> {
type CoordDescType = Self;
type Value = Category<T>;
Expand Down
20 changes: 19 additions & 1 deletion src/drawing/backend_impl/piston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ fn make_point_pair(a: BackendCoord, b: BackendCoord, scale: f64) -> [f64; 4] {
]
}

fn make_circle(center: BackendCoord, radius: u32, scale: f64) -> [f64; 4] {
circle(
center.0 as f64 * scale,
center.1 as f64 * scale,
radius as f64 * scale,
)
}

impl<'a, 'b> PistonBackend<'a, 'b> {
pub fn new(size: (u32, u32), scale: f64, context: Context, graphics: &'b mut G2d<'a>) -> Self {
Self {
Expand Down Expand Up @@ -150,7 +158,7 @@ impl<'a, 'b> DrawingBackend for PistonBackend<'a, 'b> {
style: &S,
fill: bool,
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
let rect = circle(center.0 as f64, center.1 as f64, radius as f64);
let rect = make_circle(center, radius, self.scale);
if fill {
ellipse(
make_piston_rgba(&style.as_color()),
Expand Down Expand Up @@ -204,3 +212,13 @@ pub fn draw_piston_window<F: FnOnce(PistonBackend) -> Result<(), Box<dyn std::er
}
None
}

#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_make_circle() {
assert_eq!(make_circle((1, 1), 0, 1.0), [1.0, 1.0, 0.0, 0.0]);
assert_eq!(make_circle((1, 2), 3, 4.0), [-8.0, -4.0, 24.0, 24.0]);
}
}