Jump to content

Rectangle Division (Incompleted Code Available Here)


Recommended Posts

Right now, I have a near complete code.

 

The only problem is that DEBUG shows ModifiableRect[0] outputs (0,0,0,0,0). If that wasn't the case, then the code should work in theory.

 

The idea is to create array containing rectangles, and the voids modify existing rectangle and insert new array of rectangles. iter means number of iteration of splitting.

 

Some questions:

1. Is the binary Random condition coded in here is correct? myRandom(2)==0?

2. Any reason why it outputs a blank image?

 

Spoiler
// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
IntSliderControl max_divide = 10; // [1,20] Division
IntSliderControl init_add_thick = 0; // [0,100] Additional Thickness
IntSliderControl max_iter = 5; // [2,40] Maximum Division Iteration
IntSliderControl probability = 95; // [0,100] Probability of Division(%)
IntSliderControl max_loop = 2000; // [1000,2097152] Loop Limit
DoubleSliderControl border = 10; // [0,100] Border(%)
ReseedButtonControl seed = 0; // Reseed
#endregion
#if DEBUG
#endif

int[,] Rectangles_Surface;

Random Seed = new Random();
int new_seed;

List<(int,int,int,int,int)> Modifiable_Rect = new List<(int,int,int,int,int)>{
            (0,0,0,0,0)
};

List<(int,int,int,int,int)> Unmodifiable_Rect = new List<(int,int,int,int,int)>{
            (0,0,0,0,0)
};

bool diff_check(int a)
{
    int mri = max_divide;
    double ds = (double)(a/mri);
    if (mri>1&&(ds<1)){
        while(mri>1&&(ds<1)){
            mri--;
            ds = (double)(a/mri);
        }
    }

    return ds>1;
}

void divide_column(int point,int border_size,int test_boundary,Random rand){
    int ri = rand.Next(1,max_divide)+1;
    int mri = ri-1;
    int min_x=Modifiable_Rect[point].Item1;
    int max_x=Modifiable_Rect[point].Item2;
    int min_y=Modifiable_Rect[point].Item3;
    int max_y=Modifiable_Rect[point].Item4;
    int iter=Modifiable_Rect[point].Item5 + 1;
    int sx = min_x+test_boundary;
    int ex = max_x+test_boundary;
    bool bound_check = (ex-sx)>test_boundary;
    bool skip = false;
    double mx,d_np;
    int np;
    Modifiable_Rect.RemoveAt(point);
    if(bound_check)
    {
        int diff = (ex-sx);
        double ds=(double)(diff/mri);
        if(mri>1&&(ds<=(double)(test_boundary)))
        {
            while(mri>1&&(ds<=(double)(test_boundary)))
            {
                ri--;
                mri--;
                ds=(double)(diff/mri);
            }
        }
        bool process = (test_boundary>0)||(diff>0)?(ds>(double)(test_boundary)):true;
        if (process)
        {
            int p=min_x;
            mx=(double)(p);
            for (int ri_ind= 0 ; ri_ind < ri ; ri_ind++){
                bool rb = probability<100?(rand.Next(100)>probability):false;
                if (ri_ind==mri)
                {
                    if(p>max_x){
                        p=max_x;

                        if((max_y-min_y)==0){rb=true;}

                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((p,max_x,min_y,max_y,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((p,max_x,min_y,max_y,iter));

                        }
                    }
                }
                else
                {
                    if(ri_ind==0)
                    {
                        mx+=ds;
                        d_np=Math.Min(Math.Max(sx,rand.Next(p,(int)(Math.Round(mx)))),ex);
                        np=(int)(Math.Round(d_np));
                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((p,np,min_y,max_y,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((p,np,min_y,max_y,iter));

                        }
                        p=np+1;
                        if(p>=ex)
                        {
                            skip = true;
                        }

                    }
                    else
                    {
                        if(skip){continue;}
                        mx+=ds;
                        d_np=Math.Min(Math.Max(p+border_size,rand.Next(p,(int)(Math.Round(mx)))),ex);
                        np=(int)(Math.Round(d_np));
                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((p,np,min_y,max_y,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((p,np,min_y,max_y,iter));
                        }
                    }
                }
            }
        }
        else
        {
            Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,iter));
        }

    }
    else
    {
        Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,iter));
    }
}

