Пример #1
0
    void MyDrawTriangle(Texture2D tex)
    {
        float x1 = p1.x, y1 = p1.y;
        float x2 = p2.x, y2 = p2.y;
        float x3 = p3.x, y3 = p3.y;

        // 令 y1 ≥ y2 ≥ y3
        if (y2 <= y3)
        {
            MyUtility.Swap(ref y2, ref y3);
            MyUtility.Swap(ref x2, ref x3);
        }
        if (y1 <= y2)
        {
            MyUtility.Swap(ref y2, ref y1);
            MyUtility.Swap(ref x2, ref x1);
        }
        if (y2 < y3)
        {
            MyUtility.Swap(ref y2, ref y3);
            MyUtility.Swap(ref x2, ref x3);
        }
        // 令 y2 == y3 && x2 < x3
        if (y2 == y3 && x2 > x3)
        {
            MyUtility.Swap(ref y2, ref y3);
            MyUtility.Swap(ref x2, ref x3);
        }
        // 令 y1 == y2 && x2 < x1
        if (y2 == y1 && x2 > x1)
        {
            MyUtility.Swap(ref y2, ref y1);
            MyUtility.Swap(ref x2, ref x1);
        }

        var A = new MyVector3(x1, y1);
        var B = new MyVector3(x2, y2);
        var C = new MyVector3(x3, y3);

        // 直线AC y - y1 = m * (x - x1), m = (y1-y3)/(x1-x3)
        float     M_x   = (y2 - y1) / ((y1 - y3) / (x1 - x3)) + x1;
        MyVector3 M     = new MyVector3(M_x, y2);
        var       Left  = B.x < M.x ? B : M;
        var       Right = B.x < M.x ? M : B;

        DrawTriangleInner(tex, A, Left, Right, -1);
        DrawTriangleInner(tex, C, Left, Right, +1);

        // 最后画BM
        DrawLine.Draw(tex, (int)B.x, (int)B.y, (int)M.x, (int)M.y, triangleColor, DrawLine.LineType.Line);

        // 4个顶点
        MyUtility.DrawPoint(tex, (int)A.x, (int)A.y, Color.red);
        MyUtility.DrawPoint(tex, (int)B.x, (int)B.y, Color.green);
        MyUtility.DrawPoint(tex, (int)C.x, (int)C.y, Color.blue);
        MyUtility.DrawPoint(tex, (int)M.x, (int)M.y, Color.yellow);
    }
Пример #2
0
    public void MyDraw(Texture2D tex)
    {
        if (Points == null || Points.Length < 1)
        {
            return;
        }

        switch (type)
        {
        case BizierCurveType.Quad_th_BizierCurve:
            Quad_th_BizierCurve(tex);
            break;

        case BizierCurveType.MyNth_BizierCurve:
            MyNth_BizierCurve(tex);
            break;

        case BizierCurveType.Nth_BizierCurve:
            Nth_BizierCurve(tex);
            break;

        default:
            break;
        }

        // debug
        int n = Points.Length;

        MyUtility.DrawPoint(tex, (int)Points[0].x, (int)Points[0].y, Color.red);
        for (int i = 0, j = 1; j < n; i++, j++)
        {
            var pre = Points[i];
            var cur = Points[j];
            int x0 = (int)pre.x, y0 = (int)pre.y;
            int x1 = (int)cur.x, y1 = (int)cur.y;
            DrawLine.Draw(tex, x0, y0, x1, y1, Color.white, DrawLine.LineType.Bresenham);
            MyUtility.DrawPoint(tex, x1, y1, Color.red);
        }
    }
Пример #3
0
    // https://www.jasondavies.com/animated-bezier/
    void Quad_th_BizierCurve(Texture2D tex, float t = 0)
    {
        /* t 在[0, 1]
         * n = 2 -> B1(t) = P0 + (P1 - p0)t, 从P0起点沿P0->P1直线方向移动, 当t = 1时到达P1
         * n = 3 -> B2(t) = (1-t)²*P0 + (1-t)*t*P1 + t²*P2
         */
        if (Points == null || Points.Length < 3)
        {
            return;
        }
        if (t >= 1)
        {
            return;
        }

        MyVector2 p1 = Points[0];
        MyVector2 p2 = Points[1];
        MyVector2 p3 = Points[2];

        int x1 = (int)p1.x, y1 = (int)p1.y;
        int x2 = (int)p2.x, y2 = (int)p2.y;
        int x3 = (int)p3.x, y3 = (int)p3.y;

        DrawLine.Draw(tex, x1, y1, x2, y2, Color.white, DrawLine.LineType.Bresenham);
        DrawLine.Draw(tex, x2, y2, x3, y3, Color.white, DrawLine.LineType.Bresenham);
        MyUtility.DrawPoint(tex, x1, y1, Color.red);
        MyUtility.DrawPoint(tex, x2, y2, Color.green);
        MyUtility.DrawPoint(tex, x3, y3, Color.blue);

        // p' = (1-t)*p0 + t*p1
        var pa = (1 - t) * p1 + t * p2;
        var pb = (1 - t) * p2 + t * p3;
        var o  = (1 - t) * pa + t * pb;

        tex.SetPixel((int)o.x, (int)o.y, Color.red);
        Quad_th_BizierCurve(tex, t + 0.01f);
    }