00001
00011 #pragma once
00012
00013 #include "MatrixTypes.h"
00014
00016 class Shape2d { };
00017
00018
00025 template< typename T >
00026 class Rect : public Shape2d
00027 {
00028 public:
00029 Rect() {}
00030 Rect (T left_in, T right_in, T top_in, T bottom_in)
00031 {
00032 left = left_in;
00033 right = right_in;
00034 top = top_in;
00035 bottom = bottom_in;
00036 }
00037
00038 void expandToEnclosePoint (T x, T y)
00039 {
00040 left = MIN (x, left);
00041 bottom = MIN (y, bottom);
00042
00043 right = MAX (x, right);
00044 top = MAX (y, top);
00045 }
00046
00047 Rect< T > intersectRect (const Rect< T > &r)
00048 {
00049 Rect< T > result;
00050 result.left = MAX (left, r.left);
00051 result.bottom = MAX (bottom, r.bottom);
00052
00053 result.right = MIN (right, r.right);
00054 result.top = MIN (top, r.top);
00055
00056 return result;
00057 }
00058
00059 bool hasPositiveArea()
00060 {
00061 return (right > left && top > bottom);
00062 }
00063
00064 T left, right;
00065 T top, bottom;
00066 };
00067
00068
00069
00070
00072 template< typename T >
00073 class LineSegment : public Shape2d
00074 {
00075 public:
00076 typedef LineSegment< T > type;
00077 LineSegment(T P_in, T Q_in)
00078 {
00079 P = P_in;
00080 Q = Q_in;
00081
00082 T r = P + Q;
00083 }
00084
00085 type lerp(float t, const type & otherLine)
00086 {
00087 T newP = P.linearlyInterpolate (otherLine.P, t);
00088 T newQ = Q.linearlyInterpolate (otherLine.Q, t);
00089
00090 return LineSegment< T >(newP, newQ);
00091 }
00092
00093 void scale (float xScalingFactor, float yScalingFactor)
00094 {
00095 Q[0] *= xScalingFactor;
00096 Q[1] *= yScalingFactor;
00097
00098 P[0] *= xScalingFactor;
00099 P[1] *= yScalingFactor;
00100 }
00101
00102 T P;
00103 T Q;
00104 };