Having 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: Add code 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.
To see some simple OpenGL examples on how to use the mouse, look in
Code/mouse.
- The polygon has to be simple. To start, the user should make sure that
the polygon entered is simple. Eventually, you need to implement 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. Eventually, you need to implement a function
that tests whether a point is inside a polygon.
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 big
problem in itself.
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 have a chance to come up with a
solution form scratch. A quadratic algorithm will be fine.
If you can come up with an O(n lg n) algorithm you'll get extra credit
and a round of applause in class (hint: radial sweep).