1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_POLYGON_H
24 #define COMMON_POLYGON_H
25 
26 #include "common/scummsys.h"
27 
28 #ifdef ENABLE_VKEYBD
29 
30 #include "common/array.h"
31 #include "common/rect.h"
32 
33 namespace Common {
34 
35 struct Polygon {
36 
PolygonPolygon37 	Polygon() {}
PolygonPolygon38 	Polygon(Array<Point> p) : _points(p) {
39 		for (uint i = 0; i < p.size(); i++) {
40 			_bound.extend(Rect(p[i].x, p[i].y, p[i].x, p[i].y));
41 		}
42 	}
PolygonPolygon43 	Polygon(Point *p, int n) {
44 		for (int i = 0; i < n; i++) {
45 			addPoint(p[i]);
46 		}
47 	}
48 
addPointPolygon49 	void addPoint(const Point &p) {
50 		_points.push_back(p);
51 		_bound.extend(Rect(p.x, p.y, p.x, p.y));
52 	}
53 
addPointPolygon54 	void addPoint(int16 x, int16 y) {
55 		addPoint(Point(x, y));
56 	}
57 
getPointCountPolygon58 	uint getPointCount() {
59 		return _points.size();
60 	}
61 
62 	/**
63 	 * Check if given position is inside this polygon.
64 	 *
65 	 * @param x the horizontal position to check
66 	 * @param y the vertical position to check
67 	 * @return true if the given position is inside this polygon, false otherwise
68 	*/
69 	bool contains(int16 x, int16 y) const;
70 
71 	/**
72 	 * Check if given point is inside this polygon.
73 	 *
74 	 * @param p the point to check
75 	 * @return true if the given point is inside this polygon, false otherwise
76 	*/
containsPolygon77 	bool contains(const Point &p) const {
78 		return contains(p.x, p.y);
79 	}
80 
moveToPolygon81 	void moveTo(int16 x, int16 y) {
82 		int16 dx = x - ((_bound.right + _bound.left) / 2);
83 		int16 dy = y - ((_bound.bottom + _bound.top) / 2);
84 		translate(dx, dy);
85 	}
86 
moveToPolygon87 	void moveTo(const Point &p) {
88 		moveTo(p.x, p.y);
89 	}
90 
translatePolygon91 	void translate(int16 dx, int16 dy) {
92 		Array<Point>::iterator it;
93 		for (it = _points.begin(); it != _points.end(); it++) {
94 			it->x += dx;
95 			it->y += dy;
96 		}
97 	}
98 
getBoundingRectPolygon99 	Rect getBoundingRect() const {
100 		return _bound;
101 	}
102 
103 private:
104 	Array<Point> _points;
105 	Rect _bound;
106 };
107 
108 } // End of namespace Common
109 
110 #endif // #ifdef ENABLE_VKEYBD
111 
112 #endif // #ifndef COMMON_POLYGON_H
113