OWLNext    7.0
Borland's Object Windows Library for the modern age
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
geometry.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland WinSys Library
3// Copyright (c) 1993, 1996 by Borland International, All Rights Reserved
4//
5/// \file
6/// Implementation of classes for windowing system geometry
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
9#include <owl/defs.h>
10#include <owl/geometry.h>
11#include <windowsx.h>
12
13namespace owl {
14
15// Let the compiler know that the following template instances will be defined elsewhere.
16//#pragma option -Jgx
17
18//
19// Calculates the integer square root of a 32bit signed long. Returns a 16bit
20// signed. Is fairly fast, esp. compared to FP versions
21//
24{
25 if (val <= 0)
26 return 0; // Could throw a math exception?
27
28 uint mask = 0x4000; // Bit mask to shift right
29 int best = 0; // Best estimate so far
30
31 for (; mask; mask >>= 1)
32 if (long((static_cast<uint32>(static_cast<long>(best))+mask)*(static_cast<uint32>(static_cast<long>(best))+mask)) <= val)
33 best |= mask;
34
35 return int16(best);
36}
37
38_OWLFUNC(int)
40{
41 if (val <= int64(0l))
42 return 0; // Could throw a math exception?
43
44 uint mask = 0x40000000; // Bit mask to shift right
45 int64 best = 0l; // Best estimate so far
46 int64 temp;
47
48 for (; mask; mask >>= 1) {
49 temp = best | int64(mask);
50 if (temp * temp <= val)
51 best = temp;
52 }
53 return int32(best);
54}
55
56//
57/// Creates a TPoint object from a packed point.
58/// The given point must be encoded as in the LPARAM argument of a Windows message such as
59/// the WM_CONTEXTMENU message.
60//
66
67//
68/// Returns the distance between the origin and the point.
69//
71{
72 int64 x64 = x;
73 int64 y64 = y;
74 return Sqrt(x64 * x64 + y64 * y64);
75}
76
77//
78/// Returns the length of the diagonal of the rectangle represented by this object.
79/// The value returned is an int approximation to the square root of (cx2 + cy2).
80//
82{
83 int64 x64 = cx;
84 int64 y64 = cy;
85 return Sqrt(x64 * x64 + y64 * y64);
86}
87
88//
89/// Normalizes this rectangle by switching the left and right data member values if
90/// left > right, and switching the top and bottom data member values if top >
91/// bottom. Normalize returns the normalized rectangle. A valid but nonnormal
92/// rectangle might have left > right or top > bottom or both. In such cases, many
93/// manipulations (such as determining width and height) become unnecessarily
94/// complicated. Normalizing a rectangle means interchanging the corner point values
95/// so that left < right and top < bottom. The physical properties of a rectangle
96/// are unchanged by this process.
97//
98TRect&
100{
101 if (left > right)
102 Swap(left, right);
103 if (top > bottom)
104 Swap(top, bottom);
105 return *this;
106}
107
108//
109/// Changes this rectangle so its corners are offset by the given delta values. The
110/// revised rectangle has a top left corner at (left + dx, top + dy) and a right
111/// bottom corner at (right + dx, bottom + dy). The revised rectangle is returned.
112//
113TRect&
115{
116 left += dx;
117 top += dy;
118 right += dx;
119 bottom += dy;
120 return *this;
121}
122
123//
124/// Inflates a rectangle inflated by the given delta arguments.
125/// The top left corner of the returned rectangle is (left - dx, top - dy),
126/// while its bottom right corner is (right + dx, bottom + dy).
127//
128TRect&
130{
131 left -= dx;
132 top -= dy;
133 right += dx;
134 bottom += dy;
135 return *this;
136}
137//
138// Inflate the rectangle so that new top left point is (left-dx, top-dy) and
139// the new bottom right point is (right+dx, bottom+dy).
140//
141TRect&
143{
144 return Inflate(-dx,-dy);
145}
146
147//
148/// Changes this rectangle to its intersection with the other rectangle. This
149/// rectangle object is returned. Returns a NULL rectangle if there is no
150/// intersection.
151//
152TRect&
154{
155 if (!IsNull()) {
156 if (other.IsNull())
157 SetNull();
158 else {
159 left = std::max(left, other.left);
160 top = std::max(top, other.top);
161 right = std::min(right, other.right);
162 bottom = std::min(bottom, other.bottom);
163 }
164 }
165 return *this;
166}
167
168//
169/// Changes this rectangle to its union with the other rectangle. This rectangle
170/// object is returned.
171//
172TRect&
174{
175 if (!other.IsNull()) {
176 if (IsNull())
177 *this = other;
178 else {
179 left = std::min(left, other.left);
180 top = std::min(top, other.top);
181 right = std::max(right, other.right);
182 bottom = std::max(bottom, other.bottom);
183 }
184 }
185 return *this;
186}
187
188//
189/// Determines the parts of this rect that do not lie within "other" region. The
190/// resulting rectangles are placed in the "result" array.
191/// Returns the resulting number of rectangles. This number will be 1, 2, 3, or 4.
192//
193int
194TRect::Subtract(const TRect& other, TRect result[]) const
195{
196 // Case of non-intersection, result is just this rectangle
197 //
198 if (!Touches(other)) {
199 result[0] = *this;
200 return 1;
201 }
202 // Check for up to four sub-rectangles produced
203 //
204 else {
205 int i = 0;
206
207 // Top piece of this rect
208 //
209 if (other.top > top) {
210 result[i].left = left;
211 result[i].top = top;
212 result[i].right = right;
213 result[i].bottom = other.top;
214 i++;
215 }
216
217 // Bottom piece of this rect
218 //
219 if (other.bottom < bottom) {
220 result[i].left = left;
221 result[i].top = other.bottom;
222 result[i].right = right;
223 result[i].bottom = bottom;
224 i++;
225 }
226
227 // Left piece of this rect
228 //
229 if (other.left > left) {
230 result[i].left = left;
231 result[i].top = std::max(top, other.top);
232 result[i].right = other.left;
233 result[i].bottom = std::min(bottom, other.bottom);
234 i++;
235 }
236
237 // Right piece of this rect
238 //
239 if (other.right < right) {
240 result[i].left = other.right;
241 result[i].top = std::max(top, other.top);
242 result[i].right = right;
243 result[i].bottom = std::min(bottom, other.bottom);
244 i++;
245 }
246 return i;
247 }
248}
249
250} // OWL namespace
251
int Magnitude() const
Returns the distance between the origin and the point.
Definition geometry.cpp:70
TPoint()
Constructs an uninitialized point.
Definition geometry.h:413
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
TRect & operator|=(const TRect &other)
Changes this rectangle to its union with the other rectangle.
Definition geometry.cpp:173
bool IsNull() const
Returns true if left, right, top, and bottom are all 0; otherwise, returns false.
Definition geometry.h:1167
TRect & Offset(int dx, int dy)
Changes this rectangle so its corners are offset by the given delta values.
Definition geometry.cpp:114
int Subtract(const TRect &other, TRect result[]) const
Determines the parts of this rect that do not lie within "other" region.
Definition geometry.cpp:194
TRect & Inflate(int dx, int dy)
Inflates a rectangle inflated by the given delta arguments.
Definition geometry.cpp:129
bool Touches(const TRect &other) const
Returns true if the other rectangle shares any interior points with this rectangle; otherwise,...
Definition geometry.h:1222
void SetNull()
Sets the left, top, right, and bottom of the rectangle to 0.
Definition geometry.h:1075
TRect & DeflateRect(int dx, int dy)
Definition geometry.cpp:142
TRect & operator&=(const TRect &other)
Changes this rectangle to its intersection with the other rectangle.
Definition geometry.cpp:153
TRect & Normalize()
Normalizes this rectangle by switching the left and right data member values if left > right,...
Definition geometry.cpp:99
int Magnitude() const
Returns the length of the diagonal of the rectangle represented by this object.
Definition geometry.cpp:81
Classes for window system geometry.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
__int64 int64
Definition number.h:36
signed long int32
Definition number.h:30
unsigned long uint32
Definition number.h:34
int16 Sqrt(int32 val)
Definition geometry.cpp:23
signed short int16
Definition number.h:29
unsigned int uint
Definition number.h:25
General definitions used by all ObjectWindows programs.
#define _OWLFUNC(p)
Definition defs.h:341
void Swap(T &a, T &b)
Definition defs.h:365