MCPcopy Create free account
hub / github.com/cpmech/plotpy / draw_cylinder

Method draw_cylinder

src/surface_geometry.rs:44–88  ·  view source on GitHub ↗

Draws a cylinder # Input `a` -- first point on the cylinder (centered) axis `b` -- second point on the cylinder (centered) axis `radius` -- the cylinder's radius `ndiv_axis` -- number of divisions along the axis (≥ 1) `ndiv_perimeter` -- number of divisions along the cross-sectional circle perimeter (≥ 3) # Example ``` use plotpy::{Plot, StrError, Surface}; use std::path::Path; fn main() -> R

(
        &mut self,
        a: &[f64],
        b: &[f64],
        radius: f64,
        ndiv_axis: usize,
        ndiv_perimeter: usize,
    )

Source from the content-addressed store, hash-verified

42 ///
43 /// See also integration tests in the [tests directory](https://github.com/cpmech/plotpy/tree/main/tests)
44 pub fn draw_cylinder(
45 &mut self,
46 a: &[f64],
47 b: &[f64],
48 radius: f64,
49 ndiv_axis: usize,
50 ndiv_perimeter: usize,
51 ) -> Result<(), StrError> {
52 if a.len() != 3 {
53 return Err("a.len() must equal to 3");
54 }
55 if b.len() != 3 {
56 return Err("b.len() must equal to 3");
57 }
58 if ndiv_axis < 1 {
59 return Err("ndiv_axis must be ≥ 1");
60 }
61 if ndiv_perimeter < 3 {
62 return Err("ndiv_perimeter must be ≥ 3");
63 }
64 let (e0, e1, e2) = Surface::aligned_system(a, b)?;
65 let cylinder_height =
66 f64::sqrt((b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1]) + (b[2] - a[2]) * (b[2] - a[2]));
67 let (n_height, n_alpha) = (ndiv_axis + 1, ndiv_perimeter + 1);
68 let mut x = vec![vec![0.0; n_height]; n_alpha];
69 let mut y = vec![vec![0.0; n_height]; n_alpha];
70 let mut z = vec![vec![0.0; n_height]; n_alpha];
71 let delta_height = cylinder_height / ((n_height - 1) as f64);
72 let delta_alpha = 2.0 * std::f64::consts::PI / ((n_alpha - 1) as f64);
73 let mut p = vec![0.0; 3];
74 for i in 0..n_alpha {
75 let v = (i as f64) * delta_alpha;
76 for j in 0..n_height {
77 let u = (j as f64) * delta_height;
78 for k in 0..3 {
79 p[k] = a[k] + u * e0[k] + radius * f64::sin(v) * e1[k] + radius * f64::cos(v) * e2[k];
80 }
81 x[i][j] = p[0];
82 y[i][j] = p[1];
83 z[i][j] = p[2];
84 }
85 }
86 self.draw(&x, &y, &z);
87 Ok(())
88 }
89
90 /// Draws a plane that has a normal vector with a non-zero z (nzz) component
91 ///

Callers 3

draw_cylinder_worksFunction · 0.80
test_surface_cylinderFunction · 0.80

Calls 1

drawMethod · 0.45

Tested by 3

draw_cylinder_worksFunction · 0.64
test_surface_cylinderFunction · 0.64