12 |
* different chunks of code and having to convert between them. |
* different chunks of code and having to convert between them. |
13 |
* |
* |
14 |
* Revision history: |
* Revision history: |
15 |
|
* v1.3 - added the IsRectEmpty and the IntersectRect methods |
16 |
* v1.2 - added the setRect method |
* v1.2 - added the setRect method |
17 |
* v1.1 - added the PointInRect and the setRectEmpty methods |
* v1.1 - added the PointInRect and the setRectEmpty methods |
18 |
* |
* |
19 |
* @author <A href="mailto:vovansim@hotmail.com">Vovansim (aka Scorpion)</A> |
* @author <A href="mailto:vovansim@hotmail.com">Vovansim (aka Scorpion)</A> |
20 |
* @version 1.2 |
* @version 1.3 |
21 |
* @date May 26, 2003 |
* @date June 4, 2003 |
22 |
*/ |
*/ |
23 |
|
|
24 |
#include "IsoGeometryTools.h" |
#include "IsoGeometryTools.h" |
27 |
|
|
28 |
/** Creates a new POINT with the given coordinates. */ |
/** Creates a new POINT with the given coordinates. */ |
29 |
POINT CreatePoint(int x, int y) { |
POINT CreatePoint(int x, int y) { |
30 |
POINT result; |
POINT result; |
31 |
|
|
32 |
result.x = x; |
result.x = x; |
33 |
result.y = y; |
result.y = y; |
34 |
|
|
35 |
return result; |
return result; |
36 |
} |
} |
37 |
|
|
38 |
/** Makes an SDL_Rect out of the POINT passed in. Width and Height are 0. */ |
/** Makes an SDL_Rect out of the POINT passed in. Width and Height are 0. */ |
39 |
SDL_Rect* GetSDLRect(POINT* p) { |
SDL_Rect* GetSDLRect(POINT* p) { |
40 |
SDL_Rect* result = new SDL_Rect(); |
SDL_Rect* result = new SDL_Rect(); |
41 |
|
|
42 |
result->x = p->x; |
result->x = p->x; |
43 |
result->y = p->y; |
result->y = p->y; |
44 |
|
|
45 |
result->w = 0; |
result->w = 0; |
46 |
result->h = 0; |
result->h = 0; |
47 |
|
|
48 |
return result; |
return result; |
49 |
} |
} |
50 |
|
|
51 |
bool PointInRect(RECT* r, POINT* p) { |
bool PointInRect(RECT* r, POINT* p) { |
52 |
if(p->x <= r->left) { |
if(p->x <= r->left) { |
53 |
return false; |
return false; |
54 |
} |
} |
55 |
|
|
56 |
if(p->x > r->right) { |
if(p->x > r->right) { |
57 |
return false; |
return false; |
58 |
} |
} |
59 |
|
|
60 |
if(p->y <= r->top) { |
if(p->y <= r->top) { |
61 |
return false; |
return false; |
62 |
} |
} |
63 |
|
|
64 |
if(p->y > r->bottom) { |
if(p->y > r->bottom) { |
65 |
return false; |
return false; |
66 |
} |
} |
67 |
|
|
68 |
return true; |
return true; |
69 |
} |
} |
70 |
|
|
71 |
/////////////////////////////// End POINT tools /////////////////////////////// |
/////////////////////////////// End POINT tools /////////////////////////////// |
76 |
|
|
77 |
/** Sets all the datat members of the RECT passed in to zero. */ |
/** Sets all the datat members of the RECT passed in to zero. */ |
78 |
void setRectEmpty(RECT* rectangle) { |
void setRectEmpty(RECT* rectangle) { |
79 |
rectangle->bottom = 0; |
rectangle->bottom = 0; |
80 |
rectangle->left = 0; |
rectangle->left = 0; |
81 |
rectangle->right = 0; |
rectangle->right = 0; |
82 |
rectangle->top = 0; |
rectangle->top = 0; |
83 |
} |
} |
84 |
|
|
85 |
/** Sets the data members of the RECT passed in to the values passed in. */ |
/** Sets the data members of the RECT passed in to the values passed in. */ |
86 |
void setRect(RECT* rectangle, long left, long top, long right, long bottom) { |
void setRect(RECT* rectangle, long left, long top, long right, long bottom) { |
87 |
rectangle->bottom = bottom; |
rectangle->bottom = bottom; |
88 |
rectangle->left = left; |
rectangle->left = left; |
89 |
rectangle->right = right; |
rectangle->right = right; |
90 |
rectangle->top = top; |
rectangle->top = top; |
91 |
} |
} |
92 |
|
|
93 |
/** Copies the source rectangle into the destination rectangle. */ |
/** Copies the source rectangle into the destination rectangle. */ |
94 |
void CopyRect(RECT* destination, RECT* source) { |
void CopyRect(RECT* destination, RECT* source) { |
95 |
destination->bottom = source->bottom; |
destination->bottom = source->bottom; |
96 |
destination->left = source->left; |
destination->left = source->left; |
97 |
destination->right = source->right; |
destination->right = source->right; |
98 |
destination->top = source->top; |
destination->top = source->top; |
99 |
} |
} |
100 |
|
|
101 |
/** Moves the rectangle passed in by the values passed in. */ |
/** Moves the rectangle passed in by the values passed in. */ |
102 |
void OffsetRect(RECT* rectangle, int dx, int dy) { |
void OffsetRect(RECT* rectangle, int dx, int dy) { |
103 |
rectangle->left += dx; |
rectangle->left += dx; |
104 |
rectangle->right += dx; |
rectangle->right += dx; |
105 |
|
|
106 |
rectangle->top += dy; |
rectangle->top += dy; |
107 |
rectangle->bottom += dy; |
rectangle->bottom += dy; |
108 |
} |
} |
109 |
|
|
110 |
void OffsetRect(RECT* rectangle, POINT point) { |
void OffsetRect(RECT* rectangle, POINT point) { |
111 |
OffsetRect(rectangle, point.x, point.y); |
OffsetRect(rectangle, point.x, point.y); |
112 |
|
} |
113 |
|
|
114 |
|
/** Returns true if the rectangle passed in has the width and / or |
115 |
|
height of 0, and false otherwise. */ |
116 |
|
bool IsRectEmpty(RECT* rectangle) { |
117 |
|
if((rectangle->right - rectangle->left) == 0) { |
118 |
|
return true; |
119 |
|
} |
120 |
|
|
121 |
|
if((rectangle->bottom - rectangle->top) == 0) { |
122 |
|
return true; |
123 |
|
} |
124 |
|
|
125 |
|
return false; |
126 |
|
} |
127 |
|
|
128 |
|
/** Puts the intersection of the two rectangles passed in into the |
129 |
|
* destination rectangle |
130 |
|
* |
131 |
|
* @note The intersection is the largest rectangle contained in both |
132 |
|
* existing rectangles. |
133 |
|
* @return True if the intersection is non-empty, and false if it is. |
134 |
|
*/ |
135 |
|
bool IntersectRect(RECT* destination, RECT* rectangle1, RECT* rectangle2) { |
136 |
|
//Take care of the left edge of the rectangle: |
137 |
|
if(rectangle1->left > rectangle2->left && |
138 |
|
rectangle1->left <= rectangle2->right) { |
139 |
|
destination->left = rectangle1->left; |
140 |
|
} else if(rectangle2->left > rectangle1->left && |
141 |
|
rectangle2->left <= rectangle1->right) { |
142 |
|
destination->left = rectangle2->left; |
143 |
|
} else { |
144 |
|
destination->left = 0; |
145 |
|
} |
146 |
|
|
147 |
|
//Take care of the top edge of the rectangle: |
148 |
|
if(rectangle1->top > rectangle2->top && |
149 |
|
rectangle1->top <= rectangle2->bottom) { |
150 |
|
destination->top = rectangle1->top; |
151 |
|
} else if(rectangle2->top > rectangle1->top && |
152 |
|
rectangle2->top <= rectangle1->bottom) { |
153 |
|
destination->top = rectangle2->top; |
154 |
|
} else { |
155 |
|
destination->top = 0; |
156 |
|
} |
157 |
|
|
158 |
|
//Take care of the right edge of the rectangle: |
159 |
|
if(rectangle1->right > rectangle2->left && |
160 |
|
rectangle1->right <= rectangle2->right) { |
161 |
|
destination->right = rectangle1->right; |
162 |
|
} else if(rectangle2->right > rectangle1->left && |
163 |
|
rectangle2->right <= rectangle1->right) { |
164 |
|
destination->right = rectangle2->right; |
165 |
|
} else { |
166 |
|
destination->right = 0; |
167 |
|
} |
168 |
|
|
169 |
|
//Take care of the bottom edge of the rectangle: |
170 |
|
if(rectangle1->bottom > rectangle2->top && |
171 |
|
rectangle1->bottom <= rectangle2->bottom) { |
172 |
|
destination->bottom = rectangle1->bottom; |
173 |
|
} else if(rectangle2->bottom > rectangle1->top && |
174 |
|
rectangle2->bottom <= rectangle1->bottom) { |
175 |
|
destination->bottom = rectangle2->bottom; |
176 |
|
} else { |
177 |
|
destination->bottom = 0; |
178 |
|
} |
179 |
|
|
180 |
|
return !IsRectEmpty(destination); |
181 |
} |
} |
182 |
|
|
183 |
/** Makes an SDL_Rect out of a RECT. */ |
/** Makes an SDL_Rect out of a RECT. */ |
184 |
SDL_Rect* GetSDLRect(RECT* r) { |
SDL_Rect* GetSDLRect(RECT* r) { |
185 |
SDL_Rect* result = new SDL_Rect(); |
SDL_Rect* result = new SDL_Rect(); |
186 |
|
|
187 |
result->x = r->left; |
result->x = r->left; |
188 |
result->y = r->top; |
result->y = r->top; |
189 |
result->w = r->right - r->left; |
result->w = r->right - r->left; |
190 |
result->h = r->bottom - r->top; |
result->h = r->bottom - r->top; |
191 |
|
|
192 |
return result; |
return result; |
193 |
} |
} |
194 |
|
|
195 |
/////////////////////////////// End RECT tools //////////////////////////////// |
/////////////////////////////// End RECT tools //////////////////////////////// |