-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVector2.cpp
130 lines (125 loc) · 2.78 KB
/
Vector2.cpp
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
#pragma once
#include "Vector2.h"
void set(Vector2* v,double fx, double fy) {
v->x = fx;
v->y = fy;
}
void translate(Vector2* v,double fx, double fy) {
v->x += fx;
v->y += fy;
}
bool isZero(Vector2* v) {
return _IS_DOUBLE_ZERO(v->x) && _IS_DOUBLE_ZERO(v->y);
}
bool isOne(Vector2* v) {
return _IS_DOUBLE_ZERO(v->x - 1) && _IS_DOUBLE_ZERO(v->y - 1);
}
void scale(Vector2* v, double fx, double fy) {
v->x = v->x * fx;
v->y = v->y * fy;
}
void rotate(Vector2* v, Vector2* point, double radians) {
double sinRadians = sin(radians);
double cosRadians = cos(radians);
if (isZero(point)) {
double tempX = v->x * cosRadians - v->y * sinRadians;
v->y = v->y * cosRadians + v->x * sinRadians;
v->x = tempX;
}
else {
double tempX = v->x - (*point).x;
double tempY = v->y - (*point).y;
v->x = tempX * cosRadians - tempY * sinRadians + (*point).x;
v->y = tempY * cosRadians + tempX * sinRadians + (*point).y;
}
}
void normalize(Vector2* v ) {
double len = v->x * v->x + v->y * v->y;
if (len == 1) {
return;
}
double n = sqrt(len);
if (_IS_DOUBLE_ZERO(n)) {
return;
}
n = 1 / n;
v->x *= n;
v->y *= n;
}
void forward(Vector2* v, double dis)
{
static Vector2 vec;
vec.x = v->x;
vec.y = v->y;
normalize(&vec);
vec.x *= dis;
vec.y *= dis;
v->x += vec.x;
v->y += vec.y;
}
void back(Vector2* v, double dis)
{
static Vector2 vec;
vec.x = v->x;
vec.y = v->y;
normalize(&vec);
vec.x *= dis;
vec.y *= dis;
v->x -= vec.x;
v->y -= vec.y;
}
double dot(Vector2* v, Vector2* other) {
return v->x * other->x + v->y * other->y;
}
double angle(Vector2* v, Vector2* other) {
static Vector2* v1 = (Vector2*)malloc(sizeof(Vector2));
static Vector2* v2 = (Vector2*)malloc(sizeof(Vector2));
set(v1, v->x, v->y);
v2 = other;
normalize(v1);
normalize(v2);
double cosRadians = dot(v1,v2);
// 使用三维向量叉乘处理正负问题
// A(src->x,src->y,1) B(dest->x,dest->y,1)
// z = x1y2-y1x2
double z = v->x * other->y - v->y * other->x;
return z >= 0 ? acos(cosRadians) : -acos(cosRadians);
}
double module(Vector2* v ) {
return sqrt(v->x * v->x + v->y * v->y);
}
double modulePower2(Vector2* v ) {
return v->x * v->x + v->y * v->y;
}
double distance(Vector2* v, const Vector2* other)
{
double dx = other->x - v->x;
double dy = other->y - v->y;
return sqrt(dx * dx + dy * dy);
}
double distancePower2(Vector2* v, const Vector2* other)
{
double dx = other->x - v->x;
double dy = other->y - v->y;
return dx * dx + dy * dy;
}
Vector2 projection(Vector2* v, Vector2* other)
{
Vector2 vn;
double len2 = modulePower2(other);
len2 = 1 / len2;
double tmp = (v->x * other->x + v->y * other->y) * len2;
set(&vn,other->x * tmp, other->y * tmp);
return vn;
}
Vector2 right(Vector2* v ) {
Vector2 vn;
set(&vn, v->y, -v-> x);
return vn;
}
Vector2 left(Vector2* v)
{
Vector2 vn ;
set(&vn,-v->y, v->x);
return vn;
}