|
25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more... |
Home Projects Up Sitemap Search Blog Forum+Chat About Us Privacy Terms of Use Feedback FAQ Images Services Ads Donate Humor |
|
|
C++ class for rectangles with dimensions as doublesDoubleRect is a C++ class like the Borland ObjectWindows Library (OWL) TRect, but with doubles used for dimensions and for all calculations. Some of its behavior is different from TRect because it is not intended for use with window regions. |
|
/* doubrect.h 7-24-00
Copyright (C)1995-2000, 2006 Steven Whitney.
Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.
like TRect (except as noted), but with doubles.
can hold coordinates for a region of the Mandelbrot set;
points within the set are complex instead of TPoint because TPoint only uses ints.
------
Notes:
--One function, which uses TRect, is unavailable when using this class under MSDOS.
*/
#ifndef __DOUBRECT_H
#define __DOUBRECT_H
#include <complex.h>
#if defined(_Windows)
#include <owl\point.h>
#endif
#include "c:\bcs\my.h"
#pragma hdrstop
//////////////////////////////////////////////////////////////////////////////
struct DoubleRect
{
// default values define the Mandelbrot set: note it's a square region
DoubleRect(double l = -2, double t = 1.25, double r = .5, double b = -1.25);
DoubleRect(complex tl, complex br);
DoubleRect(const DoubleRect& other) { *this = other; }
#if defined(_Windows)
DoubleRect(const TRect& other); // removing this should make it MSDOS-compatible
#endif
DoubleRect& operator = (const DoubleRect& other); // assignment
BOOL operator == (const DoubleRect& other) const;
BOOL operator != (const DoubleRect& other) const { return(!(*this == other)); }
friend ostream& operator << (ostream&, DoubleRect&); // put-to operator
friend istream& operator >> (istream&, DoubleRect&); // get-from operator
// functions
complex TopLeft(); // the corner points of the rectangle
complex TopRight();
complex BottomLeft();
complex BottomRight();
complex MidLeft(); // the midpoints of the boundaries
complex MidTop();
complex MidRight();
complex MidBottom();
complex Center(); // the centerpoint of the rectangle
double Height();
double Width();
double Area();
void Normalize(); // ensure top < bottom, left < right
BOOL Contains(const DoubleRect& m); // borders ARE part of the rectangle. TRUE if identical
BOOL Contains(complex c); // also TRUE if lands ON a boundary
DoubleRect TopLeftQuad(); // calculate quadrants of this DoubleRect
DoubleRect TopRightQuad();
DoubleRect BottomLeftQuad();
DoubleRect BottomRightQuad();
DoubleRect CenterQuad();
DoubleRect IsCenterQuadOf(); // (larger) region of which this one is the CenterQuad
DoubleRect FlippedVertical();
DoubleRect FlippedHorizontal();
// variables
double left; // boundaries of the rectangle
double top;
double right;
double bottom;
};
//////////////////////////////////////////////////////////////////////////////
#endif // __DOUBRECT_H
/* doubrect.cpp 7-6-00
Copyright (C)1995-2000, 2006 Steven Whitney.
Published under GNU GPL (General Public License) Version 2, with ABSOLUTELY NO WARRANTY.
like TRect (except as noted), but with doubles.
can hold coordinates for a region of the Mandelbrot set;
points within the set are complex instead of TPoint because TPoint only uses ints.
*/
#include "c:\bcs\library\doubrect.h"
//////////////////////////////////////////////////////////////////////////////
// code for class DoubleRect
//----------------------------------------------------------------------------
// constructor
DoubleRect::DoubleRect(double l, double t, double r, double b) :
left(l), top(t), right(r), bottom(b)
{
Normalize();
}
//----------------------------------------------------------------------------
// constructor
DoubleRect::DoubleRect(complex tl, complex br) :
left(real(tl)), top(imag(tl)), right(real(br)), bottom(imag(br))
{
Normalize();
}
//----------------------------------------------------------------------------
BOOL DoubleRect::operator == (const DoubleRect& other) const
{
DoubleRect a(*this), b(other); // the copies are Normalized
return((a.left == b.left) && (a.top == b.top) &&
(a.right == b.right) && (a.bottom == b.bottom));
}
//----------------------------------------------------------------------------
// removing this fn should make the class MSDOS-compatible instead of Windows/OWL-only.
#if defined(_Windows)
DoubleRect::DoubleRect(const TRect& r) :
left(r.left), top(r.top), right(r.right), bottom(r.bottom)
{
Normalize();
}
#endif
//----------------------------------------------------------------------------
// a normal DoubleRect has higher Y value at TOP, UNLIKE a Normalized() TRect.
void DoubleRect::Normalize()
{
if(left > right) swap(left,right);
if(bottom > top) swap(bottom,top);
}
//----------------------------------------------------------------------------
double DoubleRect::Height() { Normalize(); return(top - bottom); }
double DoubleRect::Width() { Normalize(); return(right - left); }
double DoubleRect::Area() { return(Height() * Width()); }
//----------------------------------------------------------------------------
// differs from its TRect counterpart: all borders ARE part of the rectangle
// also TRUE if the two rectangles are identical
BOOL DoubleRect::Contains(const DoubleRect& m)
{
return((left <= m.left) && (right >= m.right) && (top >= m.top) && (bottom <= m.bottom));
}
//----------------------------------------------------------------------------
// also TRUE if the point lands ON a boundary
BOOL DoubleRect::Contains(complex c)
{
return((left <= real(c)) && (right >= real(c)) && (top >= imag(c)) && (bottom <= imag(c)));
}
//----------------------------------------------------------------------------
// the corner points of the rectangle
complex DoubleRect::TopLeft() { return(complex(left,top)); }
complex DoubleRect::TopRight() { return(complex(right,top)); }
complex DoubleRect::BottomLeft() { return(complex(left,bottom)); }
complex DoubleRect::BottomRight() { return(complex(right,bottom)); }
//----------------------------------------------------------------------------
// the midpoints of the boundaries
complex DoubleRect::MidLeft() { return(complex(left,top - Height() / 2.)); }
complex DoubleRect::MidTop() { return(complex(left + Width() / 2., top)); }
complex DoubleRect::MidRight() { return(complex(right, top - Height() / 2.)); }
complex DoubleRect::MidBottom() { return(complex(left + Width() / 2., bottom)); }
//----------------------------------------------------------------------------
// the centerpoint
complex DoubleRect::Center() { return(complex(left + Width() / 2., top - Height() / 2.)); }
//----------------------------------------------------------------------------
// assignment
DoubleRect& DoubleRect::operator = (const DoubleRect& other)
{
if(&other != this)
{
left = other.left;
top = other.top;
right = other.right;
bottom = other.bottom;
}
Normalize();
return(*this);
}
//----------------------------------------------------------------------------
// output to any ostream, format: left top right bottom
ostream& operator << (ostream& os, DoubleRect& r)
{
os << setprecision(16) << r.left << " " << setprecision(16) << r.top << " "
<< setprecision(16) << r.right << " " << setprecision(16) << r.bottom;
return os;
}
//----------------------------------------------------------------------------
// input from any istream, format: left top right bottom\n
istream& operator >> (istream& is, DoubleRect& r)
{
is >> r.left >> r.top >> r.right >> r.bottom;
return is;
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::TopLeftQuad()
{
Normalize();
return DoubleRect(left,top,left + Width()/2.,top - Height()/2.);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::TopRightQuad()
{
Normalize();
return DoubleRect(left + Width()/2.,top,right,top - Height()/2.);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::BottomLeftQuad()
{
Normalize();
return DoubleRect(left,top - Height()/2.,left + Width()/2.,bottom);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::BottomRightQuad()
{
Normalize();
return DoubleRect(left + Width()/2.,top - Height()/2.,right,bottom);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::CenterQuad()
{
Normalize();
return DoubleRect(left + Width()/4.,top - Height()/4.,
right - Width()/4.,bottom + Height()/4.);
}
//----------------------------------------------------------------------------
// returns region of which this one is the CenterQuad (effectively zooms out)
// returned region has 4x the area, enlarged 2x in both directions.
DoubleRect DoubleRect::IsCenterQuadOf()
{
Normalize();
return DoubleRect(left - Width()/2.,top + Height()/2.,
right + Width()/2.,bottom - Height()/2.);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::FlippedVertical()
{
Normalize();
return DoubleRect(left, -bottom, right, -top);
}
//----------------------------------------------------------------------------
DoubleRect DoubleRect::FlippedHorizontal()
{
Normalize();
return DoubleRect(-right, top, -left, bottom);
}
//----------------------------------------------------------------------------
// end class DoubleRect
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|