dstv/
bend.rs

1use crate::dstv_element::ParseDstvError;
2use crate::get_f64_from_str;
3use crate::prelude::DstvElement;
4
5/// A bend is a circular arc.
6/// It is defined by the angle of the arc, the radius of the arc, and the start and end points of the arc.
7/// The start point is the origin of the arc.
8/// The end point is the point where the arc ends.
9/// The arc is drawn counter-clockwise from the origin to the end point.
10/// The arc is drawn clockwise from the end point to the origin.
11#[derive(Debug)]
12pub struct Bend {
13    /// The angle of the arc in degrees.
14    pub angle: f64,
15    /// The radius of the arc.
16    pub radius: f64,
17    /// The x-coordinate of the end point of the arc.
18    pub finish_x: f64,
19    /// The y-coordinate of the end point of the arc.
20    pub finish_y: f64,
21    /// The x-coordinate of the origin of the arc.
22    pub origin_x: f64,
23    /// The y-coordinate of the origin of the arc.
24    pub origin_y: f64,
25}
26
27impl DstvElement for Bend {
28    /// Create a new bend from a string slice.
29    /// The string slice is expected to be a line from a DSTV file.
30    /// # Arguments
31    /// * `line` - A string slice containing a line from a DSTV file.
32    /// # Returns
33    /// A Result containing either a Bend or a &'static str.
34    fn from_str(line: &str) -> Result<Self, ParseDstvError> {
35        let mut iter = line.split_whitespace();
36        let origin_x = get_f64_from_str(iter.next(), "origin_x")?;
37        let origin_y = get_f64_from_str(iter.next(), "origin_y")?;
38        let angle = get_f64_from_str(iter.next(), "angle")?;
39        let radius = get_f64_from_str(iter.next(), "radius")?;
40        let finish_x = get_f64_from_str(iter.next(), "finish_x")?;
41        let finish_y = get_f64_from_str(iter.next(), "finish_y")?;
42        Ok(Self {
43            angle,
44            radius,
45            finish_x,
46            finish_y,
47            origin_x,
48            origin_y,
49        })
50    }
51
52    fn get_index(&self) -> usize {
53        2
54    }
55
56    fn get_facing(&self) -> &crate::prelude::PartFace {
57        &crate::prelude::PartFace::Top
58    }
59
60    /// Convert the bend to an SVG path.
61    /// # Returns
62    /// A string containing an SVG path.
63    fn to_svg(&self) -> String {
64        format!(
65            "<path d=\"M{},{} A{},{},0,0,1,{},{}\" stroke=\"black\" fill=\"none\" />",
66            self.origin_x, self.origin_y, self.radius, self.radius, self.finish_x, self.finish_y
67        )
68    }
69}