Exemplo n.º 1
0
        private Point FindNearestPoint(List <Vector2> points, List <LineSegment> snap_lines, Vector2 p)
        {
            float min_distance = 1e9f;
            int   min_point    = -1;

            if (points.Count > 0)
            {
                min_point    = 0;
                min_distance = Vector2Ext.Distance(p, points[0]);

                for (int index = 1; index < points.Count; index++)
                {
                    float dist = Vector2Ext.Distance(p, points[index]);
                    if (dist < min_distance)
                    {
                        min_point    = index;
                        min_distance = dist;
                    }
                }
                //return points[min_point];
            }

            int min_line = -1;

            if (snap_lines.Count > 0)
            {
                for (int index = 0; index < snap_lines.Count; index++)
                {
                    float dist = snap_lines[index].Distance(p);
                    if (dist < min_distance)
                    {
                        min_line     = index;
                        min_distance = dist;
                        min_point    = -1;
                    }
                }
            }

            if (min_point == -1)
            {
                if (min_line == -1)
                {
                    return(p.ToPoint());
                }
                else
                {
                    Vector2 nearestPoint = GeometryHelper.GetNearestPointInSegmentToPoint(
                        snap_lines[min_line].StartPos, snap_lines[min_line].EndPos, p);
                    return(nearestPoint.ToPoint());
                }
            }
            else
            {
                return(points[min_point].ToPoint());
            }
        }