Vertex3 FindClosestVertex(DMatrix4 mat, DVector4 vec) { List <Vertex3> vs = new List <Vertex3>(); double radius = NormalizeDistance(VertexSize); foreach (Vertex3 v in surface.border0.vertices) { DVector4 tmp = mat * new DVector4(v.Point, 1.0); DVector2 d = new DVector2(tmp.X, tmp.Y) - new DVector2(vec.X, vec.Y); if (d.GetLength() < radius) { vs.Add(v); } } foreach (Vertex3 v in surface.border1.vertices) { DVector4 tmp = mat * new DVector4(v.Point, 1.0); DVector2 d = new DVector2(tmp.X, tmp.Y) - new DVector2(vec.X, vec.Y); if (d.GetLength() < radius) { vs.Add(v); } } foreach (Vertex3 v in surface.border2.vertices) { DVector4 tmp = mat * new DVector4(v.Point, 1.0); DVector2 d = new DVector2(tmp.X, tmp.Y) - new DVector2(vec.X, vec.Y); if (d.GetLength() < radius) { vs.Add(v); } } foreach (Vertex3 v in surface.border3.vertices) { DVector4 tmp = mat * new DVector4(v.Point, 1.0); DVector2 d = new DVector2(tmp.X, tmp.Y) - new DVector2(vec.X, vec.Y); if (d.GetLength() < radius) { vs.Add(v); } } Vertex3 res = null; double minDist = 100000000; foreach (Vertex3 v in vs) { DVector4 tmp = mat * new DVector4(v.Point, 1.0); DVector2 d = new DVector2(tmp.X, tmp.Y) - new DVector2(vec.X, vec.Y); double dist = d.GetLength(); if (dist < minDist) { res = v; minDist = dist; } } return(res); }