Polygon

Stroustrup has said "Polygon is a Closed_polyline where no two line segments intersect" in PPP.

Please look at this simple code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <Simple_window.h>

int main()
{
	using namespace Graph_lib;

	Point t(100,100);
	Simple_window win(t,500,300, "Test");

	Graph_lib::Polygon p;
	p.add(Point(30,30));
	p.add(Point(50,50));

	p.add(Point(30,80));
	p.add(Point(45,70));
	win.attach(p);
	win.wait_for_button();
}

If you run it, you see that there are two line segments that intersect! Why? Isn't his talk true!?
The question is: who should enforce that rule?

Should the Polygon connect the dots given to it in any way it pleases?
OR
Should the user supply the dots in such order that a valid polygon results?
If you run it, you see that there are two line segments that intersect! Why?
Why don't you try to plot the lines with pen and paper in the order you instructed above. What do you get?
Guys,
I know since the Polygon is a Closed Polyline so it should connects all the dots the user gives it. But the problem here is within the definition. It has been said that "no two line segments intersect" but by a simple code I broke that talk. Probably in fact it's up to the user how defines those dots and sometimes it's possible to two line segments intersect. Apparently there is no difference between Polygon and Closed Polyline.
Apparently there is no difference between Polygon and Closed Polyline.

Can you read/show the code of class Graph_lib::Polygon?
No unfortunately. In your point of view is there any difference between them? if so please aware me of it.
In order to use a class one must see its definition, i.e. the header. So that part of the code you must be able to read.

I have no idea what PPP and Graph_lib are, so I won't speculate.
Can you read/show the code of class Graph_lib::Polygon?


You can find it here:
http://www.stroustrup.com/Programming/Graphics/

The definition is in Graph.h and the implementation is in Graph.cpp.

I see:
1
2
3
4
5
for (int i = 1; i<np-1; ++i) {    // check that new segment doesn't interset and old point
  Point ignore(0,0);
  if (line_segment_intersect( point(np-1), p, point(i-1), point(i), ignore))
    error("intersect in polygon");
}

This clearly does check whether the new segment would intersect with any of the existing segments.

However, it does not check against the implicit closing segment p,point(0).
One should add after that loop:
1
2
3
4
Point ignore(0,0);
if (line_segment_intersect( point(np-1), p, p, point(0), ignore))
  error("intersect in polygon");
}


Therefore, I do see a clear difference between Polygon and Closed Polyline, but the implementation of the Polygon appears to have a logical error.
Whether the definition/implementation is correct or not, I'm not thinking about it. What I see is that, if we don't be careful (about the coordinates of points) both of the Closed Polyline and the Polygon intersect. And there isn't (apparently) any difference between them in practice! What the Stroustrup will say in this case!
Ask him.

Change the fourth point to (40,30). Is there now a difference?
I asked him, but when he feels there is case that can be a challenge about his book's errors he doesn't like to reply such an E-mails!

I deliberately gave that fourth point those coordinates because I wanted to show that this definition about the Polygon is not correct.

I don't agree with that. Definition of Polygon is reasonable. Implementation of Polygon fails to fulfil the promise of the definition for that special input.

Simple errata for example code is hardly a challenge. Wrong tone of correspondence can be.
If you think the problem was a wrong tone of correspondence! so mail him yourself and I'll be very happy if you tell me (or us) what's his answer.
Topic archived. No new replies allowed.