# [Help] random quads generator? (non-uniform grid?)

## Recommended Posts

Posted (edited)

@Ego Eram Reputo Can you move this to the plugin development section? The reason why is that I'm trying to develop a solution here to this problem, and it would be more observable to plugin developer. Even though I'm not using c#, some of the codes here are directly translate-able. But this depends if someone wants to collaborate with me even with language barrier regarding g'mic and c#.

The code isn't actually finished, but it's a starting point as the newly created vector is what's going to define rectangle and squares.

```size_sqr=50

mw=\${-max_w} #Find maximum images width#
mh=\${-max_h} #Find maximum images height#

lw={ceil(\$mw/\$size_sqr)*\$size_sqr+2} #Find nearest value to the nearest multiple of size_sqr#
lh={ceil(\$mh/\$size_sqr)*\$size_sqr+2} #Find nearest value to the nearest multiple of size_sqr#

\$lw,\$lh,100%,1,"x==0||x==w-1||y==0||y==h-1?1" #Create 2D vector of size iw,ih. The part between "" is a formula to fill the values of vector#

#Below can be directly translated to codelab provided a vector or something, can it?#
eval \${-math_lib}"

xx=1;
yy=1;
val=1;

selectable_2=vector2(0);
selectable_3=vector3(0);

const msize=3;

do( selectable_size=0;brk_point=0;

if(!i(#-1,xx-1,yy),selectable_size++;);
if(!i(#-1,xx+1,yy),selectable_size++;);
if(!i(#-1,xx,yy+1),selectable_size++;);
if(!i(#-1,xx,yy-1),selectable_size++;);

if(selectable_size==4,dir=round(u(0,3));
,if(selectable_size==1,
if(!i(#-1,xx-1,yy),dir=0;);
if(!i(#-1,xx+1,yy),dir=1;);
if(!i(#-1,xx,yy+1),dir=2;);
if(!i(#-1,xx,yy-1),dir=3;);
,
if(!i(#-1,xx-1,yy),brk_point++;);
if(!i(#-1,xx+1,yy),if(selectable_size==2,selectable_2[brk_point]=1;,selectable_3[brk_point]=1;);brk_point++;);
if(brk_point<selectable_size,if(!i(#-1,xx,yy+1),if(selectable_size==2,selectable_2[brk_point]=2;,selectable_3[brk_point]=2;);brk_point++;););
if(brk_point<selectable_size,if(!i(#-1,xx,yy-1),if(selectable_size==2,selectable_2[brk_point]=3;,selectable_3[brk_point]=3;);););

if(selectable_size==2,
sdir=round(u(0,1));
dir=selectable_2[sdir];
,
sdir=round(u(0,2));
dir=selectable_2[sdir];
);
);
);

mdist=round(u(1,msize));
cl=0;

if(dir==0,
do(xx--;i(#-1,xx,yy)=val;cl++;,cl<mdist||!i(#-1,xx-1,yy));,
if(dir==1,
do(xx++;i(#-1,xx,yy)=val;cl++;,cl<mdist||!i(#-1,xx+1,yy));,
if(dir==2,
do(yy++;i(#-1,xx,yy)=val;cl++;,cl<mdist||!i(#-1,xx,yy+1));,
do(yy--;i(#-1,xx,yy)=val;cl++;,cl<mdist||!i(#-1,xx,yy-1));
);
);
);

val+=1;,!i(#-1,xx-1,yy)||!i(#-1,xx+1,yy)||!i(#-1,xx,yy-1)||!i(#-1,xx,yy+1)
);

"```

Edited by Reptillian

##### Share on other sites
Posted (edited)

Thanks for splitting it so anyone that would like to solve the problem can do so here. I got some progress on solving the square and rectangle problem.

