-
Notifications
You must be signed in to change notification settings - Fork 19
Description
Hi, I was doing the exercise 16 of chapter 11 (PPP3), where you have to define a new class named Poly that checks in the constructor that, indeed, the object is a polygon. In the process, to test the class, I tried to construct a Poly that in reality is not a polygon with the following points:
Poly a {Point{100,100}, Point{100,150}, Point{150,100}, Point{150,150}};Obtaining (to my surprise) the following output:

After that, I tried doing
Polygon b;
b.add(Point{100,100});
b.add(Point{100,150});
b.add(Point{150,100});
b.add(Point{150,150});Obtaining again the same output (I'm eluding the "scaffolding").
To solve this, I propose adding a check inside the member function Polygon::draw_specifics().
The original definition is:
void Polygon::draw_specifics(Painter& painter) const
{
if (number_of_points() < 3) error("less than 3 points in a Polygon");
Closed_polyline::draw_specifics(painter);
}The modified definition I'm proposing is:
void Polygon::draw_specifics(Painter& painter) const
{
int np = number_of_points(); // added line
if (np < 3) error("less than 3 points in a Polygon");
// added "block"
Point ignored_point{0,0};
for (int i=2; i<np-1; ++i) // int i=2, since the if statement for i==1 will always
// be true (opening and closing line intersect at point(0)).
if (line_segment_intersect(point(0),point(np-1),point(i-1),point(i),ignored_point))
error("intersect in polygon");
Closed_polyline::draw_specifics(painter);
}With this change it no longer draws the non-polygon, but it displays the wanted error (so it resolves the problem I'm reporting).
Please, let me know if this is an actual issue or if I'm missing something (rogerjove13@gmail.com).