java - Delaunay triangles point connectivity? -
i watching video: delaunay triangulation , want use generate procedural content in same way. had pretty hard time figuring out how work delaunaytriangulation
class supplied libgdx
guess figured out.
my question how know point connected point in easy way? how setup points testing, these points need supplied rooms generated.
private void triangletest() { delaunaytriangulator triangulator = new delaunaytriangulator(); points = new float[8]; points[0] = 80; points[1] = 30; points[2] = 40; points[3] = 45; points[4] = 0; points[5] = 10; points[6] = -100; points[7] = 100; indices = triangulator.computetriangles(points, false); system.out.println(indices); //output [1, 0, 2, 1, 2, 3] }
and how drawing points , lines/triangles, visualization , understanding.
private void drawtriangles() { //draw points (int = 0; < points.length / 2; i++) { debughelper.drawdebugpoint(new vector2(points[i * 2], points[i * 2 + 1]), camera.combined); } //draw lines (int = 0; < indices.size / 3; i++) { vector2 v1 = new vector2(points[indices.get(i * 3) * 2], points[indices.get(i * 3) * 2 + 1]); vector2 v2 = new vector2(points[indices.get(i * 3 + 1) * 2], points[indices.get(i * 3 + 1) * 2 + 1]); vector2 v3 = new vector2(points[indices.get(i * 3 + 2) * 2], points[indices.get(i * 3 + 2) * 2 + 1]); debughelper.drawdebugline(v1, v2, camera.combined); debughelper.drawdebugline(v2, v3, camera.combined); debughelper.drawdebugline(v3, v1, camera.combined); } }
assuming using correctly (the points , triangles drawn correctly) i'm drawing double lines:
[1, 0, 2] //1st indices (2nd point connects 1st point) [1, 2, 3] //2nd indices (1st point connects 2nd point)
so there someway filter these out? since interested in connectivity , not drawing triangles side side generate meshes.
also, in the video mentioned on top notice there lines being removed have death ends @ 50 seconds in movie. how calculated? colored , leaves nice mix of death ends , loops. interested know how achieved.
it seems want compute set of edges of resulting triangles. therefore, can create edge
class, 2 special properties:
- the
equals
method returnstrue
if vertices of edge compare swapped (so edge(2,1)
considered "equal" edge(1,2)
) - the
hashcode
method implemented in way consistentequals
implementation, noted in object#hashcode documentation:if 2 objects equal according equals(object) method, calling hashcode method on each of 2 objects must produce same integer result.
edge
objects can put set
, , set
take care of rest: if "duplicate" edges added, set contain each edge once. import java.util.linkedhashset; import java.util.set; public class delaunayedges { public static void main(string[] args) { int triangleindices[] = new int[] { 1, 0, 2, 1, 2, 3 }; set<edge> edges = computeedges(triangleindices); system.out.println("edges: "+edges); } static class edge { private final int vertex0; private final int vertex1; public edge(int vertex0, int vertex1) { this.vertex0 = vertex0; this.vertex1 = vertex1; } @override public string tostring() { return "("+vertex0+","+vertex1+")"; } @override public int hashcode() { return vertex0 ^ vertex1; } @override public boolean equals(object object) { if (this == object) { return true; } if (object == null) { return false; } if (getclass() != object.getclass()) { return false; } edge = (edge) object; return (this.vertex0 == that.vertex0 && this.vertex1 == that.vertex1) || (this.vertex0 == that.vertex1 && this.vertex1 == that.vertex0); } } private static set<edge> computeedges(int triangleindices[]) { set<edge> edges = new linkedhashset<edge>(); (int i=0; i<triangleindices.length; i+=3) { int i0 = triangleindices[i+0]; int i1 = triangleindices[i+1]; int i2 = triangleindices[i+2]; edges.add(new edge(i0, i1)); edges.add(new edge(i1, i2)); edges.add(new edge(i2, i0)); } return edges; } }
the above program print
edges: [(1,0), (0,2), (2,1), (2,3), (3,1)]
thus, omitting edge (1,2)
, because considered equal edge (2,1)
.
(of course, 1 convert set plain int[]
array of edge indices, that's not point of question)
Comments
Post a Comment