blob: c881bbd55068898dba21d0774221978125e520f7 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef PPAPI_CPP_POINT_H_
6#define PPAPI_CPP_POINT_H_
7
8#include "ppapi/c/pp_point.h"
9
10/// @file
11/// This file defines the API to create a 2 dimensional point.
12
13namespace pp {
14
15/// A 2 dimensional point with 0,0 being the upper-left starting coordinate.
16class Point {
17 public:
18 /// The default constructor for a point at 0,0.
19 Point() {
20 point_.x = 0;
21 point_.y = 0;
22 }
23
24 /// A constructor accepting two int32_t values for x and y and converting
25 /// them to a Point.
26 ///
27 /// @param[in] in_x An int32_t value representing a horizontal coordinate
28 /// of a point, starting with 0 as the left-most coordinate.
29 /// @param[in] in_y An int32_t value representing a vertical coordinate
30 /// of a point, starting with 0 as the top-most coordinate.
31 Point(int32_t in_x, int32_t in_y) {
32 point_.x = in_x;
33 point_.y = in_y;
34 }
35
36 /// A constructor accepting a pointer to a PP_Point and converting the
37 /// PP_Point to a Point. This is an implicit conversion constructor.
38 ///
39 /// @param[in] point A pointer to a PP_Point.
40 Point(const PP_Point& point) { // Implicit.
41 point_.x = point.x;
42 point_.y = point.y;
43 }
44
45 /// Destructor.
46 ~Point() {
47 }
48
49 /// A function allowing implicit conversion of a Point to a PP_Point.
50 /// @return A Point.
51 operator PP_Point() const {
52 return point_;
53 }
54
55 /// Getter function for returning the internal PP_Point struct.
56 ///
57 /// @return A const reference to the internal PP_Point struct.
58 const PP_Point& pp_point() const {
59 return point_;
60 }
61
62 /// Getter function for returning the internal PP_Point struct.
63 ///
64 /// @return A mutable reference to the PP_Point struct.
65 PP_Point& pp_point() {
66 return point_;
67 }
68
69 /// Getter function for returning the value of x.
70 ///
71 /// @return The value of x for this Point.
72 int32_t x() const { return point_.x; }
73
74 /// Setter function for setting the value of x.
75 ///
76 /// @param[in] in_x A new x value.
77 void set_x(int32_t in_x) {
78 point_.x = in_x;
79 }
80
81 /// Getter function for returning the value of y.
82 ///
83 /// @return The value of y for this Point.
84 int32_t y() const { return point_.y; }
85
86 /// Setter function for setting the value of y.
87 ///
88 /// @param[in] in_y A new y value.
89 void set_y(int32_t in_y) {
90 point_.y = in_y;
91 }
92
93 /// Adds two Points (this and other) together by adding their x values and
94 /// y values.
95 ///
96 /// @param[in] other A Point.
97 ///
98 /// @return A new Point containing the result.
99 Point operator+(const Point& other) const {
100 return Point(x() + other.x(), y() + other.y());
101 }
102
103 /// Subtracts one Point from another Point by subtracting their x values
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100104 /// and y values. Returns a new point with the result.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000105 ///
106 /// @param[in] other A Point.
107 ///
108 /// @return A new Point containing the result.
109 Point operator-(const Point& other) const {
110 return Point(x() - other.x(), y() - other.y());
111 }
112
113 /// Adds two Points (this and other) together by adding their x and y
114 /// values. Returns this point as the result.
115 ///
116 /// @param[in] other A Point.
117 ///
118 /// @return This Point containing the result.
119 Point& operator+=(const Point& other) {
120 point_.x += other.x();
121 point_.y += other.y();
122 return *this;
123 }
124
125 /// Subtracts one Point from another Point by subtracting their x values
126 /// and y values. Returns this point as the result.
127 ///
128 /// @param[in] other A Point.
129 ///
130 /// @return This Point containing the result.
131 Point& operator-=(const Point& other) {
132 point_.x -= other.x();
133 point_.y -= other.y();
134 return *this;
135 }
136
137 /// Swaps the coordinates of two Points.
138 ///
139 /// @param[in] other A Point.
140 void swap(Point& other) {
141 int32_t x = point_.x;
142 int32_t y = point_.y;
143 point_.x = other.point_.x;
144 point_.y = other.point_.y;
145 other.point_.x = x;
146 other.point_.y = y;
147 }
148
149 private:
150 PP_Point point_;
151};
152
153/// A 2 dimensional floating-point point with 0,0 being the upper-left starting
154/// coordinate.
155class FloatPoint {
156 public:
157 /// A constructor for a point at 0,0.
158 FloatPoint() {
159 float_point_.x = 0.0f;
160 float_point_.y = 0.0f;
161 }
162
163 /// A constructor accepting two values for x and y and converting them to a
164 /// FloatPoint.
165 ///
166 /// @param[in] in_x An value representing a horizontal coordinate of a
167 /// point, starting with 0 as the left-most coordinate.
168 ///
169 /// @param[in] in_y An value representing a vertical coordinate of a point,
170 /// starting with 0 as the top-most coordinate.
171 FloatPoint(float in_x, float in_y) {
172 float_point_.x = in_x;
173 float_point_.y = in_y;
174 }
175
176 /// A constructor accepting a pointer to a PP_FloatPoint and converting the
177 /// PP_Point to a Point. This is an implicit conversion constructor.
178 ///
179 /// @param[in] point A PP_FloatPoint.
180 FloatPoint(const PP_FloatPoint& point) { // Implicit.
181 float_point_.x = point.x;
182 float_point_.y = point.y;
183 }
184 /// Destructor.
185 ~FloatPoint() {
186 }
187
188 /// A function allowing implicit conversion of a FloatPoint to a
189 /// PP_FloatPoint.
190 operator PP_FloatPoint() const {
191 return float_point_;
192 }
193
194 /// Getter function for returning the internal PP_FloatPoint struct.
195 ///
196 /// @return A const reference to the internal PP_FloatPoint struct.
197 const PP_FloatPoint& pp_float_point() const {
198 return float_point_;
199 }
200
201 /// Getter function for returning the internal PP_Point struct.
202 ///
203 /// @return A mutable reference to the PP_Point struct.
204 PP_FloatPoint& pp_float_point() {
205 return float_point_;
206 }
207
208 /// Getter function for returning the value of x.
209 ///
210 /// @return The value of x for this Point.
211 float x() const { return float_point_.x; }
212
213 /// Setter function for setting the value of x.
214 ///
215 /// @param[in] in_x A new x value.
216 void set_x(float in_x) {
217 float_point_.x = in_x;
218 }
219
220 /// Getter function for returning the value of y.
221 ///
222 /// @return The value of y for this Point.
223 float y() const { return float_point_.y; }
224
225 /// Setter function for setting the value of y.
226 ///
227 /// @param[in] in_y A new y value.
228 void set_y(float in_y) {
229 float_point_.y = in_y;
230 }
231
232 /// Adds two Points (this and other) together by adding their x values and
233 /// y values.
234 ///
235 /// @param[in] other A Point.
236 ///
237 /// @return A new Point containing the result.
238 FloatPoint operator+(const FloatPoint& other) const {
239 return FloatPoint(x() + other.x(), y() + other.y());
240 }
241
242 /// Subtracts one Point from another Point by subtracting their x values
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100243 /// and y values. Returns a new point with the result.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000244 ///
245 /// @param[in] other A FloatPoint.
246 ///
247 /// @return A new Point containing the result.
248 FloatPoint operator-(const FloatPoint& other) const {
249 return FloatPoint(x() - other.x(), y() - other.y());
250 }
251
252 /// Adds two Points (this and other) together by adding their x and y
253 /// values. Returns this point as the result.
254 ///
255 /// @param[in] other A Point.
256 ///
257 /// @return This Point containing the result.
258 FloatPoint& operator+=(const FloatPoint& other) {
259 float_point_.x += other.x();
260 float_point_.y += other.y();
261 return *this;
262 }
263
264 /// Subtracts one Point from another Point by subtracting their x values
265 /// and y values. Returns this point as the result.
266 ///
267 /// @param[in] other A Point.
268 ///
269 /// @return This Point containing the result.
270 FloatPoint& operator-=(const FloatPoint& other) {
271 float_point_.x -= other.x();
272 float_point_.y -= other.y();
273 return *this;
274 }
275
276 /// Swaps the coordinates of two Points.
277 ///
278 /// @param[in] other A Point.
279 void swap(FloatPoint& other) {
280 float x = float_point_.x;
281 float y = float_point_.y;
282 float_point_.x = other.float_point_.x;
283 float_point_.y = other.float_point_.y;
284 other.float_point_.x = x;
285 other.float_point_.y = y;
286 }
287
288 private:
289 PP_FloatPoint float_point_;
290};
291
292} // namespace pp
293
294/// Determines whether the x and y values of two Points are equal.
295///
296/// @param[in] lhs The Point on the left-hand side of the equation.
297/// @param[in] rhs The Point on the right-hand side of the equation.
298///
299/// @return true if they are equal, false if unequal.
300inline bool operator==(const pp::Point& lhs, const pp::Point& rhs) {
301 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
302}
303
304/// Determines whether two Points have different coordinates.
305///
306/// @param[in] lhs The Point on the left-hand side of the equation.
307/// @param[in] rhs The Point on the right-hand side of the equation.
308///
309/// @return true if the coordinates of lhs are equal to the coordinates
310/// of rhs, otherwise false.
311inline bool operator!=(const pp::Point& lhs, const pp::Point& rhs) {
312 return !(lhs == rhs);
313}
314
315/// Determines whether the x and y values of two FloatPoints are equal.
316///
317/// @param[in] lhs The Point on the left-hand side of the equation.
318/// @param[in] rhs The Point on the right-hand side of the equation.
319///
320/// @return true if they are equal, false if unequal.
321inline bool operator==(const pp::FloatPoint& lhs, const pp::FloatPoint& rhs) {
322 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
323}
324
325/// Determines whether two Points have different coordinates.
326///
327/// @param[in] lhs The Point on the left-hand side of the equation.
328/// @param[in] rhs The Point on the right-hand side of the equation.
329///
330/// @return true if the coordinates of lhs are equal to the coordinates
331/// of rhs, otherwise false.
332inline bool operator!=(const pp::FloatPoint& lhs, const pp::FloatPoint& rhs) {
333 return !(lhs == rhs);
334}
335
336#endif // PPAPI_CPP_POINT_H_