1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use crate::Shape;

pub struct Arc {
    pub x1: f32,
    pub y1: f32,
    pub x2: f32,
    pub y2: f32,
    pub radius: f32,
    pub sweep_flag: bool,
}

impl Arc {
    pub fn new(
        x1: f32,
        y1: f32,
        x2: f32,
        y2: f32,
        radius: f32,
        sweep_flag: bool,
    ) -> Self {
        Arc {
            x1,
            y1,
            x2,
            y2,
            radius,
            sweep_flag,
        }
    }

    /// calculate the center of this arc given start point, end point, radius and sweep direction
    pub fn center(&self) -> (f32, f32) {
        let q = ((self.x2 - self.x1).powf(2.0) + (self.y2 - self.y1).powf(2.0))
            .sqrt();
        let y3 = (self.y1 + self.y2) / 2.0;
        let x3 = (self.x1 + self.x2) / 2.0;

        let rr_q22 = (self.radius.powf(2.0) - (q / 2.0).powf(2.0)).sqrt();

        let base_x = rr_q22 * (self.y1 - self.y2) / q;
        let base_y = rr_q22 * (self.x2 - self.x1) / q;

        if self.sweep_flag {
            let cx = x3 + base_x;
            let cy = y3 + base_y;
            (cx, cy)
        } else {
            let cx = x3 - base_x;
            let cy = y3 - base_y;
            (cx, cy)
        }
    }

    /// which octant range the arc lies
    fn octant(&self) -> (u8, u8) {
        let (cx, cy) = self.center();
        let o1 = Self::line_octant(cx, cy, self.x1, self.y1);
        let o2 = Self::line_octant(cx, cy, self.x2, self.y2);
        (o1 + 1, o2)
    }

    /// calculate the octant of a line
    fn line_octant(x1: f32, y1: f32, x2: f32, y2: f32) -> u8 {
        let mut dx = x2 - x1;
        let mut dy = -(y2 * 2.0 - y1 * 2.0);

        let mut octant = 0;

        if dy < 0.0 {
            dx = -dx;
            dy = -dy;
            octant += 4;
        }

        if dx < 0.0 {
            let tmp = dx;
            dx = dy;
            dy = -tmp;
            octant += 2
        }

        if dx < dy {
            octant += 1
        }
        octant
    }
}

impl<'a> Shape<'a> for Arc {
    fn points(&'a self) -> Box<dyn Iterator<Item = (f32, f32)> + 'a> {
        let mut x = self.radius;
        let mut y = 0.0;
        let mut err = 0.0;

        let inc = 0.25;

        let mut points = vec![];

        let (cx, cy) = self.center();
        let (o1, o2) = self.octant();

        while x >= y {
            if (o1..=o2).contains(&7) {
                points.push((cx + x, cy + y));
            }
            if (o1..=o2).contains(&6) {
                points.push((cx + y, cy + x));
            }
            if (o1..=o2).contains(&5) {
                points.push((cx - y, cy + x));
            }
            if (o1..=o2).contains(&4) {
                points.push((cx - x, cy + y));
            }
            if (o1..=o2).contains(&3) {
                points.push((cx - x, cy - y));
            }
            if (o1..=o2).contains(&2) {
                points.push((cx - y, cy - x));
            }
            if (o1..=o2).contains(&1) {
                points.push((cx + y, cy - x));
            }
            if (o1..=o2).contains(&0) {
                points.push((cx + x, cy - y));
            }

            if err <= 0.0 {
                y += inc;
                err += 2.0 * y + inc;
            }

            if err > 0.0 {
                x -= inc;
                err -= 2.0 * x + inc;
            }
        }
        Box::new(points.into_iter())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::*;

    #[test]
    fn arc_center() {
        let arc = Arc {
            x1: 0.0,
            y1: 0.0,
            x2: 10.0,
            y2: 10.0,
            radius: 10.0,
            sweep_flag: false,
        };

        let center = arc.center();
        assert_eq!(center, (10.0, 0.0));
    }

    #[test]
    fn arc_center2() {
        let arc = Arc {
            x1: 0.0,
            y1: 0.0,
            x2: 10.0,
            y2: 10.0,
            radius: 10.0,
            sweep_flag: true,
        };

        let center = arc.center();
        assert_eq!(center, (0.0, 10.0));
    }

    #[test]
    fn draw_arc() {
        let width = 11.0;
        let height = 11.0;
        let mut context = Context::new(width, height);
        let arc = Arc {
            x1: 0.0,
            y1: 0.0,
            x2: 10.0,
            y2: 10.0,
            radius: 10.0,
            sweep_flag: false,
        };

        context.draw(&arc);

        let result = context.to_string();
        println!("{}", result);
        let expected = [
            "⢱                     ",
            "⢸                     ",
            " ⡇                    ",
            " ⠸⡀                   ",
            "  ⠱⡀                  ",
            "   ⠑⡄                 ",
            "    ⠈⠢⡀               ",
            "      ⠈⠢⡀             ",
            "        ⠈⠑⠤⣀          ",
            "            ⠉⠒⠢⠤⢄⣀⣀⣀  ",
            "                    ⠁ ",
        ];
        let expected = expected.join("\n");
        assert_eq!(result, expected);
    }

    #[test]
    fn draw_arc2() {
        let width = 22.0;
        let height = 22.0;
        let mut context = Context::new(width, height);
        let arc = Arc {
            x1: 10.0,
            y1: 0.0,
            x2: 10.0,
            y2: 20.0,
            radius: 10.0,
            sweep_flag: false,
        };

        context.draw(&arc);

        let result = context.to_string();
        println!("{}", result);
        let expected = [
            "             ⣀⡠⠤⠔⠒⠒⠒⠁                       ",
            "         ⢀⠤⠒⠉                               ",
            "       ⡠⠊⠁                                  ",
            "     ⡠⠊                                     ",
            "   ⢀⠎                                       ",
            "  ⢠⠃                                        ",
            " ⢠⠃                                         ",
            " ⡎                                          ",
            "⢰⠁                                          ",
            "⢸                                           ",
            "⢱                                           ",
            "⢸                                           ",
            " ⡇                                          ",
            " ⠸⡀                                         ",
            "  ⠱⡀                                        ",
            "   ⠑⡄                                       ",
            "    ⠈⠢⡀                                     ",
            "      ⠈⠢⡀                                   ",
            "        ⠈⠑⠤⣀                                ",
            "            ⠉⠒⠢⠤⢄⣀⣀⣀                        ",
            "                    ⠁                       ",
            "                                            ",
        ];
        let expected = expected.join("\n");
        assert_eq!(result, expected);
    }
}