public static int DistanceSq(D2D_Point a, D2D_Point b)
    {
        var x = b.X - a.X;
        var y = b.Y - a.Y;

        return(x * x + y * y);
    }
    public void Split(ref D2D_Quad first, ref D2D_Quad second, float irregularity)
    {
        if (first == null)
        {
            first = new D2D_Quad();
        }
        if (second == null)
        {
            second = new D2D_Quad();
        }

        var b = D2D_Point.DistanceSq(BL, BR);
        var t = D2D_Point.DistanceSq(TL, TR);
        var l = D2D_Point.DistanceSq(BL, TL);
        var r = D2D_Point.DistanceSq(BR, TR);

        // Vertical split
        if (b > l || b > t || t > l || t > r)
        {
            var TS = TL + (TR - TL) * Random.Range(0.5f - irregularity, 0.5f + irregularity);
            var BS = BL + (BR - BL) * Random.Range(0.5f - irregularity, 0.5f + irregularity);

            first.BL = BL;
            first.BR = BS;
            first.TL = TL;
            first.TR = TS;
            first.CalculateSize();

            second.BL = BS;
            second.BR = BR;
            second.TL = TS;
            second.TR = TR;
            second.CalculateSize();
        }
        // Horizontal split
        else
        {
            var LS = BL + (TL - BL) * Random.Range(0.5f - irregularity, 0.5f + irregularity);
            var RS = BR + (TR - BR) * Random.Range(0.5f - irregularity, 0.5f + irregularity);

            first.BL = LS;
            first.BR = RS;
            first.TL = TL;
            first.TR = TR;
            first.CalculateSize();

            second.BL = BL;
            second.BR = BR;
            second.TL = LS;
            second.TR = RS;
            second.CalculateSize();
        }
    }
예제 #3
0
    public void AddTriangle(D2D_Point a, D2D_Point b, D2D_Point c)
    {
        if (a.Y != b.Y || a.Y != c.Y)
        {
            // Make a highest, and c lowest
            if (b.Y > a.Y)
            {
                D2D_Helper.Swap(ref a, ref b);
            }
            if (c.Y > a.Y)
            {
                D2D_Helper.Swap(ref c, ref a);
            }
            if (c.Y > b.Y)
            {
                D2D_Helper.Swap(ref b, ref c);
            }

            var fth = a.Y - c.Y;             // Full triangle height
            var tth = a.Y - b.Y;             // Top triangle height
            var bth = b.Y - c.Y;             // Bottom triangle height

            // Find a to c intercept along b plane
            var inx = c.X + (a.X - c.X) * D2D_Helper.Divide(bth, fth);
            var d   = new D2D_Point((int)inx, b.Y);

            // Top triangle
            var abs = D2D_Helper.Divide(a.X - b.X, tth);             // A/B slope
            var ads = D2D_Helper.Divide(a.X - d.X, tth);             // A/D slope

            AddTriangle(b.X, d.X, abs, ads, b.Y, 1, tth);

            // Bottom triangle
            var cbs = D2D_Helper.Divide(c.X - b.X, bth);             // C/B slope
            var cds = D2D_Helper.Divide(c.X - d.X, bth);             // C/D slope

            AddTriangle(b.X, d.X, cbs, cds, b.Y, -1, bth);
        }
    }