private float MinimumDistanceFromPointToEdge(Edge line, Vector2 p)
    {
        var v = new Vector2(line.ClippedEnds[LR.LEFT].x, line.ClippedEnds[LR.LEFT].y);
        var w = new Vector2(line.ClippedEnds[LR.RIGHT].x, line.ClippedEnds[LR.RIGHT].y);

        var vf = new Vector2f(v.x, v.y);
        var wf = new Vector2f(w.x, w.y);
        var pf = new Vector2f(p.x, p.y);

        // Return minimum distance between line segment vw and point p
        float l2 = vf.DistanceSquare(wf);

        if (l2 == 0.0)
        {
            return(pf.DistanceSquare(vf));
        }
        float t = Vector2.Dot((p - v), (w - v)) / l2;

        if (t < 0 || t > 1)
        {
            return(-1);
        }

        // Projection falls on the segment
        Vector2 projection   = v + t * (w - v);
        var     prrojectionF = new Vector2f(projection.x, projection.y);

        return(pf.DistanceSquare(prrojectionF));
    }
Пример #2
0
    private Site GetClosestSection(Voronoi diagram, Vector2f point)
    {
        List <Site> sites       = diagram.Sites;
        int         siteCount   = sites.Count;
        float       minDistSq   = float.PositiveInfinity;
        Site        closestSite = null;

        for (int i = 0; i < siteCount; i++)
        {
            float distSq = Vector2f.DistanceSquare(point, sites[i].Coord);
            if (distSq < minDistSq)
            {
                closestSite = sites[i];
                minDistSq   = distSq;
            }
            ;
        }

        return(closestSite);
    }
Пример #3
0
 public float DistanceSquare(Vector2f v)
 {
     return(Vector2f.DistanceSquare(this, v));
 }