## 1.4 幾何輔助類
與 GDI 的類似,在 GDI+ API 中也定義了許多繪圖的輔助類,常用的有點、大小和矩形 等幾何類。它們都是沒有基類的獨立類,被定義在頭文件 GdiplusTypes.h 中。與 GDI 不同 的是,在 GDI+中新增加了浮點型的幾何類。
浮點數版的幾何對象和繪圖方法,是 GDI+新增的功能,這些在各種工程技術領域都非 常有用。因為一般的實際圖形設計,都是基于實數坐標的。包括機械(../輪船/飛機 等)、建筑(../道路/堤壩等)和圖形動畫設計(../../軌跡等)等設 計,都必須使用浮點參數和坐標系。
下面對 GDI+的幾何輔助類,逐個進行簡單的介紹。
### 1.4.1 Point[F](點)
GDI+中,有兩種類型的點:整數點(對應于 Point 類,與 GDI 的 MFC 類 CPoint 類似) 和浮點數點(對應于 PointF 類),下面分別加以介紹。
(1)整數點類 Point
```
class Point
{
public:
Point() {X = Y = 0;}
Point(const Point &point) {X = point.X; Y = point.Y;}
Point(const Size &size) {X = size.Width; Y = size. Height;}
Point(INT x, INT y) {X = x; Y = y;}
Point operator+(const Point& point) const {return Point(X + point.X, Y + point.Y);}
Point operator-(const Point& point) const {return Point(X - point.X, Y - point.Y);}
BOOL Equals(const Point& point) {return (X == point.X) && (Y == point.Y);}
public:
INT X; INT Y; // 大寫 X、Y
};
```
其中:typedef int INT; 為 4 字節有符號整數(windef.h)。 注意,GDI+的點與 GDI 的區別:POINT 和 CPoint 采用小寫的 x、y。
(2)浮點數點類 PointF
```
class PointF
{
public:
PointF() {X = Y = 0.0f;}
PointF(REAL x, REAL y) {X = x; Y = y;}
... // 與整數版的類似
public:
REAL X; REAL Y;
};
```
其中:typedef float REAL; 為 4 字節浮點數(GdiplusTypes.h)。
### 1.4.2 Size[F](大小)
GDI+中,也有兩種類型的大小(尺寸):整數大小(對應于 Size 類,與 GDI 的 MFC 類 CSize 類似)和浮點數大小(對應于 SizeF 類)。下面分別加以介紹:
(1)整數大小類 Size:
class Size
{
public:
Size() {Width = Height = 0;}
Size(INT width, INT height) {Width = width; Height = height;}
...
public:
INT Width;
INT Height; // 寬和高,不再是 cx 和 cy
};
注意,這里的大小與 GDI 的區別:SIZE 和 CSize 的分量成員為 cx 和 cy,不是寬和高。
(2)浮點數大小類 SizeF:
```
class SizeF
{
public:
SizeF() {Width = Height = 0.0f;}
SizeF(REAL width, REAL height) {Width = width; Height = height;}
...
public:
REAL Width; REAL Height;
};
```
### 1.4.3 Rect[F](矩形)
GDI+中,也有兩種類型的矩形:整數矩形(對應于 Rect 類,與 GDI 的 MFC 類 CRect 類似)和浮點數矩形(對應于 RectF 類),下面分別加以介紹。
(1)整數矩形類 Rect:
```
class Rect
{
public:
Rect() {X = Y = Width = Height = 0;}
Rect(INT x, INT y, INT width, INT height);
...
INT GetLeft() const {return X;} INT GetTop() const {return Y;}
INT GetRight() const {return X+Width;} INT GetBottom() const {return Y+Height;}
BOOL IsEmptyArea() const{return (Width <= 0) || (Height <= 0);} BOOL Equals(const Rect & rect) const;
BOOL Contains(INT x, INT y) const; BOOL **Contains**(const Point& pt) const; BOOL Contains(Rect& rect) const;
...
VOID Offset(const Point& point); VOID Offset(INT dx, INT dy);
public:
INT X; INT Y; // 大寫的 X 和 Y(為矩形左上角的坐標),不再是 left 和 top
INT Width;
INT Height; // 寬和高,不再是 right 和 bottom
};
```
注意,這里的矩形與 GDI 的區別:RECT 和 CRect 的分量成員是左、頂、右、底而不 是 X、Y、寬、高。雖然 Rect 中的(X, Y)等價于 RECT 的(left, top),但是 Rect 中的(Width, Height) 卻不同于 RECT 的(right, bottom)。
(2)浮點數矩形類 RectF:
```
class RectF
{
public:
RectF() {X = Y = Width = Height = 0.0f;}
RectF(REAL x, REAL y, REAL width, REAL height);
...
public:
REAL X;
REAL Y;
REAL Width;
REAL Height;
};
```
在 GDI 的 MFC 封裝中,除了定義有點、大小和矩形的類外,還保留了對應的 API 結構 POINT、SIZE 和 RECT,主要是考慮運行效率及與底層 GDI API 的兼容。
比較可知,GDI 和 GDI+都有對應的幾何類,不過 GDI+沒有對應的結構(但有新增加 的浮點數版類),而 GDI 則沒有對應的浮點數版類(但卻有對應的結構)。