void divide_row(int point,int border_size,int test_boundary,Random rand){
    int ri = rand.Next(1,max_divide)+1;
    int mri = ri-1;
    int min_x=Modifiable_Rect[point].Item1;
    int max_x=Modifiable_Rect[point].Item2;
    int min_y=Modifiable_Rect[point].Item3;
    int max_y=Modifiable_Rect[point].Item4;
    int iter=Modifiable_Rect[point].Item5 + 1;
    int sy = min_y+test_boundary;
    int ey = max_y+test_boundary;
    bool bound_check = (ey-sy)>test_boundary;
    bool skip = false;
    double my,d_np;
    int np;
    Modifiable_Rect.RemoveAt(point);
    if(bound_check)
    {
        int diff = (ey-sy);
        double ds=(double)(diff/mri);
        if(mri>1&&(ds<=(double)(test_boundary)))
        {
            while(mri>1&&(ds<=(double)(test_boundary)))
            {
                ri--;
                mri--;
                ds=(double)(diff/mri);
            }
        }
        bool process = (test_boundary>0)||(diff>0)?(ds>(double)(test_boundary)):true;
        if (process)
        {
            int p=min_y;
            my=(double)(p);
            for (int ri_ind= 0 ; ri_ind < ri ; ri_ind++){
                bool rb = probability<100?(rand.Next(100)>probability):false;
                if (ri_ind==mri)
                {
                    if(p>max_x){
                        p=max_y;

                        if((max_x-min_x)==0){rb=true;}

                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((min_x,max_x,p,max_y,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((min_x,max_x,p,max_y,iter));

                        }
                    }
                }
                else
                {
                    if(ri_ind==0)
                    {
                        my+=ds;
                        d_np=Math.Min(Math.Max(sy,rand.Next(p,(int)(Math.Round(my)))),ey);
                        np=(int)(Math.Round(d_np));
                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((min_x,max_y,p,np,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((min_x,max_y,p,np,iter));

                        }
                        p=np+1;
                        if(p>=ey)
                        {
                            skip = true;
                        }

                    }
                    else
                    {
                        if(skip){continue;}
                        my+=ds;
                        d_np=Math.Min(Math.Max(p+border_size,rand.Next(p,(int)(Math.Round(my)))),ey);
                        np=(int)(Math.Round(d_np));
                        if (rb || iter==max_iter)
                        {
                            Modifiable_Rect.Add((min_x,min_y,p,np,iter));
                        }
                        else
                        {
                            Unmodifiable_Rect.Add((min_x,min_y,p,np,iter));
                        }
                    }
                }
            }
        }
        else
        {
            Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,iter));
        }

    }
    else
    {
        Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,iter));
    }
}

bool pass=false;

