Exemple #1
0
    /// <summary>
    /// Distance between a point and the shape.
    /// </summary>
    /// <param name="pt">Test point</param>
    /// <param name="mode">Snap modes to consider</param>
    /// <returns>Distance from point to nearest point on shape</returns>
    public override SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode)
    {
        SnapPoint snap = new SnapPoint();

        snap.point = position;

        if ((mode & SnapPoint.Mode.Center) != 0)
        {
            snap.mode = SnapPoint.Mode.Center;
        }
        else if ((mode & SnapPoint.Mode.Endpoint) != 0)
        {
            snap.mode = SnapPoint.Mode.Endpoint;
        }
        else if ((mode & SnapPoint.Mode.Midpoint) != 0)
        {
            snap.mode = SnapPoint.Mode.Midpoint;
        }
        else if ((mode & SnapPoint.Mode.Edge) != 0)
        {
            snap.mode = SnapPoint.Mode.Edge;
        }

        return(snap);
    }
Exemple #2
0
    /// <summary>
    /// Distance between a point and the shape.
    /// </summary>
    /// <param name="pt">Test point</param>
    /// <param name="mode">Snap modes to consider</param>
    /// <returns>Distance from point to nearest point on shape</returns>
    public override SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode)
    {
        SnapPoint snap     = new SnapPoint();
        float     distance = float.MaxValue;

        if ((mode & SnapPoint.Mode.Center) != 0)
        {
            float d = Vector2.Distance(pt, position);
            if (d < distance)
            {
                distance   = d;
                snap.mode  = SnapPoint.Mode.Center;
                snap.point = position;
            }
        }

        if ((mode & SnapPoint.Mode.Midpoint) != 0)
        {
            float     offset45  = 0.7071f * radius;
            Vector2[] midPoints =
            {
                position + new Vector2(radius,           0f),
                position + new Vector2(offset45,  offset45),
                position + new Vector2(0f,        radius),
                position + new Vector2(-offset45, offset45),
                position + new Vector2(-radius,          0f),
                position + new Vector2(-offset45, -offset45),
                position + new Vector2(0f,          -radius),
                position + new Vector2(offset45,  -offset45),
            };

            foreach (Vector2 testPt in midPoints)
            {
                float d = Vector2.Distance(pt, testPt);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Midpoint;
                    snap.point = testPt;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Edge) != 0)
        {
            float d = Distance(pt);
            if (d < distance)
            {
                distance  = d;
                snap.mode = SnapPoint.Mode.Edge;
                float angle = Mathf.Atan2(pt.y - position.y, pt.x - position.x);
                snap.point.x = position.x + Mathf.Cos(angle) * radius;
                snap.point.y = position.y + Mathf.Sin(angle) * radius;
            }
        }

        return(snap);
    }
Exemple #3
0
 /// <summary>
 /// Distance between a point and the shape.
 /// </summary>
 /// <param name="pt">Test point</param>
 /// <param name="mode">Snap modes to consider</param>
 /// <returns>Distance from point to nearest point on shape</returns>
 public abstract SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode);
Exemple #4
0
    /// <summary>
    /// Distance between a point and the shape.
    /// </summary>
    /// <param name="pt">Test point</param>
    /// <param name="mode">Snap modes to consider</param>
    /// <returns>Distance from point to nearest point on shape</returns>
    public override SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode)
    {
        SnapPoint snap     = new SnapPoint();
        float     distance = float.MaxValue;

        if ((mode & SnapPoint.Mode.Center) != 0)
        {
            if (closed && (vertices.Length > 0))
            {
                Vector2 center = new Vector2();
                for (int i = 0; i < vertices.Length; i++)
                {
                    center += vertices[i].position;
                }

                center /= vertices.Length;
                float d = Vector2.Distance(pt, center);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Center;
                    snap.point = center;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Endpoint) != 0)
        {
            for (int i = 0; i < vertices.Length; i++)
            {
                float d = Vector2.Distance(pt, vertices[i].position);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Endpoint;
                    snap.point = vertices[i].position;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Midpoint) != 0)
        {
            int count = closed ? vertices.Length : vertices.Length - 1;
            for (int i = 0; i < count; i++)
            {
                Vertex  vert     = vertices[i];
                Vertex  vertNext = vertices[NextIndex(i)];
                Vector2 midPoint;

                if (vertices[i].segmentCurves)
                {
                    midPoint = VectorShapeUtils.EvaluateCubicCurve(vert.position, vert.exitCP, vertNext.enterCP, vertNext.position, 0.5f);
                }
                else
                {
                    midPoint = (vert.position + vertNext.position) / 2f;
                }
                float d = Vector2.Distance(pt, midPoint);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Midpoint;
                    snap.point = midPoint;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Edge) != 0)
        {
            int count = closed ? vertices.Length : vertices.Length - 1;
            for (int i = 0; i < count; i++)
            {
                Vertex  vert     = vertices[i];
                Vertex  vertNext = vertices[NextIndex(i)];
                Vector2 closest;

                if (vertices[i].segmentCurves)
                {
                    closest = VectorShapeUtils.ClosetPointOnBezierCurve(pt, vert.position, vert.exitCP, vertNext.enterCP, vertNext.position);
                }
                else
                {
                    closest = VectorShapeUtils.ClosestPointOnLineSegment(pt, vert.position, vertNext.position);
                }
                float d = Vector2.Distance(pt, closest);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Edge;
                    snap.point = closest;
                }
            }
        }

        return(snap);
    }
Exemple #5
0
    /// <summary>
    /// Distance between a point and the shape.
    /// </summary>
    /// <param name="pt">Test point</param>
    /// <param name="mode">Snap modes to consider</param>
    /// <returns>Distance from point to nearest point on shape</returns>
    public override SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode)
    {
        SnapPoint snap     = new SnapPoint();
        float     distance = float.MaxValue;

        if ((mode & SnapPoint.Mode.Center) != 0)
        {
            Vector2[] centerPoints =
            {
                position,
                position + majorAxis * eccentricity,
                position - majorAxis * eccentricity,
            };

            foreach (Vector2 testPt in centerPoints)
            {
                float d = Vector2.Distance(pt, testPt);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Midpoint;
                    snap.point = testPt;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Midpoint) != 0)
        {
            Vector2[] midPoints =
            {
                position + majorAxis,
                position + MinorAxis,
                position - majorAxis,
                position - MinorAxis,
            };

            foreach (Vector2 testPt in midPoints)
            {
                float d = Vector2.Distance(pt, testPt);
                if (d < distance)
                {
                    distance   = d;
                    snap.mode  = SnapPoint.Mode.Midpoint;
                    snap.point = testPt;
                }
            }
        }

        if ((mode & SnapPoint.Mode.Edge) != 0)
        {
            Vector2 closest = ClosestPoint(pt);
            float   d       = Vector2.Distance(pt, closest);

            if (d < distance)
            {
                distance   = d;
                snap.mode  = SnapPoint.Mode.Edge;
                snap.point = closest;
            }
        }

        return(snap);
    }
Exemple #6
0
    /// <summary>
    /// Distance between a point and the shape.
    /// </summary>
    /// <param name="pt">Test point</param>
    /// <param name="mode">Snap modes to consider</param>
    /// <returns>Distance from point to nearest point on shape</returns>
    public override SnapPoint GetSnap(Vector2 pt, SnapPoint.Mode mode)
    {
        SnapPoint snap = new SnapPoint();

        return(snap);
    }