packages

plot::path

On this page

Vector paths in pixel coordinates (y down): moves, lines, quadratic and cubic Beziers, and closes. The SVG backend writes them as they are; the rasterizer and the terminal flatten them to polylines.

Structs

PathSource

pub struct Path

A path. Its point lists are shared by copies, so build it in one place and hand it over.

nabi
use plot::Path;

let p = Path::new();
p.move_to(0.0, 0.0);
p.line_to(10.0, 0.0);
p.close();
assert(p.len() == 3);
  • pub fn new() -> Path§

    Source

    An empty path.

    nabi
    assert(plot::Path::new().is_empty());
    
  • pub fn len(self) -> usize§

    Source

    The number of commands.

    nabi
    assert(plot::Path::rect(0.0, 0.0, 1.0, 1.0).len() == 5);
    
  • pub fn is_empty(self) -> bool§

    Source

    Whether the path has no commands.

    nabi
    assert(!plot::Path::circle(0.0, 0.0, 1.0).is_empty());
    
  • pub fn move_to(self, x: f64, y: f64)§

    Source

    Starts a subpath at (x, y).

    nabi
    let p = plot::Path::new();
    p.move_to(1.0, 2.0);
    assert(p.bounds() == (1.0, 2.0, 1.0, 2.0));
    
  • pub fn line_to(self, x: f64, y: f64)§

    Source

    A straight line to (x, y).

    nabi
    let p = plot::Path::new();
    p.move_to(0.0, 0.0);
    p.line_to(3.0, 4.0);
    assert(p.bounds().3 == 4.0);
    
  • pub fn quad_to(self, cx: f64, cy: f64, x: f64, y: f64)§

    Source

    A quadratic Bezier through control (cx, cy) to (x, y).

    nabi
    let p = plot::Path::new();
    p.move_to(0.0, 0.0);
    p.quad_to(1.0, 2.0, 2.0, 0.0);
    assert(p.len() == 2);
    
  • pub fn cubic_to(self, c1x: f64, c1y: f64, c2x: f64, c2y: f64, x: f64, y: f64)§

    Source

    A cubic Bezier through controls (c1x, c1y), (c2x, c2y) to (x, y).

    nabi
    let p = plot::Path::new();
    p.move_to(0.0, 0.0);
    p.cubic_to(0.0, 1.0, 1.0, 1.0, 1.0, 0.0);
    assert(p.len() == 2);
    
  • pub fn close(self)§

    Source

    Closes the current subpath.

    nabi
    let p = plot::Path::new();
    p.move_to(0.0, 0.0);
    p.close();
    assert(p.len() == 2);
    
  • pub fn rect(x: f64, y: f64, w: f64, h: f64) -> Path§

    Source

    An axis-aligned rectangle, clockwise on screen.

    nabi
    assert(plot::Path::rect(1.0, 2.0, 3.0, 4.0).bounds() == (1.0, 2.0, 4.0, 6.0));
    
  • pub fn add_rect(self, x: f64, y: f64, w: f64, h: f64)§

    Source

    Adds a rectangle subpath.

    nabi
    let p = plot::Path::new();
    p.add_rect(0.0, 0.0, 1.0, 1.0);
    p.add_rect(2.0, 0.0, 1.0, 1.0);
    assert(p.len() == 10);
    
  • pub fn circle(cx: f64, cy: f64, r: f64) -> Path§

    Source

    A circle of four cubic arcs.

    nabi
    assert(plot::Path::circle(0.0, 0.0, 2.0).bounds() == (-2.0, -2.0, 2.0, 2.0));
    
  • pub fn add_circle(self, cx: f64, cy: f64, r: f64)§

    Source

    Adds a circle subpath, clockwise on screen.

    nabi
    let p = plot::Path::new();
    p.add_circle(5.0, 5.0, 1.0);
    assert(p.len() == 6);
    
  • pub fn add_polygon(self, xs: List<f64>, ys: List<f64>)§

    Source

    Adds a closed polygon through the points.

    nabi
    let p = plot::Path::new();
    p.add_polygon([0.0, 1.0, 0.0], [0.0, 0.0, 1.0]);
    assert(p.len() == 4);
    
  • pub fn polyline(xs: List<f64>, ys: List<f64>) -> Path§

    Source

    An open polyline through the points; a NaN breaks it into pieces.

    nabi
    assert(plot::Path::polyline([0.0, 1.0, f64::NAN, 3.0], [0.0, 1.0, 2.0, 3.0]).len() == 3);
    
  • pub fn transformed(self, sx: f64, sy: f64, deg: f64, dx: f64, dy: f64) -> Path§

    Source

    The path moved by (dx, dy) after scaling by (sx, sy) and rotating by deg degrees clockwise on screen around the origin.

    nabi
    let p = plot::Path::rect(0.0, 0.0, 1.0, 1.0).transformed(2.0, 2.0, 0.0, 10.0, 0.0);
    assert(p.bounds() == (10.0, 0.0, 12.0, 2.0));
    
  • pub fn extend(self, o: Path)§

    Source

    Appends every command of o.

    nabi
    let p = plot::Path::rect(0.0, 0.0, 1.0, 1.0);
    p.extend(plot::Path::circle(3.0, 3.0, 1.0));
    assert(p.len() == 11);
    
  • pub fn bounds(self) -> (f64, f64, f64, f64)§

    Source

    (min x, min y, max x, max y) of the points, controls included.

    nabi
    assert(plot::Path::rect(-1.0, 0.0, 2.0, 1.0).bounds().0 == -1.0);