void PreRender(Surface dst, Surface src)
{

    if (Rectangles_Surface==null){
        Rectangles_Surface=new int [src.Width,src.Height];
    }

    Random myRandom;

    if (new_seed != seed)
    {
        myRandom = Seed;
    }
    else
    {
        myRandom = new Random(int.MaxValue);
    }

    new_seed = seed;

    int width  = src.Width  - 1;
    int height = src.Height - 1;
    int border_size;
    int test_boundary;

    if (init_add_thick>0)
    {
        border_size=init_add_thick+1;
        test_boundary=border_size+1;
    }
    else{
        border_size=0;
        test_boundary=0;
    }

    bool width_check=diff_check(width-border_size*2);
    bool height_check=diff_check(height-border_size*2);

    if (width_check||height_check){
        pass = true;
        Unmodifiable_Rect.RemoveAt(0);
        Modifiable_Rect.Add((0,width,0,height,0));
        Debug.WriteLine(Modifiable_Rect[0]);
        Modifiable_Rect.RemoveAt(0);
        if (width_check&&height_check)
        {
            if(myRandom.Next(2)==0)
            {
                divide_row(0,border_size,test_boundary,myRandom);
            }
            else
            {
                divide_column(0,border_size,test_boundary,myRandom);
            }
        }
        else
        {
            if(height_check)
            {
                divide_row(0,border_size,test_boundary,myRandom);
            }
            else
            {
                divide_column(0,border_size,test_boundary,myRandom);
            }
        }
        
        int arr_point=0;
        int p_ind,sx,sy,mx,my;

        while(Modifiable_Rect.Count>0)
        {
            p_ind=myRandom.Next(Modifiable_Rect.Count-1);
            if(myRandom.Next(2)==0)
            {
                divide_row(p_ind,border_size,test_boundary,myRandom);
            }
            else
            {
                divide_column(p_ind,border_size,test_boundary,myRandom);
            }
            arr_point++;
            if(arr_point>=max_loop){break;}
        }

        if(Modifiable_Rect.Count>0){
            for(int i = 0 ; i < Modifiable_Rect.Count ; i++){
                sx = Modifiable_Rect[i].Item1;
                sy = Modifiable_Rect[i].Item2;
                mx = Modifiable_Rect[i].Item3 + 1;
                my = Modifiable_Rect[i].Item4 + 1;
                for (int x = sx ; x < mx ; x++){
                    for (int y = sy ; y < my ; y++){
                        Rectangles_Surface[x,y]=i;
                    }
                }
            }
        }
        if(Unmodifiable_Rect.Count>0){
            for(int i = 0 ; i < Unmodifiable_Rect.Count ; i++){
                sx = Unmodifiable_Rect[i].Item1;
                sy = Unmodifiable_Rect[i].Item2;
                mx = Unmodifiable_Rect[i].Item3 + 1;
                my = Unmodifiable_Rect[i].Item4 + 1;
                for (int x = sx ; x < mx ; x++){
                    for (int y = sy ; y < my ; y++){
                        Rectangles_Surface[x,y]=i;
                    }
                }
            }
        }
    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need
    Rectangle selection = EnvironmentParameters.SelectionBounds;
    int centerX = ((selection.Right - selection.Left) / 2) + selection.Left;
    int centerY = ((selection.Bottom - selection.Top) / 2) + selection.Top;
    ColorBgra primaryColor = EnvironmentParameters.PrimaryColor;
    ColorBgra secondaryColor = EnvironmentParameters.SecondaryColor;
    int brushWidth = (int)EnvironmentParameters.BrushWidth;
    int val;

    ColorBgra currentPixel;
    for (int y = rect.Top; y < rect.Bottom; y++)
    {
        if (IsCancelRequested) return;
        for (int x = rect.Left; x < rect.Right; x++)
        {
            currentPixel = src[x,y];
            val = Rectangles_Surface[x,y];
            dst[x,y] = ColorBgra.FromBgra((byte)(val),(byte)(val),(byte)(val),255);
        }
    }
}

 

 

Edited by Reptillian

G'MIC Filter Developer

Link to comment
Share on other sites

2 hours ago, Reptillian said:

The only problem is that DEBUG shows ModifiableRect[0] outputs (0,0,0,0,0)

 

Yes, according to your code, those are the correct values.

 

An index of 0 will give you the first item in the list, which you initialized with (0,0,0,0,0)

List<(int,int,int,int,int)> Modifiable_Rect = new List<(int,int,int,int,int)>{
            (0,0,0,0,0)
};

 

And then in PreRender(), you added a second item; which would have the index of 1.   But then you do a WriteLine of index 0, the first item in the list.

Modifiable_Rect.Add((0,width,0,height,0));
Debug.WriteLine(Modifiable_Rect[0]);

 

Link to comment
Share on other sites

I think I'm just gonna leave it with this uncomplete code. Turns out it's not achievable:

 

Here's what I get (hence why I don't think it can be achieved):

 

unknown.png?width=1073&height=621

The expected output:

 

unknown.png?width=621&height=621

With more work, it can look like this: https://cdn.discordapp.com/attachments/880256029705773099/893671307558916157/unknown.png

// Name:
// Submenu:
// Author:
// Title:
// Version:
// Desc:
// Keywords:
// URL:
// Help:
#region UICode
IntSliderControl max_divide = 10; // [1,20] Division
IntSliderControl init_add_thick = 1; // [0,100] Additional Thickness
IntSliderControl max_iter = 5; // [2,40] Maximum Division Iteration
IntSliderControl probability = 95; // [0,100] Probability of Division(%)
IntSliderControl max_loop = 2000; // [2000,2097152] Loop Limit
DoubleSliderControl border = 10; // [0,100] Border(%)
ReseedButtonControl seed = 0; // Reseed
#endregion
#if DEBUG
#endif

