Skip to content

Instantly share code, notes, and snippets.

@karl-zylinski
Last active September 12, 2024 23:28
Show Gist options
  • Save karl-zylinski/ffccda0babb7e05b0657bf0acd3f1a99 to your computer and use it in GitHub Desktop.
Save karl-zylinski/ffccda0babb7e05b0657bf0acd3f1a99 to your computer and use it in GitHub Desktop.
For splitting up rectangles into smaller rectangles easily. Great for IMGUI layouting.
Rect :: struct {
x: f32,
y: f32,
width: f32,
height: f32,
}
cut_rect_top :: proc(r: ^Rect, y: f32, m: f32) -> Rect {
res := r^
res.y += m
res.height = y
r.y += y + m
r.height -= y + m
return res
}
cut_rect_bottom :: proc(r: ^Rect, h: f32, m: f32) -> Rect {
res := r^
res.height = h
res.y = r.y + r.height - h - m
r.height -= h + m
return res
}
cut_rect_left :: proc(r: ^Rect, x, m: f32) -> Rect {
res := r^
res.x += m
res.width = x
r.x += x + m
r.width -= x + m
return res
}
cut_rect_right :: proc(r: ^Rect, w, m: f32) -> Rect {
res := r^
res.width = w
res.x = r.x + r.width - w - m
r.width -= w + m
return res
}
split_rect_top :: proc(r: Rect, y: f32, m: f32) -> (top, bottom: Rect) {
top = r
bottom = r
top.y += m
top.height = y
bottom.y += y + m
bottom.height -= y + m
return
}
split_rect_left :: proc(r: Rect, x: f32, m: f32) -> (left, right: Rect) {
left = r
right = r
left.width = x
right.x += x + m
right.width -= x +m
return
}
split_rect_bottom :: proc(r: Rect, y: f32, m: f32) -> (top, bottom: Rect) {
top = r
top.height -= y + m
bottom = r
bottom.y = top.y + top.height + m
bottom.height = y
return
}
split_rect_right :: proc(r: Rect, x: f32, m: f32) -> (left, right: Rect) {
left = r
right = r
right.width = x
left.width -= x + m
right.x = left.x + left.width
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment