Fun in an art gallery
In this assignment the goal is to find the visible area of a guard (point)
inside an art gallery (polygon).
That is, assume you have an input polygon in the plane that represents the
plan of an art gallery, and a point inside this polygon that
represents a guard. Your task is to implement an algorithm that
computes and displays the part of the gallery visible to the guard.
To manage complexity we'll split it into two parts:
- Part 1: the user can click on points, and the code computes and
displays the visible area from that point.
- Part 2: Extend so that the guard (last point clicked by the
user) is moving inside the polygon, and the visible area is
displayed as the guard is moving. Make it so that the guard does not
get stuck in a corner of the polygon.
The interface
- To make it easy to test, your code will use the mouse to initialize a
polygon, and also to initialize the position of a guard inside the
polygon.
You'll find examples on how to use the mouse in the github repo for
this assignment.
- The polygon has to be simple. To start, the user should make
sure that the polygon entered is simple. Ideally there would be a
function that tests whether a polygon is simple.
- The guard has to be inside the polygon. To start, the user
should make sure it's inside. Ideally there would be a
function that tests whether a point is inside a polygon ---- we'll
talk about this in class, and it's a cool little idea (the degenerate
cases are messy, but there is full pseudocode in the textbook).
- Once the polygon and the guard are set, run your algorithm that
computes that polygon that's visible and render it with a different
color.
You may have already seen that OpenGL can render a polygon in two
ways: just its boundary (GL_LINE mode), or filled (GL_FILL
mode), by turning on one of:
glPolygonMode(GL_LINE);
//glPolygonMode(GL_FILL);
Something to be aware of is that openGL can only render filled
polygons that are convex. This seems like a big limitation, however if
you think about it a little it becomes clear that its not trivial to
render a non-convex polygon filled. Essentially you need to compute
the triangulation of the polygon, and then render one triangle at a
time. Computing a triangulation of a non-convex polygon is a bigger
problem in itself, and OpenGL does not do it as part of glDraw.
You'll need to find a different way to render the visible non-convex
polygon. Ideally it has to be filled.
Computing the visible polygon
The crux of this assignment is to come up with an algorithm to compute
the visible polygon of the guard. We have not talked specifically
about this problem in class, so you need to come up with a solution
from scratch. A quadratic algorithm will be fine.
If you implement an O(n lg n) algorithm you'll get extra credit
and a round of applause in class (hint: radial sweep). If you are
considering an improved algorithm, the papers
listed below may be helpful.