I have implemented a polygon triangulation algorithm into my program. The algorithm first takes a simple polygon, described as 2D points/vertices, and splits it into y-monotone pieces. After this is done, the algorithm then takes each monotone polygon piece and splits it into triangular pieces.
The input to the algorithm I need help with is an array of vertices outlining a y-monotonic polygon in clockwise or counter-clockwise order. The output is a collection of all edges, both from the original polygon but also the new ones added by the triangulation algorithm in order to split this y-monotonic piece into triangular pieces. This works great if I only want to paint the result as it's possible to just draw each edge.
However, there is no particular order to this collection of edges, and I need the output to be an array of type triangle strip or an array where each triangle is simply described by it's 3 vertices (Example: [a1,a2,a3,b1,b2,b3] as we have triangle a and triangle b).
Is there any conventional algorithm or any other way which can help me solve this? Speed is not of extreme importance but I'd prefer a fast solution.
Here is a pseudocode example of the program used in order to give a better understanding of my question:
class Vertex {
var point;
var index;
init(x,y,index){
point = (x,y)
self.index = index
}
}
class Edge {
var from; // of type Vertex
var to; // of type Vertex
init(from, to){
self.from = from
self.to = to
}
}
A call to a function getMonotonePiecesOfPolygon(polygon)
could generate one of many monotonic polygon pieces that would have these vertices and edges:
var vertex0 = Vertex(x0,y0,0)
var vertex1 = Vertex(x1,y1,1)
var vertex2 = Vertex(x2,y2,2)
var vertex3 = Vertex(x3,y3,3)
var edge0 = Edge(vertex0, vertex1)
var edge1 = Edge(vertex1, vertex2)
var edge2 = Edge(vertex2, vertex3)
var edge3 = Edge(vertex3, vertex4)
This could correspond to a rectangle like this:
edge0
0------1
| |
edge3 | | edge1
| |
3------2
edge2
Then, we can supply these edges in an array and divide the polygon into triangles with the a triangulation method 'makeMonotonePolygonIntoTrianglePieces()'
var edgesArray = [edge0,edge1,edge2,edge3]
var edgesArray = makePolygonIntoTrianglePieces(edgesArray)
Now the edgesArray
could look like this:
edgesArray == [edge0,edge1,edge2,edge3,newEdge1]
where
newEdge1.from == vertex0
newEdge1.to == vertex2
which would correspond to a polygon like this if we were to draw each edge:
edge0
0------1
|\ |
edge3 | \ | edge1
| \ |
3------2
edge2
Now here is the part of what my question is seeking to solve. I need an array which would look like this:
var triangles = [
vertex0,vertex3,vertex2,
vertex0,vertex2,vertex1
]
or using triangle strips (each triangle described by one new vertex and two vertices from the previous triangle):
var triangles = [
vertex3, vertex2, vertex0,
vertex1
]
0
and2
. That gives you the two triangles you show in your array. If you then start with either isolated vertex -- in only one triangle (it will have exactly two edges -- 1 and 3 are those isolated vertices), you can "walk" your way down the triangle strip. – Prune