Skip to content

Polygon::add() intersection checking does not account for the closing line added by Closed_polyline. #9

@rutssii

Description

@rutssii

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:
image

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions