]> git.vomp.tv Git - vompclient.git/blob - region.cc
10 CWFs
[vompclient.git] / region.cc
1 /*
2     Copyright 2020 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     VOMP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "region.h"
21
22 bool Region::overlappedBy(Region& d)
23 {
24   return
25   ( (d.x2() >= x) &&
26     (d.x <= x2()) &&
27     (d.y <= y2()) &&
28     (d.y2() >= y)    );
29 }
30
31 UINT Region::x2()
32 {
33   return x + w - 1;
34 }
35
36 UINT Region::y2()
37 {
38   return y + h - 1;
39 }
40
41 Region Region::operator + (Region& other)
42 {
43   Region toReturn;
44   
45   toReturn.x = (x <= other.x ? x : other.x);
46   toReturn.y = (y <= other.y ? y : other.y);
47   toReturn.w = ((x+w) >= (other.x+other.h) ? (x+w) : (other.x+other.h)) - toReturn.x;
48   toReturn.h = ((y+h) >= (other.y+other.h) ? (y+h) : (other.y+other.h)) - toReturn.y;
49   
50   return toReturn;
51 }
52
53 Region Region::subtract(Region& other)
54 {
55   //OK printf("This:           %i %i %i %i\n", x, y, w, h);
56   //OK printf("Subtract this:  %i %i %i %i\n", other.x, other.y, other.w, other.h);
57
58
59   Region s;
60
61   if (x < other.x)
62   {
63 //OK    printf("Case 1\n");
64     s.x = x;
65     s.y = y;
66     s.w = other.x - x;
67     s.h = h;
68   }
69   else if (x2() > other.x2())
70   {
71 //OK    printf("Case 2\n");
72     s.x = other.x2()+1;
73     s.y = y;
74     s.w = w - s.x;
75     s.h = h;
76   }
77   else if (y < other.y)
78   {
79 //OK    printf("Case 3\n");
80     s.x = x;
81     s.y = y;
82     s.w = w;
83     s.h = other.y - y;
84   }
85   else if (y2() > other.y2())
86   {
87 //OK    printf("Case 4\n");
88     s.x = x;
89     s.y = other.y2()+1;
90     s.w = w;
91     s.h = h - s.y;
92   }
93   else
94   {
95     s.x = 0;
96     s.y = 0;
97     s.w = 0;
98     s.h = 0;
99   }
100   //OK printf("Result:         %i %i %i %i\n", s.x, s.y, s.w, s.h);
101
102   return s;
103 }
104
105
106   //i.x = (x >= other.x ? x : other.x);
107   //i.y = (y >= other.y ? y : other.y);
108   //i.w = (x2() <= other.x2() ? x2() : other.x2()) - i.x;
109   //i.h = (y2() <= other.y2() ? y2() : other.y2()) - i.y;
110   //return i;