Assignment: Orthogonal segment intersections
Implement the sweep line algorithm for finding the intersections
of a set of orthogonal line segments in 2D and animate the algorithm.
Base code is provided here. You will notice
that the main file viewpoints.cpp is now in cpp and uses
vectors. This is the one assignment where I wolud suggest
that you use cpp, because it gives you balanced binary search trees.
If you decide to stick to C, you'll need to find a free
implementation of balanced binary search trees (like red-black trees
or AVL trees).
You don't need to write OO code, just c plus the vectors and linked
lists that come conveniently with cpp.
The base code is provided to help, but it represents my
vision. Feel free to use as much or as little of this base code as you
want. Feel free to extend it and change it as you see fit.
The code should compile as is, but it does not do anything besides
You need to extend this code to:
- Compute the intersection points
- Simulate the algorithm as it is computing the intersection points (visualize the sweep line moving to the right, the segments being added or deleted from the active structure, and the intersection points as they are computed).
- Provide some interesting test cases (configurations of points). Every
team, please email your special test_cases to the whole class, so that
everyone can include everyone's test cases in their code.
- Include all the test cases emailed by the other teams into your own code.
- Provide a nice way to cycle through the test cases that does not
involve recompiling the code. For example you could make it so that
every time the user presses 'i', the segments are initialized with
the next test case. OR, you could make it so that different test
cases are called when the user presses '0', '1', '2', '3', '4',....
- Whenever the user presses a new test case, your code should
start over simulating the sweep.
What should I write and where?
Part of this assignment is to understand how the code works, why it's
doing what it's doing, and where to insert your code. Once you
understand, you will see that the actual code you need to write is
There are a couple of issues you will need to think about: how to
use binary search trees in cpp (you can use sets, or multimaps, which
are implemented as BST's), how write the comparator function for the
binary search trees; and how to get them to do a range search for you.
Keep in mind that in the subsequent assignments we'll be moving
towards you writing the code from scratch.
You are encouraged to work with a partner, but you don't have to. If
you work with a partner, you will be held to higher standards than if
you work alone; put differently, you'll need to do more work than if
you work alone.
Please give me feedback on how your team works. If I don't hear
any feedback from you I assume your team is working well.
If at any point you would like to work alone or change partners,
email me so that I can ask DJ to update the svn folders.
As usual, you need to strive you write not merely code, but simple,
elegant and easy to understand code. Furthermore, you need to strive
to do this out of habit, as you start programming, and not only at the
end. Writing good code has to become your second nature. Write good
code not because you have to, but because you don't know any other
People often disagree what consitutues elegant when it comes to
coding, but everyone agrees on the following:
- structure your code into small functions with intuitive
arguments that allow you to test your code incrementally after every
function you add
- code should be well documented, ideally at block level but
certainly at function level. Every function should have a header
specifying what are the expected input values, what is the return value, and what
does the function actually do.
- no function should be longer than a screen
- code should be accompanied by a README tha details how to run
it, and whether there are any known bugs that the user shoud be
- [insert here all style guidelines that you know from pevious classes]
How do you know if you write good code? My theory is that the the
quality of your code is ditrectly proportional to how easy and
pleasant is to debug and update/extend your code. If you find debugging is frustrating and like searching a needle in a haystack, then your code style is probably not good.
Submitting your work
Make a folder called p2-segmentintersection in your svn
folder on microwave. I will have access to this svn, so no need to
submit anything --- just make sure everything is checked in.
Last modified: Mon Feb 8 17:13:11 EST 2016