I just finished porting as3delaunay to C# (https://github.com/jceipek/Unity-delaunay) and I'm trying to figure out how to best expose a graph of connected nodes for the Voronoi results. It seems that adjacent LineSegments produced by Voronoi.voronoiDiagram do not have perfectly overlapping vertices; even the individual coordinates of the vertices of the LineSegments are sometimes off by a small margin.
Could you perhaps offer some suggestions as to how to create such a graph?
At the moment, I'm using a dictionary in a manner similar to the way the kruskal function works but indexing on a 2-Tuple with custom hash and equality functions to make nearby vertices hash the same way, but that seems quite kludgy and doesn't always work perfectly:
public struct Tuple : System.IEquatable<Tuple>
{
readonly float x;
readonly float y;
public Tuple (float first, float second)
{
this.x = first;
this.y = second;
}
public float X { get { return x; } }
public float Y { get { return y; } }
public override int GetHashCode ()
{
return (Mathf.RoundToInt (x)).GetHashCode () ^ (Mathf.RoundToInt (y)).GetHashCode ();
}
public override bool Equals (object obj)
{
if (obj == null || GetType () != obj.GetType ()) {
return false;
}
return Equals ((Tuple)obj);
}
public bool Equals (Tuple other)
{
return Mathf.Approximately (other.x, x) && Mathf.Approximately (other.y, y);
}
}
// After setting up lineSegments = voronoiInstance.voronoiDiagram(points,colors,rect)
Dictionary<Tuple,Node> network = new Dictionary<Tuple,Node> ();
for (int i = 0; i< lineSegments.Count; i++) {
Tuple p0 = new Tuple (((Vector2)lineSegments [i].p0).x, ((Vector2)lineSegments [i].p0).y);
if (!network.ContainsKey (p0)) {
network [p0] = new Node ();
}
Tuple p1 = new Tuple (((Vector2)lineSegments [i].p1).x, ((Vector2)lineSegments [i].p1).y);
if (!network.ContainsKey (p1)) {
network [p1] = new Node ();
}
if (i == r0) {
t_a = network [p0];
} else if (i == r1) {
t_b = network [p1];
}
ConnectNodes (network [p0], network [p1]);
}
I just finished porting
as3delaunay
toC#
(https://github.com/jceipek/Unity-delaunay) and I'm trying to figure out how to best expose a graph of connected nodes for the Voronoi results. It seems that adjacentLineSegment
s produced byVoronoi.voronoiDiagram
do not have perfectly overlapping vertices; even the individual coordinates of the vertices of theLineSegment
s are sometimes off by a small margin.Could you perhaps offer some suggestions as to how to create such a graph? At the moment, I'm using a dictionary in a manner similar to the way the
kruskal
function works but indexing on a2-Tuple
with custom hash and equality functions to make nearby vertices hash the same way, but that seems quite kludgy and doesn't always work perfectly: