summaryrefslogtreecommitdiffstats
path: root/ppapi/cpp/point.h
blob: 028d050e7adc9b6ebd1704149da7c70b7dc5f501 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_CPP_POINT_H_
#define PPAPI_CPP_POINT_H_

#include <stdint.h>

#include "ppapi/c/pp_point.h"

/// @file
/// This file defines the API to create a 2 dimensional point.

namespace pp {

/// A 2 dimensional point with 0,0 being the upper-left starting coordinate.
class Point {
 public:
  /// The default constructor for a point at 0,0.
  Point() {
    point_.x = 0;
    point_.y = 0;
  }

  /// A constructor accepting two int32_t values for x and y and converting
  /// them to a Point.
  ///
  /// @param[in] in_x An int32_t value representing a horizontal coordinate
  /// of a point, starting with 0 as the left-most coordinate.
  /// @param[in] in_y An int32_t value representing a vertical coordinate
  /// of a point, starting with 0 as the top-most coordinate.
  Point(int32_t in_x, int32_t in_y) {
    point_.x = in_x;
    point_.y = in_y;
  }

  /// A constructor accepting a pointer to a PP_Point and converting the
  /// PP_Point to a Point. This is an implicit conversion constructor.
  ///
  /// @param[in] point A pointer to a PP_Point.
  Point(const PP_Point& point) {  // Implicit.
    point_.x = point.x;
    point_.y = point.y;
  }

  /// Destructor.
  ~Point() {
  }

  /// A function allowing implicit conversion of a Point to a PP_Point.
  /// @return A Point.
  operator PP_Point() const {
    return point_;
  }

  /// Getter function for returning the internal PP_Point struct.
  ///
  /// @return A const reference to the internal PP_Point struct.
  const PP_Point& pp_point() const {
    return point_;
  }

  /// Getter function for returning the internal PP_Point struct.
  ///
  /// @return A mutable reference to the PP_Point struct.
  PP_Point& pp_point() {
    return point_;
  }

  /// Getter function for returning the value of x.
  ///
  /// @return The value of x for this Point.
  int32_t x() const { return point_.x; }

  /// Setter function for setting the value of x.
  ///
  /// @param[in] in_x A new x value.
  void set_x(int32_t in_x) {
    point_.x = in_x;
  }

  /// Getter function for returning the value of y.
  ///
  /// @return The value of y for this Point.
  int32_t y() const { return point_.y; }

  /// Setter function for setting the value of y.
  ///
  /// @param[in] in_y A new y value.
  void set_y(int32_t in_y) {
    point_.y = in_y;
  }

  /// Adds two Points (this and other) together by adding their x values and
  /// y values.
  ///
  /// @param[in] other A Point.
  ///
  /// @return A new Point containing the result.
  Point operator+(const Point& other) const {
    return Point(x() + other.x(), y() + other.y());
  }

  /// Subtracts one Point from another Point by subtracting their x values
  /// and y values. Returns a new point with the result.
  ///
  /// @param[in] other A Point.
  ///
  /// @return A new Point containing the result.
  Point operator-(const Point& other) const {
    return Point(x() - other.x(), y() - other.y());
  }

  /// Adds two Points (this and other) together by adding their x and y
  /// values. Returns this point as the result.
  ///
  /// @param[in] other A Point.
  ///
  /// @return This Point containing the result.
  Point& operator+=(const Point& other) {
    point_.x += other.x();
    point_.y += other.y();
    return *this;
  }

  /// Subtracts one Point from another Point by subtracting their x values
  /// and y values. Returns this point as the result.
  ///
  /// @param[in] other A Point.
  ///
  /// @return This Point containing the result.
  Point& operator-=(const Point& other) {
    point_.x -= other.x();
    point_.y -= other.y();
    return *this;
  }

  /// Swaps the coordinates of two Points.
  ///
  /// @param[in] other A Point.
  void swap(Point& other) {
    int32_t x = point_.x;
    int32_t y = point_.y;
    point_.x = other.point_.x;
    point_.y = other.point_.y;
    other.point_.x = x;
    other.point_.y = y;
  }

 private:
  PP_Point point_;
};

/// A 2 dimensional floating-point point with 0,0 being the upper-left starting
/// coordinate.
class FloatPoint {
 public:
  /// A constructor for a point at 0,0.
  FloatPoint() {
    float_point_.x = 0.0f;
    float_point_.y = 0.0f;
  }

  /// A constructor accepting two values for x and y and converting them to a
  /// FloatPoint.
  ///
  /// @param[in] in_x An value representing a horizontal coordinate of a
  /// point, starting with 0 as the left-most coordinate.
  ///
  /// @param[in] in_y An value representing a vertical coordinate of a point,
  /// starting with 0 as the top-most coordinate.
  FloatPoint(float in_x, float in_y) {
    float_point_.x = in_x;
    float_point_.y = in_y;
  }

