PCB Environment: /home/chr/devel/PhD/Environment/pub/PCBEnv/pcbenv/cxx/Math/IPoint2.hpp Source File

PCB Environment 2

Loading...
Searching...
No Matches
IPoint2.hpp
1
2#ifndef GYM_PCB_MATH_IPOINT2
3#define GYM_PCB_MATH_IPOINT2
4
5#include "Defs.hpp"
6#include <cmath>
7#include <iostream>
8
9 class IVector_2
10{
11public:
12 int32_t x, y;
13public:
14 IVector_2() { }
15 IVector_2(int X, int Y) : x(X), y(Y) { }
16 IVector_2(const IVector_2 &v) : x(v.x), y(v.y) { }
17 IVector_2& operator=(const IVector_2 &v) { x = v.x; y = v.y; return *this; }
18 bool operator==(const IVector_2 &v) const { return x == v.x && y == v.y; }
19 bool operator!=(const IVector_2 &v) const { return x != v.x || y != v.y; }
20 IVector_2 operator+(const IVector_2 &v) const { return IVector_2(x + v.x, y + v.y); }
21 IVector_2 operator-(const IVector_2 &v) const { return IVector_2(x - v.x, y - v.y); }
22 IVector_2 operator*(int s) const { return IVector_2(x * s, y * s); }
23 IVector_2 operator/(int s) const { return IVector_2((x + s - 1) / s, (y + s - 1) / s); }
24 IVector_2& operator+=(const IVector_2 &v) { x += v.x; y += v.y; return *this; }
25 IVector_2& operator-=(const IVector_2 &v) { x -= v.x; y -= v.y; return *this; }
26 IVector_2& operator*=(int s) { *this = *this * s; return *this; }
27 IVector_2& operator/=(int s) { *this = *this / s; return *this; }
28 IVector_2 abs() const { return IVector_2(std::abs(x), std::abs(y)); }
29 int32_t dot(const IVector_2 &v) const { return x * v.x + y * v.y; }
30 int32_t norm1() const { return std::abs(x) + std::abs(y); }
31 int32_t hprod() const { return x * y; }
32 IVector_2 max_cw(const IVector_2 &v) const { return IVector_2(std::max(x, v.x), std::max(y, v.y)); }
33 IVector_2 min_cw(const IVector_2 &v) const { return IVector_2(std::min(x, v.x), std::min(y, v.y)); }
34};
35
36 class IPoint_2
37{
38public:
39 int32_t x, y;
40public:
41 IPoint_2() { }
42 IPoint_2(int X, int Y) : x(X), y(Y) { }
43 IPoint_2(const IVector_2 &v) : x(v.x), y(v.y) { }
44 IPoint_2(const int *v) : x(v[0]), y(v[1]) { }
45 IPoint_2(const uint *v) : x(v[0]), y(v[1]) { }
46 IPoint_2& operator=(const IPoint_2 &P) { x = P.x; y = P.y; return *this; }
47 bool operator==(const IPoint_2 &P) const { return x == P.x && y == P.y; }
48 bool operator!=(const IPoint_2 &P) const { return x != P.x || y != P.y; }
49 bool isPositive() const { return x >= 0 && y >= 0; }
50 IPoint_2 operator+(const IVector_2 &v) const { return IPoint_2(x + v.x, y + v.y); }
51 IPoint_2 operator-(const IVector_2 &v) const { return IPoint_2(x - v.x, y - v.y); }
52 IPoint_2& operator+=(const IVector_2 &v) { x += v.x; y += v.y; return *this; }
53 IPoint_2& operator-=(const IVector_2 &v) { x -= v.x; y -= v.y; return *this; }
54 IVector_2 vector() const { return IVector_2(x, y); }
55};
56
57inline std::ostream& operator<<(std::ostream &os, const IVector_2 &v)
58{
59 return os << '(' << v.x << ',' << v.y << ')';
60}
61inline std::ostream& operator<<(std::ostream &os, const IPoint_2 &P)
62{
63 return os << '(' << P.x << ',' << P.y << ')';
64}
65
66#endif // GYM_PCB_MATH_IPOINT2
IPoint_2
Definition IPoint2.hpp:37
IVector_2
Definition IPoint2.hpp:10