```square_size=50
mw=\${-max_w}
mh=\${-max_h}
nvw={ceil(\$mw/\$square_size)}
nvh={ceil(\$mh/\$square_size)}
\$nvw,\$nvh,1,1

eval \${-math_lib}"
xx=0;
yy=0;
v=0;
const msize=1;
select_two=vector2(0);
select_three=vector(3);
for(p=0,p<wh#-1,p++,
brk_point=0;

nxx_left=xx-1;   if(nxx_left<0,nxx_left=w#-1-1;);
nxx_right=xx+1;  if(nxx_right>=w#-1,nxx_right=0;);
nyy_top=yy-1;    if(nyy_top<0,nxx_top=h#-1-1;);
nyy_bottom=yy+1; if(nyy_bottom>=h#-1,nxx_bottom=0;);

selectable=!i(#-1,nxx_left,yy)+!i(#-1,nxx_right,yy)+!i(#-1,xx,nyy_top)+!i(#-1,xx,nyy_bottom);

if(selectable==1,
if(!i(#-1,nxx_left,yy),dir=0;);
if(!i(#-1,xx,nyy_top),dir=1;);
if(!i(#-1,nxx_right,yy),dir=2;);
if(!i(#-1,xx,nyy_bottom),dir=3;);,

if(selectable==2,
if(!i(#-1,nxx_left,yy),select_two[brk_point]=0;brk_point++;);
if(!i(#-1,xx,nyy_top),select_two[brk_point]=1;brk_point++;);
if(!i(#-1,nxx_right,yy)&&brk_point<2,select_two[brk_point]=2;brk_point++;);
if(!i(#-1,xx,nyy_bottom)&&brk_point<2,select_two[brk_point]=3;);

find_dir=round(u(0,1));
dir=select_two[find_dir];,
if(selectable==3,
if(!i(#-1,nxx_left,yy),select_three[brk_point]=0;brk_point++;);
if(!i(#-1,xx,nyy_top),select_three[brk_point]=1;brk_point++;);
if(!i(#-1,nxx_right,yy),select_three[brk_point]=2;brk_point++;);
if(!i(#-1,xx,nyy_bottom)&&brk_point<3,select_three[brk_point]=3;);
find_dir=round(u(0,2));
dir=select_three[find_dir];,
xx=w#-1-1;
yy=h#-1-1;
do(xx--;
do(yy--;
,yy>0&&i(#-1,xx,yy)>0);
,xx>0&&i(#-1,xx,yy)>0);
);
);
);

if(selectable,
if(!p,cdir=dir);
if(cdir!=dir,v=ceil(v/msize)*(msize-1);cdir=dir;,v+=round(u(0,msize-1)););

if(dir==0,i(#-1,nxx_left,yy)=v;   xx=nxx_left;,
if(dir==1,i(#-1,xx,nyy_top)=v;    yy=nyy_top;,
if(dir==2,i(#-1,nxx_right,yy)=v;  xx=nxx_right;,
i(#-1,xx,nyy_bottom)=v; yy=nyy_bottom;
);
);
);
,i(#-1,xx,yy)=v;
);

v++;
);
"
label. 0,1```

EDIT: I figured it out. The newly created vector would be used as reference for finding rectangles and square.

EDIT: Actually, never mind, there's still work to do.

EDIT: Closest solution here.

Edited by Reptillian

##### Share on other sites
Posted (edited)

I have recently solved my issue (mostly). Both with converting rectilnear shapes into several different shapes, and inserting rectangle.

For converting rectinear into several different shape. It seems that this can do with a small vector as a reference for the larger image. It doesn't work 100%, but it can convert a lot of rectilinear shape into several rectangles in context of vectors.

