diff options
author | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-09 15:51:10 +0200 |
---|---|---|
committer | jakobst1n <jakob.stendahl@outlook.com> | 2024-06-09 15:51:10 +0200 |
commit | 0197c7069d9c814650cea8caf14731a39b8eca89 (patch) | |
tree | 51e16e5374c5f4ec1e35a775c9dd6541c8f3e062 /src/graph_canvas.rs | |
parent | 4e6a860b275abda39ade147ee7cdc48a3520212a (diff) | |
download | textgraph-0197c7069d9c814650cea8caf14731a39b8eca89.tar.gz textgraph-0197c7069d9c814650cea8caf14731a39b8eca89.zip |
Add some documentation
Diffstat (limited to 'src/graph_canvas.rs')
-rw-r--r-- | src/graph_canvas.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/graph_canvas.rs b/src/graph_canvas.rs index be5c4aa..884f597 100644 --- a/src/graph_canvas.rs +++ b/src/graph_canvas.rs @@ -29,21 +29,43 @@ impl std::fmt::Display for GraphPixel { } } +/// Temporary variables used while building a graph pub struct GraphCanvas<T> { + /// A array of pixels, this will ultimately be turned to a string, is initialized to width * height elements: Vec<T>, + /// Width of canvas width: usize, + /// Height of canvas height: usize, + /// Width of the area of the canvas left for the actual graph draw_width: usize, + /// Height of the area of the canvas left for the actual graph draw_height: usize, + /// x-offset for where the graph draw area begins col_offset: usize, + /// y-offset for where the graph draw area begins row_offset: usize, } impl<T: Clone + Default + std::fmt::Display> GraphCanvas<T> { + + /// Create a new canvas with desired width and height + /// + /// # Arguments + /// + /// * `width` - Width of the output canvas + /// * `height` - Height of the output canvas pub fn new(width: usize, height: usize) -> Self { GraphCanvas::new_default(T::default(), width, height) } + /// Create a new canvas with desired width, height, and default canvas pixel + /// + /// # Arguments + /// + /// * `default` - Pixel to use for the "background" of the canvas + /// * `width` - Width of the output canvas + /// * `height` - Height of the output canvas pub fn new_default(default: T, width: usize, height: usize) -> Self { GraphCanvas { elements: vec![default; width * height], @@ -56,6 +78,7 @@ impl<T: Clone + Default + std::fmt::Display> GraphCanvas<T> { } } + /// Turn canvas into a string pub fn to_string(&self) -> String { let mut out = String::with_capacity(self.height * (self.width + 1)); for (i, px) in self.elements.iter().enumerate() { @@ -67,6 +90,16 @@ impl<T: Clone + Default + std::fmt::Display> GraphCanvas<T> { out } + /// Add axis to the canvas and move graph drawing area inside axis + /// + /// # Arguments + /// + /// * `c1` - Horizontal axis lines + /// * `c2` - Vertical axis lines + /// * `c4` - Bottom left axis pixel + /// * `c5` - Top left axis pixel + /// * `c6` - Bottom right axis pixel + /// * `c7` - Top right axis pixel pub fn axis(&mut self, c1: T, c2: T, c3: T, c4: T, c5: T, c6: T) { if self.height < 2 || self.width < 2 { return; @@ -93,27 +126,44 @@ impl<T: Clone + Default + std::fmt::Display> GraphCanvas<T> { self.row_offset = 1; } + /// Width of drawable area of graph pub fn width(&self) -> usize { self.draw_width } + /// Total width of graph canvas pub fn full_width(&self) -> usize { self.width } + /// Height of drawable area of graph pub fn height(&self) -> usize { self.draw_height } + /// Total height of graph canvas pub fn full_height(&self) -> usize { self.height } + /// Set a pixel at a absolute position in the canvas + /// + /// # Argument + /// + /// * `x` - X-position of pixel + /// * `y` - Y-position of pixel + /// * `px` - The pixel to set pub fn set(&mut self, x: usize, y: usize, px: T) { let pos = y * self.width + x; self.elements[pos] = px; } + /// Get the absolite position of a character from a coordinate drawable part of the canvas + /// + /// # Argument + /// + /// * `x` - Relative X-position of pixel + /// * `y` - Relative Y-position of pixel fn element_position(&self, row: usize, col: usize) -> usize { (row + self.row_offset) * self.width + (col + self.col_offset) } |