Created
March 22, 2015 21:01
-
-
Save reportbase/4571043029644f08ccf1 to your computer and use it in GitHub Desktop.
create fitted rectangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline rectangle* uniform_fitted_rectangle::get(int cols, int rows, int width, int height, int margin) | |
{ | |
if(width < 0 || height < 0) | |
return 0; | |
int size = cols * rows; | |
int n = 0; | |
int y = 0; | |
int iheight = height + margin; | |
int rwidth = width + margin; | |
int ww = (int)(rwidth / (float) cols); | |
int hh = (int)(iheight / (float) rows); | |
rectangle* rect = (rectangle*) calloc(size, sizeof(rectangle)); | |
rectangle* hrect = rect; | |
int hadj = height - hh * rows + 1; | |
int wadj = width - ww * cols + 1; | |
for(int row = 0; row < rows; ++row) | |
{ | |
int h = hh - margin; | |
int x = 0; | |
int wadja = wadj; | |
for(int col = 0; col < cols; ++col, ++n, ++rect) | |
{ | |
int w = ww - margin; | |
rect->x = x; | |
rect->y = y; | |
rect->width = w; | |
rect->height = h; | |
int m = wadja-- > 0 ? 1 : 0; | |
x += w + margin + m; | |
} | |
int m = hadj-- > 0 ? 1 : 0; | |
y += h + margin + m; | |
} | |
return hrect; | |
} | |
inline rectangle* uniform_rectangle::get(int cols, int rows, int width, int height, int margin) | |
{ | |
if(width < 0 || height < 0) | |
return 0; | |
int size = cols * rows; | |
int n = 0; | |
int y = 0; | |
int iheight = height + margin; | |
int rwidth = width + margin; | |
int ww = (int)(rwidth / (float) cols); | |
int hh = (int)(iheight / (float) rows); | |
rectangle* rect = (rectangle*) calloc(size, sizeof(rectangle)); | |
rectangle* hrect = rect; | |
for(int row = 0; row < rows; ++row) | |
{ | |
int h = hh - margin; | |
int x = 0; | |
for(int col = 0; col < cols; ++col, ++n, ++rect) | |
{ | |
int w = ww - margin; | |
rect->x = x; | |
rect->y = y; | |
rect->width = w; | |
rect->height = h; | |
x += w + margin; | |
} | |
y += h + margin; | |
} | |
return hrect; | |
} | |
inline rectangle* fixed_rectangle::get(int cols, int rows, | |
int width, int height, int margin) | |
{ | |
if(width < 0 || height < 0) | |
return 0; | |
int x = 0; | |
int y = 0; | |
int outn = 0; | |
int size = cols * rows; | |
rectangle* out = (rectangle*) calloc(size, sizeof(rectangle)); | |
for(int n = 0; n < rows; ++n) | |
{ | |
for(int m = 0; m < cols; ++m) | |
{ | |
int w = width / cols; | |
if(x + w > width) | |
w = width - x; | |
int h = height / rows; | |
if(y + h > height) | |
h = height - y; | |
rectangle r = {x, y, w, h}; | |
memcpy(&out[outn++], &r, sizeof(rectangle)); | |
x += width / cols; | |
} | |
y += height / rows; | |
x = 0; | |
} | |
return out; | |
} | |
inline rectangle* fitted_rectangle::get(int cols, int rows, int width, int height, int margin) | |
{ | |
if(width < 0 || height < 0) | |
return 0; | |
int size = cols * rows; | |
int n = 0; | |
int y = 0; | |
int iheight = height + margin; | |
int rwidth = width + margin; | |
int ww = (int)(rwidth / (float) cols); | |
int hh = (int)(iheight / (float) rows); | |
int xadj = rwidth - (cols * ww); | |
int yadj = iheight - (rows * hh); | |
rectangle* rect = (rectangle*) malloc(size * sizeof(rectangle)); | |
rectangle* hrect = rect; | |
for(int row = 0; row < rows; ++row) | |
{ | |
int h = hh - margin; | |
if(yadj-- >= 1) | |
h++; | |
int x = 0; | |
int xadj_ = xadj; | |
for(int col = 0; col < cols; ++col, ++n, ++rect) | |
{ | |
int w = ww - margin; | |
if(xadj-- >= 1) | |
w++; | |
rect->x = x; | |
rect->y = y; | |
rect->width = w; | |
rect->height = h; | |
x += w + margin; | |
} | |
xadj = xadj_; | |
y += h + margin; | |
} | |
return hrect; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment