Okay, here's my code:
view on github.
I'm using the graph data structure and Voronoi algorithm of the
LEDA library.
The LEDA API is actually pretty good, so you should have no trouble reading the code, even if you are unfamiliar
with the library.
The output of LEDA's Voronoi algorithm is a planar graph. Every edge is labeled with the site on its left side,
and every proper node is labeled with CIRCLE(a, b, c), where a, b, c are sites closest to that node.
The target nodes of unbounded edges have nodes at ,,infinity'' that store degenerated circles.
More information
here.
The idea to bound the Voronoi Diagram, i.e., to intersect it with a quad, is as follows.
First of all, assume that we only want to get rid of unbounded regions. Then we can choose the quad so large
that is only intersects unbounded edges of the Voronoi Diagram.
The basic idea is to traverse all unbounded edges and move their target nodes from ,,infinity'' to the border
of the quad. So, for each unbounded edge you compute the intersection point with the quad. That's pretty cheap,
because the quad consists of only four segments. Also, you don't need to create new nodes or something like that.
You can efficiently find the next unbounded edge (in counter-clockwise order) by following the face cycle of
the unbounded ,,backface'' region.
While traversing, you could simply connect the target nodes of two adjacent unbounded edges with a new edge.
In addition, you have to add the four ,,corner nodes'' of the bounding quad.
In essence, I check for every pair of adjacent edges if there is a quad node between them. If that is the case,
I add it.
Because I traverse the backface in stricly counter-clockwise order, the quad nodes are added in that order, too.
So at any time, there is only one possible candidate node that I have to test.
For my particular application, I needed to bound the Voronoi Diagram with a fix-sized quad,
which might intersect edges of proper regions (I wanted to render a fix-sized section of the diagram).
Therefore, the code I show you first loops over all nodes and edges.
Every edge that intersects the quad is subdivided into two new edges: one new edge is outside the quad and can
be discarded. The other new edge is treated like an additional unbounded edge.
I implemented this algorithm because a naive approach using general segment intersection algorithms turned out
to be too slow.