-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vectorf.h
142 lines (116 loc) · 2.93 KB
/
Vectorf.h
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
#pragma once
#include <stdio.h>
#include <float.h>
#include <math.h>
#include "Log.h"
#include "randomUtil.h"
#include "StlUtil.h"
struct Vector2f {
inline static float EPS_MIN = 1e-6f; // distance to be considered ~=
float x, y;
Vector2f() : x(0.f),y(0.f){}
Vector2f( float ix, float iy ):x(ix),y(iy){}
Vector2f( float iv ):x(iv),y(iv){}
inline Vector2f& operator=( float v ) {
x=y=v;
return *this ;
}
static inline Vector2f random() { return Vector2f( randFloat(), randFloat() ) ; }
static inline Vector2f random( float min, float max ) {
return Vector2f( randFloat( min, max ), randFloat( min, max ) ) ;
}
inline bool anyZero() const { return !x || !y; }
inline float max() const { return std::max( x,y ) ; }
inline float min() const { return std::min( x,y ) ; }
inline Vector2f operator+( const Vector2f& o ) const {
return Vector2f( x+o.x, y+o.y );
}
inline Vector2f& operator+=( const Vector2f& o ) {
x+=o.x,y+=o.y;
return *this ;
}
inline Vector2f operator-() const {
return Vector2f(-x,-y);
}
inline Vector2f operator-( const Vector2f& o ) const {
return Vector2f( x - o.x, y - o.y );
}
inline Vector2f& operator-=( const Vector2f& o ) {
x-=o.x,y-=o.y;
return *this ;
}
inline Vector2f operator*( const Vector2f& o ) const {
return Vector2f( x*o.x, y*o.y );
}
inline Vector2f operator*( float s ) const {
return Vector2f( x*s, y*s );
}
inline Vector2f& operator*=( const Vector2f& o ) {
x*=o.x,y*=o.y;
return *this ;
}
inline Vector2f& operator*=( float s ) {
x*=s,y*=s;
return *this ;
}
inline Vector2f operator/( const Vector2f& o ) const {
if( o.anyZero() ) {
error( "divide by 0" );
return *this;
}
return Vector2f( x/o.x, y/o.y );
}
inline Vector2f operator/( float s ) const {
if( !s ) {
error( "divide by 0" );
return *this;
}
return Vector2f( x/s, y/s );
}
inline Vector2f& operator/=( const Vector2f& o ) {
if( o.anyZero() ) {
error( "divide by 0" );
return *this;
}
x /= o.x;
y /= o.y;
return *this;
}
inline Vector2f& operator/=( float s ) {
if( !s ) {
error( "divide by 0" );
return *this;
}
x/=s, y/=s;
return *this;
}
inline float len() const {
return sqrtf( x*x+y*y );
}
inline float len2() const {
return x*x+y*y;
}
inline Vector2f& normalize() {
float length = len();
if( !length ) {
error( "divide by 0" );
return *this;
}
return (*this) /= length;
}
inline Vector2f normalizedCopy() const {
return Vector2f( *this ).normalize() ;
}
inline bool operator==( const Vector2f& o ) const {
return x==o.x && y==o.y ;
}
inline bool operator!=( const Vector2f& o ) const {
return x!=o.x || y!=o.y ;
}
inline bool isNear( const Vector2f& o ) const {
return fabsf( x - o.x ) < EPS_MIN && fabsf( y - o.y ) < EPS_MIN;
}
string toString() const {
return makeString( "%.2f, %.2f", x, y );
}
};