ColorBgra[,] Rectangles_Surface;
ColorBgra Empty_Color=ColorBgra.Black;

Random Seed = new Random();
int new_seed;

List<(int,int,int,int,int)> Modifiable_Rect = new List<(int,int,int,int,int)>{
            (0,0,0,0,0)
};

List<(int,int,int,int,int)> Unmodifiable_Rect = new List<(int,int,int,int,int)>{
            (0,0,0,0,0)
};


bool diff_check(int a)
{
    int mri = max_divide;
    double ds = (double)(a/mri);
    if (mri>1&&(ds<1)){
        while(mri>1&&(ds<1)){
            mri--;
            ds = (double)(a/mri);
        }
    }

    return ds>1;
}


bool pass=false;

Random myRandom;

void split_column(int point,int test_boundary,int border_size){
        bool skip = false;
        int mx=0;
        int np;

        int ri = myRandom.Next(1,max_divide)+1;
        int mri = ri - 1;

        int min_x=Modifiable_Rect[point].Item1;
        int max_x=Modifiable_Rect[point].Item2;
        int min_y=Modifiable_Rect[point].Item3;
        int max_y=Modifiable_Rect[point].Item4;
        int itern=Modifiable_Rect[point].Item5+1;

        Modifiable_Rect.RemoveAt(point);

        int sx = min_x+test_boundary;
        int ex = max_x-test_boundary;

        bool bound_check = (ex-sx)>test_boundary;
        bool rb;

        int min_v,max_v;

        if (bound_check)
        {
            int diff=ex-sx;
            double ds=(double)(diff/mri);
            if(mri>1&&(ds<=test_boundary))
            {
                while(mri>1&&(ds<=test_boundary))
                {
                    ri--;
                    mri--;
                    ds=(double)(diff/mri);
                }
            }
            if(((test_boundary>0)||(diff>0))?(ds>(double)(test_boundary)):true)
            {
                int p=min_x;
                for(int ri_ind=0; ri_ind<ri ; ri_ind++)
                {
                    rb=myRandom.Next(0,99)>(probability-1);
                    if (ri_ind==mri)
                    {
                        if (p>max_x)
                        {
                            p=max_x;
                            if((max_y-min_y)==0){rb=true;}
                        }
                        if (rb || (itern==max_iter))
                        {
                            Unmodifiable_Rect.Add((p,max_x,min_y,max_y,itern));
                        }
                        else
                        {
                            Modifiable_Rect.Add((p,max_x,min_y,max_y,itern));
                        }

                    }
                    else
                    {
                        if(ri_ind==0)
                        {
                            mx=(int)(Math.Round((double)(sx+ds)));
                            min_v=Math.Min(p,mx);
                            max_v=Math.Max(p,mx);
                            np=Math.Min(Math.Max(sx,myRandom.Next(min_v,max_v)),ex);
                            if (rb || (itern==max_iter))
                            {
                                Unmodifiable_Rect.Add((p,np,min_y,max_y,itern));
                            }
                            else
                            {
                                Modifiable_Rect.Add((p,np,min_y,max_y,itern));
                            }
                            p=np+1;
                            if(p>=ex){skip=true;}
                        }
                        else
                        {
                            if(skip){continue;}
                            mx=(int)(Math.Round((double)(mx+ds)));
                            min_v=Math.Min(p,mx+1);
                            max_v=Math.Max(p,mx+1);
                            np=Math.Min(Math.Max(p+border_size,myRandom.Next(min_v,max_v)),ex);
                            if (rb || (itern==max_iter))
                            {
                                Unmodifiable_Rect.Add((Math.Min(p,ex),np,min_y,max_y,itern));
                            }
                            else
                            {
                                Modifiable_Rect.Add((Math.Min(p,ex),np,min_y,max_y,itern));
                            }
                            p=np+1;
                        }
                    }
                }
            }
            else
            {
                Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,itern));
            }
        }
        else
        {
            Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,itern));
        }
}

