1// LAF Gfx Library
2// Copyright (C) 2020 Igara Studio S.A.
3// Copyright (C) 2001-2016 David Capello
4//
5// This file is released under the terms of the MIT license.
6// Read LICENSE.txt for more information.
7
8#ifndef GFX_POINT_H_INCLUDED
9#define GFX_POINT_H_INCLUDED
10#pragma once
11
12namespace gfx {
13
14template<typename T>
15class SizeT;
16
17// A 2D coordinate in the screen.
18template<typename T>
19class PointT {
20public:
21 T x, y;
22
23 PointT() : x(0), y(0) {
24 }
25
26 PointT(const T& x, const T& y) : x(x), y(y) {
27 }
28
29 PointT(const PointT& point) : x(point.x), y(point.y) {
30 }
31
32 template<typename U>
33 explicit PointT(const PointT<U>& point) : x(static_cast<T>(point.x)),
34 y(static_cast<T>(point.y)) {
35 }
36
37 template<typename U>
38 explicit PointT(const SizeT<U>& size) : x(size.w), y(size.h) {
39 }
40
41 template<typename U>
42 const PointT& operator=(const PointT<U>& pt) {
43 x = pt.x;
44 y = pt.y;
45 return *this;
46 }
47
48 template<typename U>
49 const PointT& operator+=(const PointT<U>& pt) {
50 x += pt.x;
51 y += pt.y;
52 return *this;
53 }
54
55 template<typename U>
56 const PointT& operator-=(const PointT<U>& pt) {
57 x -= pt.x;
58 y -= pt.y;
59 return *this;
60 }
61
62 const PointT& operator+=(const T& value) {
63 x += value;
64 y += value;
65 return *this;
66 }
67
68 const PointT& operator-=(const T& value) {
69 x -= value;
70 y -= value;
71 return *this;
72 }
73
74 const PointT& operator*=(const T& value) {
75 x *= value;
76 y *= value;
77 return *this;
78 }
79
80 const PointT& operator/=(const T& value) {
81 x /= value;
82 y /= value;
83 return *this;
84 }
85
86 template<typename U>
87 PointT operator+(const PointT<U>& pt) const {
88 return PointT(x+pt.x, y+pt.y);
89 }
90
91 template<typename U>
92 PointT operator-(const PointT<U>& pt) const {
93 return PointT(x-pt.x, y-pt.y);
94 }
95
96 PointT operator+(const T& value) const {
97 return PointT(x+value, y+value);
98 }
99
100 PointT operator-(const T& value) const {
101 return PointT(x-value, y-value);
102 }
103
104 PointT operator*(const T& value) const {
105 return PointT(x*value, y*value);
106 }
107
108 PointT operator/(const T& value) const {
109 return PointT(x/value, y/value);
110 }
111
112 PointT operator-() const {
113 return PointT(-x, -y);
114 }
115
116 bool operator==(const PointT& pt) const {
117 return x == pt.x && y == pt.y;
118 }
119
120 bool operator!=(const PointT& pt) const {
121 return x != pt.x || y != pt.y;
122 }
123
124};
125
126typedef PointT<int> Point;
127typedef PointT<double> PointF;
128
129} // namespace gfx
130
131#ifdef _DEBUG
132#include "gfx/point_io.h"
133#endif
134
135#endif
136