// return index of point in the middle of p1 and p2 private static int getMiddlePoint(int p1, int p2, ref List <Point3f> vertices, ref Dictionary <long, int> cache, float radius) { // first check if we have it already bool firstIsSmaller = p1 < p2; long smallerIndex = firstIsSmaller ? p1 : p2; long greaterIndex = firstIsSmaller ? p2 : p1; long key = (smallerIndex << 32) + greaterIndex; int ret; if (cache.TryGetValue(key, out ret)) { return(ret); } // not in cache, calculate it Point3f point1 = vertices[p1]; Point3f point2 = vertices[p2]; Point3f middle = new Point3f ( (point1.X + point2.X) / 2f, (point1.Y + point2.Y) / 2f, (point1.Z + point2.Z) / 2f ); // add vertex makes sure point is on unit sphere int i = vertices.Count; vertices.Add(middle.Unit() * radius); // store it, return index cache.Add(key, i); return(i); }