-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomp_rect.c
62 lines (53 loc) · 1.94 KB
/
comp_rect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "comp_rect.h"
/// Returns true, if r1 fully contains r2
static bool rect_contains(CompRect* r1, CompRect* r2){
return r1->x1 <= r2->x1 && r1->y1 <= r2->y1 &&
r1->x2 >= r2->x2 && r1->y2 >=r2->y2;
}
static bool rects_are_intersecting(CompRect* r1, CompRect* r2)
{
// if the left point of one rect is greater
// than the right one of the other, nothing intersects.
if(r1->x1 > r2->x2 || r2->x1 > r1->x2){
return false;
}
if(r1->y1 > r2->y2 || r2->y1 > r1->y2){
return false;
}
return true;
}
/// Check if we can omit painting a window (rect). E.g., a window
/// completely occluded by another one, does not need to be
/// painted. Further, we try to select the largest possible ignore region
/// Based on window and intersection areas.
bool rect_paint_needed(CompRect* ignore_reg, CompRect* reg){
if(rect_contains(ignore_reg, reg)){
// the ignore-region completely occludes the window.
return false;
}
if(! rects_are_intersecting(ignore_reg, reg)){
// KISS and just use the greater rect as new ignore region.
if(reg->w*reg->h > ignore_reg->w*ignore_reg->h){
*ignore_reg = *reg;
}
return true;
}
// calculate the intersection rect.
short x1 = (ignore_reg->x1 > reg->x1) ? ignore_reg->x1 : reg->x1;
short x2 = (ignore_reg->x2 < reg->x2) ? ignore_reg->x1 : reg->x1;
short y1 = (ignore_reg->y1 > reg->y1) ? ignore_reg->y1 : reg->y1;
short y2 = (ignore_reg->y2 < reg->y2) ? ignore_reg->y1 : reg->y1;
short w = x2 - x1;
short h = y2 - y1;
// KISS and just use the biggest rect as new ignore rect
if(reg->w*reg->h > ignore_reg->w*ignore_reg->h){
*ignore_reg = *reg;
}
if(w*h > ignore_reg->w*ignore_reg->h){
CompRect r_intersect = {.x1 = x1, .y1 = y1,
.x2 = x2, .y2 = y2,
.w = w, .h = h };
*ignore_reg = r_intersect;
}
return true;
}