void split_row(int point,int test_boundary,int border_size){
        bool skip = false;
        int my=0;
        int np;

        int ri = myRandom.Next(1,max_divide)+1;
        int mri = ri - 1;

        int min_x=Modifiable_Rect[point].Item1;
        int max_x=Modifiable_Rect[point].Item2;
        int min_y=Modifiable_Rect[point].Item3;
        int max_y=Modifiable_Rect[point].Item4;
        int itern=Modifiable_Rect[point].Item5+1;

        Modifiable_Rect.RemoveAt(point);

        int sy = min_y+test_boundary;
        int ey = max_y-test_boundary;

        bool bound_check = (ey-sy)>test_boundary;
        bool rb;

        int min_v,max_v;

        if (bound_check)
        {
            int diff=ey-sy;
            double ds=(double)(diff/mri);
            if(mri>1&&(ds<=test_boundary))
            {
                while(mri>1&&(ds<=test_boundary))
                {
                    ri--;
                    mri--;
                    ds=(double)(diff/mri);
                }
            }
            if(((test_boundary>0)||(diff>0))?(ds>(double)(test_boundary)):true)
            {
                int p=min_x;
                for(int ri_ind=0; ri_ind<ri ; ri_ind++)
                {
                    rb=myRandom.Next(0,99)>(probability-1);
                    if (ri_ind==mri)
                    {
                        if (p>max_y)
                        {
                            p=max_y;
                            if((max_x-min_x)==0){rb=true;}
                        }
                        if (rb || (itern==max_iter))
                        {
                            Unmodifiable_Rect.Add((min_x,max_x,p,max_y,itern));
                        }
                        else
                        {
                            Modifiable_Rect.Add((min_x,max_x,p,max_y,itern));
                        }

                    }
                    else
                    {
                        if(ri_ind==0)
                        {
                            my=(int)(Math.Round((double)(sy+ds)));
                            min_v=Math.Min(p,my);
                            max_v=Math.Max(p,my);
                            np=Math.Min(Math.Max(sy,myRandom.Next(min_v,max_v)),ey);
                            if (rb || (itern==max_iter))
                            {
                                Unmodifiable_Rect.Add((min_x,max_x,p,np,itern));
                            }
                            else
                            {
                                Modifiable_Rect.Add((min_x,max_x,p,np,itern));
                            }
                            p=np+1;
                            if(p>=ey){skip=true;}
                        }
                        else
                        {
                            if(skip){continue;}
                            my=(int)(Math.Round((double)(my+ds)));
                            min_v=Math.Min(p,my+1);
                            max_v=Math.Max(p,my+1);
                            np=Math.Min(Math.Max(p+border_size,myRandom.Next(min_v,max_v)),ey);
                            if (rb || (itern==max_iter))
                            {
                                Unmodifiable_Rect.Add((min_x,max_x,Math.Min(p,ey),np,itern));
                            }
                            else
                            {
                                Modifiable_Rect.Add((min_x,max_x,Math.Min(p,ey),np,itern));
                            }
                            p=np+1;
                        }
                    }
                }
            }
            else
            {
                Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,itern));
            }
        }
        else
        {
            Unmodifiable_Rect.Add((min_x,max_x,min_y,max_y,itern));
        }
}

