Discussion about this post

User's avatar
palexdev's avatar

My implementation in Java with a few extensions: https://github.com/palexdev/rectcut-java

Expand full comment
vegasword's avatar

I'm learning UI programming from scratch, and this approach is indeed straightforward to start with. Thank you for the article! I would like to mention that for the cut functions, the shrinking mechanism is missing to counter the offset of the cut. Here are my functions as an example:

Rect CutLeft(Rect *rect, int length) {

int oldX = rect->x;

rect->x = min(rect->width, rect->x + length);

rect->width -= rect->x;

if (rect->width < 0) rect->width = 0;

return (Rect) { oldX, rect->y, rect->x, rect->height };

}

Rect CutRight(Rect* rect, int length) {

int oldWidth = rect->width;

rect->width = max(rect->x, rect->width - length);

rect->x += rect->width;

if (rect->x > width) rect->x = width;

return (Rect) { rect->width, rect->y, oldWidth, rect->height };

}

Rect CutTop(Rect* rect, int length) {

int oldY = rect->y;

rect->y = min(rect->height, rect->y + length);

rect->height -= rect->y;

if (rect->height < 0) rect->y = 0;

return (Rect) { rect->x, oldY, rect->width, rect->y };

}

Rect CutBottom(Rect* rect, int length) {

int oldHeight = rect->height;

rect->height = max(rect->y, rect->height - length);

rect->y += rect->height;

if (rect->y > height) rect->y = height;

return (Rect) { rect->x, rect->height, rect->width, oldHeight };

}

@palexdev did it in his Java implementation too

Expand full comment
1 more comment...

No posts