1
1
#include " PPP/Simple_window.h"
2
2
#include " PPP/Graph.h"
3
3
4
- std::pair<int , int > quadratic_formula (double a, double b, double c) {
5
- int x1 = (-b - std::sqrt (b*b - 4 * a * c) / 2 * a);
6
- int x2 = (-b + std::sqrt (b*b - 4 * a * c) / 2 * a);
7
- return std::make_pair (x1, x2);
8
- }
9
-
10
4
std::pair<int , int > hline_circle_intersection
11
5
(int y, Point center, int radius) {
12
- using std::pow;
13
6
// Produce two values!
14
7
if (y <= center.y - radius || y >= center.y + radius)
15
8
throw std::runtime_error{" y out of range" };
16
- constexpr double a = 1 ;
17
- double b = -2 * center.x ;
18
- double c = pow (y, 2 ) + pow (center.x , 2 ) + pow (center.y , 2 )
19
- - pow (radius, 2 ) - 2 * y * center.y ;
20
- return quadratic_formula (a, b, c);
9
+ int x1 = center.x + std::sqrt (std::pow (radius, 2 ) - std::pow (y - center.y , 2 ));
10
+ int x2 = center.x - std::sqrt (std::pow (radius, 2 ) - std::pow (y - center.y , 2 ));
11
+ return std::make_pair (x1, x2);
21
12
}
22
13
23
14
struct Striped_circle : public Circle {
@@ -28,9 +19,7 @@ struct Striped_circle : public Circle {
28
19
for (int y_pos = center.y - radius + spacing;
29
20
y_pos < center.y + radius;
30
21
y_pos += spacing) {
31
- auto [dx1, dx2] = hline_circle_intersection (y_pos, center, radius);
32
- int x1 = dx1 - center.x ;
33
- int x2 = dx2 - center.x ;
22
+ auto [x1, x2] = hline_circle_intersection (y_pos, center, radius);
34
23
stripes.add (Point{x1, y_pos}, Point{x2, y_pos});
35
24
}
36
25
}
0 commit comments