void PreRender(Surface dst, Surface src)
{

    if (Rectangles_Surface==null)
    {
        Modifiable_Rect.RemoveRange(0,1);
        Unmodifiable_Rect.RemoveRange(0,1);
        Rectangles_Surface=new ColorBgra [src.Width,src.Height];
    }
    else
    {
        Modifiable_Rect.RemoveRange(0,Modifiable_Rect.Count);
        Unmodifiable_Rect.RemoveRange(0,Unmodifiable_Rect.Count);
        Array.Clear(Rectangles_Surface,0,src.Width*src.Height);
    }

    if (new_seed != seed)
    {
        Modifiable_Rect.RemoveRange(0,Modifiable_Rect.Count);
        Unmodifiable_Rect.RemoveRange(0,Unmodifiable_Rect.Count);
        myRandom = Seed;
    }
    else
    {
        myRandom = new Random(int.MaxValue);
    }

    new_seed = seed;

    int width  = src.Width  - 1;
    int height = src.Height - 1;
    int border_size;
    int test_boundary;

    if (init_add_thick>0)
    {
        border_size=init_add_thick+1;
        test_boundary=border_size+1;
    }
    else{
        border_size=0;
        test_boundary=0;
    }

    bool width_check=diff_check(width-border_size*2);
    bool height_check=diff_check(height-border_size*2);

    if (width_check||height_check){
        pass = true;
        Modifiable_Rect.Add((0,width,0,height,0));

        //split_row(0,test_boundary,border_size);

        if (width_check&&height_check)
        {
            if (myRandom.Next(0,2)==0)
            {
                split_column(0,test_boundary,border_size);
            }
            else
            {
                split_row(0,test_boundary,border_size);
            }
        }
        else
        {
            if (height_check)
            {
                split_column(0,test_boundary,border_size);
            }
            else
            {
                split_row(0,test_boundary,border_size);
            }
        }

        int arr_point=0;
        int p_ind;

        while(Modifiable_Rect.Count>0){
            p_ind=myRandom.Next(Modifiable_Rect.Count);
            if(myRandom.Next(0,2)==0)
            {
                split_column(p_ind,test_boundary,border_size);
            }
            else
            {
                split_row(p_ind,test_boundary,border_size);
            }
            arr_point++;
            if(arr_point>=max_loop){break;}
        }


        int new_min_x;
        int new_max_x;
        int new_min_y;
        int new_max_y;
        int nv=0;

        int ex,ey;

        ColorBgra Temp_Color;

        int tr,tg,tb;

        if (Modifiable_Rect.Count>0)
        {
            for (int i = 0 ; i < Modifiable_Rect.Count ; i++){
                new_min_x=Modifiable_Rect[i].Item1;
                new_max_x=Modifiable_Rect[i].Item2;
                new_min_y=Modifiable_Rect[i].Item3;
                new_max_y=Modifiable_Rect[i].Item4;
                ex = new_max_x+1;
                ey = new_max_y+1;
                tr=myRandom.Next(256);
                tg=myRandom.Next(256);
                tb=myRandom.Next(256);
                for (int x = new_min_x ; x<ex ; x++)
                {
                    for (int y = new_min_y ; y<ey ; y++)
                    {
                        Rectangles_Surface[x,y]=ColorBgra.FromBgr((byte)(tb),(byte)(tg),(byte)(tr));
                    }
                }
                nv++;
            }
        }

        if (Unmodifiable_Rect.Count>0)
        {
            for (int j = 0 ; j < Unmodifiable_Rect.Count ; j++)
            {
                new_min_x=Unmodifiable_Rect[j].Item1;
                new_max_x=Unmodifiable_Rect[j].Item2;
                new_min_y=Unmodifiable_Rect[j].Item3;
                new_max_y=Unmodifiable_Rect[j].Item4;
                ex=new_max_x+1;
                ey=new_max_y+1;
                tr=myRandom.Next(256);
                tg=myRandom.Next(256);
                tb=myRandom.Next(256);
                for (int x = new_min_x ; x<ex ; x++)
                {
                    for (int y = new_min_y ; y<ey ; y++)
                    {
                        Rectangles_Surface[x,y]=ColorBgra.FromBgr((byte)(tb),(byte)(tg),(byte)(tr));
                    }
                }
                nv++;
            }
        }

    }
}

void Render(Surface dst, Surface src, Rectangle rect)
{
    // Delete any of these lines you don't need

    if (pass)
    {
        for (int y = rect.Top; y < rect.Bottom; y++)
        {
            if (IsCancelRequested) return;
            for (int x = rect.Left; x < rect.Right; x++)
            {
                dst[x,y] = Rectangles_Surface[x,y];
            }
        }
    }
}

 

Based off this working G'MIC code:

