MoreRect.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "MorePrefix.h"
00018
00019 #include "MoreRect.h"
00020
00021 #include "MorePrivate.h"
00022
00023 void MPtAddPoints( const PointType* point1P, const PointType* point2P,
00024 PointType* point3P )
00025 {
00026 *point3P = *point1P;
00027 point3P->x += point2P->x;
00028 point3P->y += point2P->y;
00029 }
00030
00031 void MPtAddPoint( PointType* point1P, const PointType* point2P )
00032 {
00033 point1P->x += point2P->x;
00034 point1P->y += point2P->y;
00035 }
00036
00037 void MPtSubtractPoint( PointType* point1P, const PointType* point2P )
00038 {
00039 point1P->x -= point2P->x;
00040 point1P->y -= point2P->y;
00041 }
00042
00043 void MRctPtsToRect( const PointType* point1P, const PointType* point2P,
00044 RectangleType* resultRectP )
00045 {
00046 Coord leftMost;
00047 Coord rightMost;
00048 Coord topMost;
00049 Coord bottomMost;
00050 if ( point1P->x < point2P->x )
00051 {
00052 leftMost = point1P->x;
00053 rightMost = point2P->x;
00054 }
00055 else
00056 {
00057 leftMost = point2P->x;
00058 rightMost = point1P->x;
00059 }
00060 if ( point1P->y < point2P->y )
00061 {
00062 topMost = point1P->y;
00063 bottomMost = point2P->y;
00064 }
00065 else
00066 {
00067 topMost = point1P->y;
00068 bottomMost = point1P->y;
00069 }
00070 resultRectP->topLeft.x = leftMost;
00071 resultRectP->topLeft.y = topMost;
00072 resultRectP->extent.x = rightMost - leftMost;
00073 resultRectP->extent.y = bottomMost - topMost;
00074 }
00075
00076
00077 void MRctGetBottomRight( const RectangleType* rectP, PointType* bottomRight )
00078 {
00079 *bottomRight = rectP->topLeft;
00080 MPtAddPoint( bottomRight, &rectP->extent );
00081 }
00082
00083 Coord MRctFindXCenter( const RectangleType* rectP )
00084 {
00085 return rectP->topLeft.x + (rectP->extent.x >> 1);
00086 }
00087
00088 Coord MRctFindYCenter( const RectangleType* rectP )
00089 {
00090 return rectP->topLeft.y + (rectP->extent.y >> 1);
00091 }
00092
00093 Coord MRctFindXCenterOf2( const RectangleType* rect1P,
00094 const RectangleType* rect2P )
00095 {
00096 RectangleType rect3;
00097 MRctGetUnion( rect1P, rect2P, &rect3 );
00098 return rect3.topLeft.x + (rect3.extent.x >> 1);
00099 }
00100
00101 Coord MRctFindYCenterOf2( const RectangleType* rect1P,
00102 const RectangleType* rect2P )
00103 {
00104 RectangleType rect3;
00105 MRctGetUnion( rect1P, rect2P, &rect3 );
00106 return rect3.topLeft.y + (rect3.extent.y >> 1);
00107 }
00108
00109 void MRctGetUnion( const RectangleType* rect1P, const RectangleType* rect2P,
00110 RectangleType* rect3P )
00111 {
00112 AbsRectType absRect1, absRect2, absRect3;
00113 MRctRectToAbsRect( rect1P, &absRect1 );
00114 MRctRectToAbsRect( rect2P, &absRect2 );
00115 absRect3.left = _min( absRect1.left, absRect2.left );
00116 absRect3.top = _min( absRect1.top, absRect2.top );
00117 absRect3.right = _max( absRect1.right, absRect2.right );
00118 absRect3.bottom = _max( absRect1.bottom, absRect2.bottom );
00119 MRctAbsRectToRect( &absRect3, rect3P );
00120 }
00121
00122 void MRctRectToAbsRect( const RectangleType* rectP, AbsRectType* absRectP )
00123 {
00124 absRectP->left = rectP->topLeft.x;
00125 absRectP->top = rectP->topLeft.y;
00126 absRectP->right = rectP->topLeft.x + rectP->extent.x;
00127 absRectP->bottom = rectP->topLeft.y + rectP->extent.y;
00128 }
00129
00130 void MRctAbsRectToRect( const AbsRectType* absRectP, RectangleType* rectP )
00131 {
00132 rectP->topLeft.x = absRectP->left;
00133 rectP->topLeft.y = absRectP->top;
00134 rectP->extent.x = absRectP->right - absRectP->left;
00135 rectP->extent.y = absRectP->bottom - absRectP->top;
00136 }