public static bool IsNearBezierTest(Vector2 p, Drawing.BezierCurve c, float accuracy, float maxDist)
    {
        Vector2 prepoint = c.Get(0);

        for (float i = accuracy; i < 1; i += accuracy)
        {
            var   point = c.Get(i);
            float d     = (p - point).sqrMagnitude;
            float d2    = (prepoint - point + new Vector2(maxDist, maxDist)).sqrMagnitude;
            if (d <= d2 * 2)
            {
                return(true);
            }
        }

        return(false);
    }
Пример #2
0
    public static Vector2 NearestPointOnBezier(Vector2 p, Drawing.BezierCurve c, float accuracy, bool doubleAc)
    {
        float   minDist = Mathf.Infinity;
        float   minT    = 0f;
        Vector2 minP    = Vector2.zero;

        for (float i = 0; i < 1; i += accuracy)
        {
            Vector2 point = c.Get(i);
            float   d     = (p - point).sqrMagnitude;
            if (d < minDist)
            {
                minDist = d;
                minT    = i;
                minP    = point;
            }
        }

        if (!doubleAc)
        {
            return(minP);
        }

        float st = Mathf.Clamp01(minT - accuracy);
        float en = Mathf.Clamp01(minT + accuracy);


        for (float i = st; i < en; i += accuracy / 10)
        {
            Vector2 point = c.Get(i);
            float   d     = (p - point).sqrMagnitude;
            if (d < minDist)
            {
                minDist = d;
                minT    = i;
                minP    = point;
            }
        }


        return(minP);
    }
    public static bool IsNearBezier(Vector2 p, Drawing.BezierPoint point1, Drawing.BezierPoint point2, float rad)
    {
        if (point1.curve2 != point2.curve1)
        {
            Debug.LogError("Curves Not The Same");
            return(false);
        }

        Drawing.BezierCurve curve = point1.curve2;

        var r = curve.rect;

        r.x      -= rad;
        r.y      -= rad;
        r.width  += rad * 2;
        r.height += rad * 2;

        if (!r.Contains(p))
        {
            return(false);
        }

        var nearest = NearestPointOnBezier(p, curve, 0.1f, false);

        var sec = point1.curve2.aproxLength / 10;

        if ((nearest - p).sqrMagnitude >= (sec * 3) * (sec * 3))
        {
            return(false);
        }

        nearest = NearestPointOnBezier(p, curve, 0.01f, true);

        if ((nearest - p).sqrMagnitude <= rad * rad)
        {
            return(true);
        }

        return(false);
    }
Пример #4
0
    public void test()
    {
        float startTime = Time.realtimeSinceStartup;
        float w         = 100;
        float h         = 100;

        Drawing.BezierPoint p1 = new Drawing.BezierPoint(new Vector2(10, 0), new Vector2(5, 20), new Vector2(20, 0));
        Drawing.BezierPoint p2 = new Drawing.BezierPoint(new Vector2(50, 10), new Vector2(40, 20), new Vector2(60, -10));
        Drawing.BezierCurve c  = new Drawing.BezierCurve(p1.main, p1.control2, p2.control1, p2.main);
        p1.curve2 = c;
        p2.curve1 = c;
        Vector2 elapsedTime = new Vector2((Time.realtimeSinceStartup - startTime) * 10, 0);
        float   startTime2  = Time.realtimeSinceStartup;

        for (int i = 0; i < w * h; i++)
        {
            Mathfx.IsNearBezier(new Vector2(Random.value * 80, Random.value * 30), p1, p2, 10);
        }

        Vector2 elapsedTime2 = new Vector2((Time.realtimeSinceStartup - startTime2) * 10, 0);

        Debug.Log("Drawing took " + elapsedTime.ToString() + "  " + elapsedTime2.ToString());
    }
Пример #5
0
    void test()
    {
        float startTime = Time.realtimeSinceStartup;
        var w = 100;
        var h = 100;
        var p1 = new Drawing.BezierPoint(new Vector2(10, 0), new Vector2(5, 20), new Vector2(20, 0));
        var p2 = new Drawing.BezierPoint(new Vector2(50, 10), new Vector2(40, 20), new Vector2(60, -10));
        var c = new Drawing.BezierCurve(p1.main, p1.control2, p2.control1, p2.main);
        p1.curve2 = c;
        p2.curve1 = c;
        Vector2 elapsedTime = new Vector2((Time.realtimeSinceStartup - startTime) * 10, 0);
        float startTime2 = Time.realtimeSinceStartup;
        for (var i = 0; i < w * h; i++)
        {
            Mathfx.IsNearBezier(new Vector2(Random.value * 80, Random.value * 30), p1, p2, 10);
        }

        Vector2 elapsedTime2 = new Vector2((Time.realtimeSinceStartup - startTime2) * 10, 0);
        Debug.Log("Drawing took " + elapsedTime.ToString() + "  " + elapsedTime2.ToString());

    }