#@cli rep_rrd: eq. to 'rep_random_rectangular_division' : (+)
rep_rrd: rep_random_rectangular_division $*
#@cli rep_random_rectangular_division: split>1,_additional_thickness>=0,_max_iter>=2,_probability[%]>0,_loop_limit>1,_seed
#@cli : Generate random division of rectangle as in random number of division and varying thickness within rectangle.
#@cli : _additional_thickness refers to the excess pixel thickness.
#@cli : _max_iter limits the number of iterations per rectangle.
#@cli : _probability determines the probability that a rectangle will be permitted to be utilized for further iteration.
#@cli : _loop_limit limits the number of time the process of subdivision is done.
#@cli : _seed generates the output based on defined parameter.
#@cli : Default values: '_gen_thick=0','_max_iter=5','_probability[%]=95%','_loop_limit=5000','_seed=n/a'
rep_random_rectangular_division:
skip ${2=0},${3=5},${4=95%},${5=5000},${6=}
check "$4>0"
repeat $! l[$>]
 _rep_random_rectangular_division $*
endl done
_rep_random_rectangular_division:
skip ${2=0},${3=5},${4=95%},${5=5000},${6=}
 
r 100%,100%,1,1 #You can ignore this.
 
1,1,1,5 #Modifiable Rectangle Array
1,1,1,5 #Unmodifiable Rectangle Array
 