```to_gray n 0,1
f "
topleft=i(x-1,y-1);
topmiddle=i(x,y-1);
topright=i(x+1,y-1);

middleleft=i(x-1,y);
middle=i;
middleright=i(x+1,y);

bottomleft=i(x-1,y+1);
bottommiddle=i(x,y+1)
bottomright=i(x+1,y+1);

F=i;
if(i,
if(
(
(middleleft&&topmiddle&&middleright)
||(bottommiddle&&middleleft&&topmiddle)
||(middleleft&&bottommiddle&&middleright)
||(bottommiddle&&middleright&&topmiddle)
)
||
(
(bottommiddle&&bottomright&&middleright)
||(topmiddle&&topright&&middleright)
||(topmiddle&&topleft&&middleleft)
||(bottommiddle&&bottomleft&&middleleft)
)
,F++;
);
);
F;
"```

Here's the code for generating "grid" of rectangle and squares.

```#@cli rep_rand_sqrrectex: eq. to 'rep_random_square_rectangle_texture' : (+)
rep_rand_sqrrectex: rep_random_square_rectangle_texture \$*
#@cli rep_random_square_rectangle_texture: square_dimension,_rectangle_max_length_1,_rectangle_length_2,_frequency_of_rectangle,_erode,_output={ 0=none | 1=filled_gray | 2=filled_line | 3=labeled_filled_line },_seed
#@cli : Create a texture with aligned rectangle and squares.
#@cli : (eq. to 'rep_rand_sqrrectex')
#@cli : Default values: '_rectangle_max_length=1','_rectangle_max_length=2','_frequency_of_rectangle=1','_erode=0','_output=2','_seed='
#@cli : Author: Reptorian.
#@cli : \$ rep_random_square_rectangle_texture 20,3,3,2,,0
rep_random_square_rectangle_texture:
skip \${2=2},\${3=1},\${4=1},\${5=0},\${6=2},\${7=}
if narg(\$7) srand \$7 fi
square_size=\$1
if \$6 square_size+=\$6 fi

if \$1<2 error ""\$"1>1==F" fi

mw=\${-max_w}
mh=\${-max_h}
nvw={ceil(\$mw/\$square_size)}
nvh={ceil(\$mh/\$square_size)}

\$nvw,\$nvh,1,1,"x+y*w"
if abs(round(\$2))==1&&abs(round(\$3))==1 error ""\$"2>1||"\$"3>1==F" fi

msize={sqrt(sqr(round(\$2)))+sqr(round(abs(\$3)))}

if \$1<2 error ""\$"2>1==F" fi

freq={int(avg(1/\$msize,\$4,.5)*(iM+1))}
val={iM+1}

repeat round(\$freq)
orientation={round(u(0,1))}
nsl_1={round(u(1,abs(\$2)))}
nsl_2={round(u(1,abs(\$3)))}
min_nsl={min(\$nsl_1,\$nsl_2)}
max_nsl={max(\$nsl_1,\$nsl_2)}
if \$min_nsl==1&&\$max_nsl==1 max_nsl={round(u(2,abs(max(\$2,\$3))))} fi
px={round(u(0,w#-1-1))}
py={round(u(0,h#-1-1))}
if \$orientation \$min_nsl,\$max_nsl,1,1,\$val j.. .,\$px,\$py rm.
else \$max_nsl,\$min_nsl,1,1,\$val j.. .,\$px,\$py rm.
fi
val+=1
done

label. 0,0

if \$6
{\$nvw*\$square_size+1},{\$nvh*\$square_size+1},1,1,i(#-1,floor(x/\$square_size),floor(y/\$square_size)) rm..
if \$6>1 f. "(x!=0&&x!=w-1&&y!=0&&y!=h-1)?(i(x-1,y-1,z,c,0,1)==i(x,y))*((i(x-1,y,z,c,0,1)==i(x,y))&&(i(x,y-1,z,c,0,1)==i(x,y))):0"
if \$5 erode. \$5 expand_xy. {int(\$5/2)},0 if !(\$5%2) r. {w#1-1},{h#-1-1} fi fi
fi
if \$6>2 label_fg. 0,0 fi
fi```

I'll be integrating these two. But, I hope this helps future PDN plugins developers. There might be needing more work, but those work well most of the time.

Edited by Reptillian

## Join the conversation

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

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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

×