home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 11.0 KB | 442 lines | [TEXT/MPS ] |
- // Geometry.cp
- // Copyright © 1985-1991 by Apple Computer, Inc. All rights reserved.
-
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __STDIO__
- #include <StdIo.h>
- #endif
-
- #ifndef __GEOMETRY__
- #include <Geometry.h>
- #endif
-
- #pragma segment Main
-
- //--------------------------------------------------------------------------------
- // This constructor for Point is being outlined by CFront. It claims there is an
- // expression to complex to be inline. I tried taking the space out between the
- // two curly braces…
- //--------------------------------------------------------------------------------
-
- Point::Point() { }
-
-
- //--------------------------------------------------------------------------------
- // Conversion operator, for converting Point to a textual representation of a
- // Point. "Point(v,h)". Formats Point in a static character string and returns a
- // pointer to that static character string.
- //--------------------------------------------------------------------------------
-
- Point::operator char*() const
- {
- static char textPoint[40];
-
- sprintf (textPoint, "Point(%d, %d)", v, h);
-
- return textPoint;
- }
-
-
- //--------------------------------------------------------------------------------
- // Selector operators for Point. Selects one of the two coordinates depending
- // on the value of the sel parameter.
- //--------------------------------------------------------------------------------
-
- short& Point::operator[](VHSelect sel) // For non-const Point
- {
- if (sel == vSel)
- return v;
- else
- return h;
- }
-
- const short& Point::operator[](VHSelect sel) const // For const Point
- {
- if (sel == vSel)
- return v;
- else
- return h;
- }
-
-
- //--------------------------------------------------------------------------------
- // Arithmatic operators for Point. Addition and subtraction are all that make
- // any sense. Both the Add and AddTo forms are defined.
- //--------------------------------------------------------------------------------
-
- Point Point::operator+(const Point& pt) const
- {
- Point returnPt;
-
- returnPt.v = v + pt.v;
- returnPt.h = h + pt.h;
- return returnPt;
- }
-
- Point Point::operator-(const Point& pt) const
- {
- Point returnPt;
-
- returnPt.v = v - pt.v;
- returnPt.h = h - pt.h;
- return returnPt;
- }
-
- Point& Point::operator+=(const Point& pt)
- {
- v += pt.v;
- h += pt.h;
- return *this;
- }
-
- Point& Point::operator-=(const Point& pt)
- {
- v -= pt.v;
- h -= pt.h;
- return *this;
- }
-
-
- //--------------------------------------------------------------------------------
- // Relational operators for Point. These are defined by applying the operator in
- // question to both coordinates. The condition must hold for both to hold for the
- // Points the corresponding Points.
- //--------------------------------------------------------------------------------
-
- Boolean Point::operator!=(const Point& pt) const
- {
- return v != pt.v || h != pt.h;
- }
-
- Boolean Point::operator==(const Point& pt) const
- {
- return v == pt.v && h == pt.h;
- }
-
- Boolean Point::operator>(const Point& pt) const
- {
- return v > pt.v && h > pt.h;
- }
-
- Boolean Point::operator<(const Point& pt) const
- {
- return v < pt.v && h < pt.h;
- }
-
- Boolean Point::operator>=(const Point& pt) const
- {
- return v >= pt.v && h >= pt.h;
- }
-
- Boolean Point::operator<=(const Point& pt) const
- {
- return v <= pt.v && h <= pt.h;
- }
-
-
- //--------------------------------------------------------------------------------
- // Conversion operator, for converting Rect to a textual representation of a
- // Rect. "Rect(top,left,bottom,right)". Formats Rect in a static character string
- // and returns a pointer to that static character string.
- //--------------------------------------------------------------------------------
-
- Rect::operator char*() const
- {
- static char textRect[40];
-
- sprintf (textRect, "Rect(%d, %d, %d, %d)", top, left, bottom, right);
-
- return textRect;
- }
-
-
- //--------------------------------------------------------------------------------
- // Selector operators for Point. Selects one of the two coordinates depending
- // on the value of the sel parameter.
- //--------------------------------------------------------------------------------
-
- Point& Rect::operator[](PointSelector sel)
- {
- if (sel == topLeft)
- return *((Point *) &top);
- else
- return *((Point *) &bottom);
- }
-
- const Point& Rect::operator[](PointSelector sel) const
- {
- if (sel == topLeft)
- return *((Point *) &top);
- else
- return *((Point *) &bottom);
- }
-
-
- //--------------------------------------------------------------------------------
- // Operators for adding and subtracting one Rect from to/from another. Both the
- // Add and AddTo form of operators are defined.
- //--------------------------------------------------------------------------------
-
- Rect Rect::operator+(const Rect& rt) const
- {
- Rect returnRect;
-
- returnRect.top = top + rt.top;
- returnRect.left = left + rt.left;
- returnRect.bottom = bottom + rt.bottom;
- returnRect.right = right + rt.right;
-
- return returnRect;
- }
-
- Rect Rect::operator-(const Rect& rt) const
- {
- Rect returnRect;
-
- returnRect.top = top - rt.top;
- returnRect.left = left - rt.left;
- returnRect.bottom = bottom - rt.bottom;
- returnRect.right = right - rt.right;
-
- return returnRect;
- }
-
- Rect& Rect::operator+=(const Rect& rt)
- {
- top += rt.top;
- left+= rt.left;
- bottom += rt.bottom;
- right += rt.right;
-
- return *this;
- }
-
-
- Rect& Rect::operator-=(const Rect& rt)
- {
- top -= rt.top;
- left-= rt.left;
- bottom-= rt.bottom;
- right-= rt.right;
-
- return *this;
- }
-
-
- //--------------------------------------------------------------------------------
- // Operators for adding and subtracting a Point to/from a Rect. A Point is added
- // to a Rect by adding the Point to both the top-left and bottom-right Points that
- // define the Rect. Both the Add and AddTo operators are defined. Very convenient
- // for translating Rects. These take a point and since Point has a constructor
- // that takes two shorts the Rect windowRect can be translated 100 pixels in the
- // positive y direction by the statement:
- //
- // windowRect = windowRect + Point (0, 100); or
- // windowRect += Point (0, 100);
- //--------------------------------------------------------------------------------
-
- Rect Rect::operator+(const Point& pt) const
- {
- Rect returnRect;
-
- returnRect.top = top + pt.v;
- returnRect.left = left + pt.h;
- returnRect.bottom = bottom + pt.v;
- returnRect.right = right + pt.h;
-
- return returnRect;
- }
-
- Rect Rect::operator-(const Point& pt) const
- {
- Rect returnRect;
-
- returnRect.top = top - pt.v;
- returnRect.left = left - pt.h;
- returnRect.bottom = bottom - pt.v;
- returnRect.right = right - pt.h;
-
- return returnRect;
- }
-
- Rect& Rect::operator+=(const Point& pt)
- {
- top += pt.v;
- left+= pt.h;
- bottom+= pt.v;
- right+= pt.h;
-
- return *this;
- }
-
- Rect& Rect::operator-=(const Point& pt)
- {
- top -= pt.v;
- left -= pt.h;
- bottom -= pt.v;
- right -= pt.h;
-
- return *this;
- }
-
-
- //--------------------------------------------------------------------------------
- // Inset a Rect using the coordinates in Point for the inset delta
- //--------------------------------------------------------------------------------
-
- Rect& Rect::Inset(const Point& delta)
- {
- top += delta.v;
- left += delta.h;
- bottom -= delta.v;
- right -= delta.h;
-
- return *this;
- }
-
-
- //--------------------------------------------------------------------------------
- // Equality operators, other relational operator could be defined such as <. But
- // their meaning is ambiguous and probably better implemented as methods. For
- // example, aRect < bRect could return true if aRect was inside of bRect, or could
- // return true if the area of aRect was less than the area of bRect.
- //--------------------------------------------------------------------------------
-
- Boolean Rect::operator==(const Rect& rt) const
- {
- return
- top == rt.top && left == rt.left &&
- bottom == rt.bottom && right == rt.right;
- }
-
- Boolean Rect::operator!=(const Rect& rt) const
- {
- return
- top != rt.top || left != rt.left ||
- bottom != rt.bottom || right != rt.right;
- }
-
-
- //--------------------------------------------------------------------------------
- // Two simple area operators, the intersection && (logical and in C++) and the
- // union || (logical or in C++). The definition of union here is to return a Rect
- // that exactly encloses its operands.
- //--------------------------------------------------------------------------------
-
- Rect Rect::operator&&(const Rect&) const
- {
- // Obviously unimplemented yet.
-
- Rect returnRect;
-
- returnRect.top = 0;
- returnRect.left = 0;
- returnRect.bottom = 0;
- returnRect.right = 0;
-
- return returnRect;
- }
-
- Rect Rect::operator||(const Rect& rt) const
- {
- Rect returnRect;
-
- returnRect.top = Min(top, rt.top);
- returnRect.left = Min(left, rt.left);
- returnRect.bottom = Max(bottom, rt.bottom);
- returnRect.right = Max(right, rt.right);
-
- return returnRect;
- }
-
-
- //--------------------------------------------------------------------------------
- // Returns true if a valid rectangle (left < right and top < bottom). If not
- // a valid rectangle then return false and set all coordinates to 0.
- //--------------------------------------------------------------------------------
-
- Boolean Rect::ValidRect()
- {
- if (left < right && top < bottom)
- return true;
- else
- {
- top = left = 0;
- bottom = right = 0;
- return false;
- }
- }
-
-
- //--------------------------------------------------------------------------------
- // Empty returns true if the rectangle is empty.
- //--------------------------------------------------------------------------------
-
- Boolean Rect::Empty() const
- {
- return left - right <= 0 && bottom - top <= 0;
- }
-
-
- //--------------------------------------------------------------------------------
- // Length returns the length of a Rect in a given dimension.
- //--------------------------------------------------------------------------------
-
- short Rect::Length(VHSelect sel) const
- {
- if (sel == vSel)
- return bottom - top;
- else
- return right - left;
- }
-
-
- //--------------------------------------------------------------------------------
- // Size returns the size of a Rect as a Point.
- //--------------------------------------------------------------------------------
-
- Point Rect::Size() const
- {
- return Point(bottom - top, right - left);
- }
-
-
- //--------------------------------------------------------------------------------
- // The Contains method takes either a Point or a Rect and returns true if the
- // operand is inside of the Rect the method is applied to.
- //--------------------------------------------------------------------------------
-
- Boolean Rect::Contains (const Point& pt) const
- {
- // Does the point 'pt' lie within the rectangle of 'this'?
-
- return pt.v >= top && pt.v <= bottom && pt.h >= left && pt.h <= right;
- }
-
- Boolean Rect::Contains (const Rect& rt) const
- {
- // Does the rectangle 'rt' lie withing the rectagle of 'this'?
-
- return Contains (rt[topLeft]) && Contains (rt[botRight]);
- }
-
-
- //--------------------------------------------------------------------------------
- // Max and Min are two private methods for comparing individual coordinates
- // of Rects.
- //--------------------------------------------------------------------------------
-
- short Rect::Min (const short a, const short b) const
- {
- return a < b ? a : b;
- }
-
- short Rect::Max (const short a, const short b) const
- {
- return a > b ? a : b;
- }
-