  /// A constructor accepting a pointer to a PP_FloatPoint and converting the
  /// PP_Point to a Point. This is an implicit conversion constructor.
  ///
  /// @param[in] point A PP_FloatPoint.
  FloatPoint(const PP_FloatPoint& point) {  // Implicit.
    float_point_.x = point.x;
    float_point_.y = point.y;
  }
  /// Destructor.
  ~FloatPoint() {
  }

  /// A function allowing implicit conversion of a FloatPoint to a
  /// PP_FloatPoint.
  operator PP_FloatPoint() const {
    return float_point_;
  }

  /// Getter function for returning the internal PP_FloatPoint struct.
  ///
  /// @return A const reference to the internal PP_FloatPoint struct.
  const PP_FloatPoint& pp_float_point() const {
    return float_point_;
  }

  /// Getter function for returning the internal PP_Point struct.
  ///
  /// @return A mutable reference to the PP_Point struct.
  PP_FloatPoint& pp_float_point() {
    return float_point_;
  }

  /// Getter function for returning the value of x.
  ///
  /// @return The value of x for this Point.
  float x() const { return float_point_.x; }

  /// Setter function for setting the value of x.
  ///
  /// @param[in] in_x A new x value.
  void set_x(float in_x) {
    float_point_.x = in_x;
  }

  /// Getter function for returning the value of y.
  ///
  /// @return The value of y for this Point.
  float y() const { return float_point_.y; }

  /// Setter function for setting the value of y.
  ///
  /// @param[in] in_y A new y value.
  void set_y(float in_y) {
    float_point_.y = in_y;
  }

  /// Adds two Points (this and other) together by adding their x values and
  /// y values.
  ///
  /// @param[in] other A Point.
  ///
  /// @return A new Point containing the result.
  FloatPoint operator+(const FloatPoint& other) const {
    return FloatPoint(x() + other.x(), y() + other.y());
  }

  /// Subtracts one Point from another Point by subtracting their x values
  /// and y values. Returns a new point with the result.
  ///
  /// @param[in] other A FloatPoint.
  ///
  /// @return A new Point containing the result.
  FloatPoint operator-(const FloatPoint& other) const {
    return FloatPoint(x() - other.x(), y() - other.y());
  }

  /// Adds two Points (this and other) together by adding their x and y
  /// values. Returns this point as the result.
  ///
  /// @param[in] other A Point.
  ///
  /// @return This Point containing the result.
  FloatPoint& operator+=(const FloatPoint& other) {
    float_point_.x += other.x();
    float_point_.y += other.y();
    return *this;
  }

  /// Subtracts one Point from another Point by subtracting their x values
  /// and y values. Returns this point as the result.
  ///
  /// @param[in] other A Point.
  ///
  /// @return This Point containing the result.
  FloatPoint& operator-=(const FloatPoint& other) {
    float_point_.x -= other.x();
    float_point_.y -= other.y();
    return *this;
  }

  /// Swaps the coordinates of two Points.
  ///
  /// @param[in] other A Point.
  void swap(FloatPoint& other) {
    float x = float_point_.x;
    float y = float_point_.y;
    float_point_.x = other.float_point_.x;
    float_point_.y = other.float_point_.y;
    other.float_point_.x = x;
    other.float_point_.y = y;
  }

 private:
  PP_FloatPoint float_point_;
};

}  // namespace pp

/// Determines whether the x and y values of two Points are equal.
///
/// @param[in] lhs The Point on the left-hand side of the equation.
/// @param[in] rhs The Point on the right-hand side of the equation.
///
/// @return true if they are equal, false if unequal.
inline bool operator==(const pp::Point& lhs, const pp::Point& rhs) {
  return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}

/// Determines whether two Points have different coordinates.
///
/// @param[in] lhs The Point on the left-hand side of the equation.
/// @param[in] rhs The Point on the right-hand side of the equation.
///
/// @return true if the coordinates of lhs are equal to the coordinates
/// of rhs, otherwise false.
inline bool operator!=(const pp::Point& lhs, const pp::Point& rhs) {
  return !(lhs == rhs);
}

/// Determines whether the x and y values of two FloatPoints are equal.
///
/// @param[in] lhs The Point on the left-hand side of the equation.
/// @param[in] rhs The Point on the right-hand side of the equation.
///
/// @return true if they are equal, false if unequal.
inline bool operator==(const pp::FloatPoint& lhs, const pp::FloatPoint& rhs) {
  return lhs.x() == rhs.x() && lhs.y() == rhs.y();
}

/// Determines whether two Points have different coordinates.
///
/// @param[in] lhs The Point on the left-hand side of the equation.
/// @param[in] rhs The Point on the right-hand side of the equation.
///
/// @return true if the coordinates of lhs are equal to the coordinates
/// of rhs, otherwise false.
inline bool operator!=(const pp::FloatPoint& lhs, const pp::FloatPoint& rhs) {
  return !(lhs == rhs);
}

#endif  // PPAPI_CPP_POINT_H_