File: type2.h

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (75 lines) | stat: -rw-r--r-- 3,538 bytes parent folder | download | duplicates (5)
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
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#ifndef TYPE2_H
#define TYPE2_H

#include "lib/streflop/streflop_cond.h"
#include "System/creg/creg_cond.h"
#include "System/FastMath.h"

template<typename t> struct type2 {
	CR_DECLARE_STRUCT(type2)

	constexpr type2<t>(): x(t(0)), y(t(0)) {}
	constexpr type2<t>(const t nx, const t ny) : x(nx), y(ny) {}
	template<typename T2> constexpr type2<t>(const T2 v) : x(v.x), y(v.y) {}

	bool operator == (const type2<t>& v) const { return (x == v.x) && (y == v.y); }
	bool operator != (const type2<t>& v) const { return (x != v.x) || (y != v.y); }
	bool operator  < (const type2<t>& f) const { return (y == f.y) ? (x < f.x) : (y < f.y); }

	type2<t> operator - () const { return (type2<t>(-x, -y)); }
	type2<t> operator + (const type2<t>& v) const { return (type2<t>(x + v.x, y + v.y)); }
	type2<t> operator - (const type2<t>& v) const { return (type2<t>(x - v.x, y - v.y)); }
	type2<t> operator / (const type2<t>& v) const { return (type2<t>(x / v.x, y / v.y)); }
	type2<t> operator / (const t& i) const        { return (type2<t>(x / i  , y / i  )); }
	type2<t> operator * (const type2<t>& v) const { return (type2<t>(x * v.x, y * v.y)); }
	type2<t> operator * (const t& i) const        { return (type2<t>(x * i  , y * i  )); }

	type2<t>& operator += (const t& i) { x += i; y += i; return *this; }
	type2<t>& operator += (const type2<t>& v) { x += v.x; y += v.y; return *this; }
	type2<t>& operator -= (const t& i) { x -= i; y -= i; return *this; }
	type2<t>& operator -= (const type2<t>& v) { x -= v.x; y -= v.y; return *this; }
	type2<t>& operator *= (const t& i) { x *= i; y *= i; return *this; }
	type2<t>& operator *= (const type2<t>& v) { x *= v.x; y *= v.y; return *this; }
	type2<t>& operator /= (const t& i) { x /= i; y /= i; return *this; }
	type2<t>& operator /= (const type2<t>& v) { x /= v.x; y /= v.y; return *this; }

	t distance(const type2<t>& f) const {
		const t dx = x - f.x;
		const t dy = y - f.y;
		return t(math::sqrt(dx*dx + dy*dy));
	}

	t x;
	t y;
};

template<typename t> struct itype2 : public type2<t> {
	CR_DECLARE_STRUCT(itype2)

	constexpr itype2<t>() {}
	constexpr itype2<t>(const t nx, const t ny) : type2<t>(nx, ny) {}
	constexpr itype2<t>(const type2<int>& v) : type2<t>(v.x, v.y) {}

	bool operator == (const type2<int>& v) const { return (type2<t>::x == v.x) && (type2<t>::y == v.y); }
	bool operator != (const type2<int>& v) const { return (type2<t>::x != v.x) || (type2<t>::y != v.y); }
	bool operator  < (const type2<int>& f) const { return (type2<t>::y == f.y) ? (type2<t>::x < f.x) : (type2<t>::y < f.y); }

	type2<int> operator + (const type2<int>& v) const { return (type2<int>(type2<t>::x + v.x, type2<t>::y + v.y)); }
	type2<int> operator - (const type2<int>& v) const { return (type2<int>(type2<t>::x - v.x, type2<t>::y - v.y)); }
	type2<int> operator / (const type2<int>& v) const { return (type2<int>(type2<t>::x / v.x, type2<t>::y / v.y)); }
	type2<int> operator / (const int& i) const        { return (type2<int>(type2<t>::x / i  , type2<t>::y / i  )); }
	type2<int> operator * (const type2<int>& v) const { return (type2<int>(type2<t>::x * v.x, type2<t>::y * v.y)); }
	type2<int> operator * (const int& i) const        { return (type2<int>(type2<t>::x * i  , type2<t>::y * i  )); }

	operator type2<int> () const { return (type2<int>(type2<t>::x, type2<t>::y)); }
};


typedef type2<  int>   int2;
typedef type2<float> float2;
typedef itype2<short> short2;
typedef itype2<unsigned short> ushort2;

#endif