Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Algorithms [Sedgewick, Robert]

.pdf
Скачиваний:
143
Добавлен:
02.05.2014
Размер:
4.8 Mб
Скачать

316

CHAPTER 24

count the number of lines from the polygon that it crosses. If the number is odd, the point must be inside; if it is even, the point is outside. This is easily seen by tracing what happens as we come in from the endpoint on the outside: after the first line we hit, we are inside, after the second we are outside, etc. If we proceed an even number of times, the point at which we end up (the original point) must be outside.

The situation is not quite so simple, because some intersections might occur right at the vertices of the input polygon. The drawing below shows some of the situations that need to be handled.

Lines 1 and 2 are straightforward; line 3 leaves the polygon at a vertex; and line 4 coincides with one of the edges of the polygon. The point where line 3 exits should count as 1 intersection with the polygon; all the other points where lines intersect vertices should count as 0 (or 2). The reader may be amused to try to find a simple test to distinguish these cases before reading further.

The need to handle cases where polygon vertices fall on the test lines forces us to do more than just count the line segments in the polygon which intersect the test line. Essentially, we want to travel around the polygon, incrementing an intersection counter whenever we go from one side of the test

Соседние файлы в предмете Алгоритмы