eval ${-math_lib}"
 if(narg($6),srand($6););
 const ww=w#-3-1; #Width of Image
 const hh=h#-3-1; #Height of Image
 const max_split=max(1,int(abs($1)));                   #Number of Split per Rectangle
 const internal_space=int(abs($2));                     #Additional paces between Split
 const test_boundary=internal_space?(internal_space+1); #Test if rectangle is acceptable to split
 const max_iter=max(2,round(abs($3)));                  #Maximum Iteration
 const odd=cut($4,0,1);                                 #Probability of splitting rectangle
 const arr_limit=round(abs($5));                        #The limit of looping to split rectangle
 
 diff_check(a)=( #diff_check(a) is a function
  mri=max_split;
  ds=a/mri;
  if(mri>1&&ds<1,
   while(mri>1&&ds<1,
    mri--;
    ds=a/mri;
   );
  );
  ds>1;
 );
 
 width_check=diff_check(ww-internal_space*2); # Determine if the width of image is reasonable with parameters assigne
 height_check=diff_check(hh-internal_space*2); # Determine if the height of image is reasonable with parameters assigne
 
 !(width_check||height_check)?run('error inv_dim'); #If neither are reasonable, then throws a error
 
 dar_insert(#-2,[0,ww,0,hh,0]);   #Insert the dimensions of image and the first iteration number which is 0 into the modifiable array
 
 sub_column(v,point)=(            #Function to Split Rectangle
  ri=int(u(1,max_split))+1;       #Number of Division
  mri=ri-1;                       #Maximum Division Iteration
  sx=v[0]+test_boundary;          #Start Point for Next Split
  ex=v[1]-test_boundary;          #End Gap between the End Index and the start of the final Split
  diff=ex-sx;                     #Difference between the Two
  bound_check=diff>test_boundary; #Check the boundary between two, if fails, send it to the un-modifiable rectangle array
  skip=0;                         #Boolean Value of skip
  dar_remove(#-2,point);          #Remove Rectangle Information at point
  if(bound_check,                 #If Bound Check
   ds=diff/mri;                   #The length of diff divided by the maximum-iteration number.
   if(mri>1&&ds<=test_boundary,   #Check if the distance is smaller than test_boundary. If fail, then send to unmodifiable rectangle array
    while(mri>1&&ds<=test_boundary, #Used to find the minimum mri to reach size greater than test_bounary
     ri--;
     mri--;
     ds=diff/mri;
    );
   );
   if(test_boundary||diff?(ds>test_boundary):1, #If condition meets, then it will modify the rectangle. Otherwise, send it to unmodifiable
    py=[v[2],v[3]];   #Two Variable inserted as one. Used for simplifying Code
    iter=v[4]+1;      #The current iteration number of rectangle plus 1. Used for stopping splitting at iteration-n
    p=v[0];           #Start at min-x of rectangle
    repeat(ri,ri_ind, #Repeat ri time, and ri_ind is the number of used as iterator
     rb=odd<1?u(0,1)>odd:0; #The probability of a rectangle will be modifiable or not
     ri_ind==mri?( #Meets the last iteration of ri loop
      if(p>v[1],
       p=v[1];
       if(!(v[3]-v[2]),rb=1;); #If the pixel dimension is equal to 1, it no longer becomes modifiable
      );
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[p,v[1],py,iter]); #Send to unmodifiable
      ):(
       dar_insert(#-2,[p,v[1],py,iter]); #Send to modifiable
      );
     ):
     !ri_ind?(
      mx=sx+ds;
      np=min(max(sx,round(u(p,mx))),ex);
      vp=[p,np];
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[vp,py,iter]);
      ):(
       dar_insert(#-2,[vp,py,iter]);
      );
      p=np+1;
      if(p>=ex,skip=1;);
     ):(
      if(skip,continue(););
      mx+=ds;
      np=min(max(p+internal_space,round(u(p,mx))),ex);
      vp=[min(p,ex),np];
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[vp,py,iter]);
      ):(
       dar_insert(#-2,[vp,py,iter]);
      );
      p=np+1;
     );
    );
   ,dar_insert(#-1,v);
   );
  ,dar_insert(#-1,v);
  );
 );
 
 sub_row(v,point)=(
  ri=int(u(1,max_split))+1;
  mri=ri-1;
  sy=v[2]+test_boundary;
  ey=v[3]-test_boundary;
  diff=ey-sy;
  bound_check=diff>test_boundary;
  skip=0;
  dar_remove(#-2,point);
  if(bound_check,
   ds=diff/mri;
   if(mri>1&&ds<=test_boundary,
    while(mri>1&&ds<=test_boundary,
     ri--;
     mri--;
     ds=diff/mri;
    );
   );
   if(test_boundary||diff?(ds>test_boundary):1,
    px=[v[0],v[1]];
    iter=v[4]+1;
    p=v[2];
    repeat(ri,ri_ind,
     rb=odd<1?u(0,1)>odd:0;
     ri_ind==mri?(
      if(p>v[3],
       p=v[3];
       if(!(v[1]-v[0]),rb=1;);
      );
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[px,p,v[3],iter]);
      ):(
       dar_insert(#-2,[px,p,v[3],iter]);
      );
     ):
     !ri_ind?(
      my=sy+ds;
      np=min(max(sy,round(u(p,my))),ey);
      vp=[p,np];
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[px,vp,iter]);
      ):(
       dar_insert(#-2,[px,vp,iter]);
      );
      p=np+1;
      if(p>=ey,skip=1;);
     ):(
      if(skip,continue(););
      my+=ds;
      np=min(max(p+internal_space,round(u(p,my))),ey);
      vp=[min(p,ey),np];
      (rb||(iter==max_iter))?(
       dar_insert(#-1,[px,vp,iter]);
      ):(
       dar_insert(#-2,[px,vp,iter]);
      );
      p=np+1;
     );
    );
   ,dar_insert(#-1,v);
   );
  ,dar_insert(#-1,v);
  );
 );
 
 
 v=I[#-2,0];
 
 width_check&&height_check?(
  u(0,1)<=.5?(
   sub_row(v,0);
  ):(
   sub_column(v,0);
  );
 ):
 width_check?(
  sub_column(v,0);
 ):
 height_check?(
  sub_row(v,0);
 );
 
 arr_point=0;
 
 while(dar_size(#-2),
  p_ind=int(u(dar_size(#-2)));
  cv=I[#-2,p_ind];
  u(0,1)<.5?(
   sub_row(cv,p_ind);
  ):(
   sub_column(cv,p_ind);
  );
  arr_point++;
  if(arr_point>=arr_limit,break());
 );
 
 resize(#-2,1,dar_size(#-2),1,4,0);
 resize(#-1,1,dar_size(#-1),1,4,0);"
 
a[-2,-1] y
 
eval. :"begin(
  convert2coords(c)=(
   tl=[c[0],c[2]];
   tr=[c[1],c[2]];
   bl=[c[0],c[3]];
   br=[c[1],c[3]];
   [tl,tr,br,bl];
  );
 );
 polygon(#-2,4,convert2coords(I),1,y);"
 
rm.

 

Edited by Reptillian
Added comments into G'MIC code if anyone can help

G'MIC Filter Developer

Link to comment
Share on other sites

  • Reptillian changed the title to Rectangle Division (Incompleted Code Available Here)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...