-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
80 lines (69 loc) · 1.53 KB
/
vector.c
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
/*
* $Id: vector.c 268 2005-11-10 08:02:20Z mrbrown $
*
* Copyright 2002 Kenta Cho. All rights reserved.
*/
/**
* Vector functions.
*
* @version $Revision: 268 $
*/
#include <math.h>
#include "vector.h"
float vctInnerProduct(Vector *v1, Vector *v2) {
return (float)v1->x*v2->x + (float)v1->y*v2->y;
}
Vector vctGetElement(Vector *v1, Vector *v2) {
Vector ans;
int ll = v2->x*v2->x + v2->y*v2->y;
if ( ll != 0 ) {
int mag = vctInnerProduct(v1, v2);
ans.x = mag*v2->x/ll;
ans.y = mag*v2->y/ll;
} else {
ans.x = ans.y = 0;
}
return ans;
}
void vctAdd(Vector *v1, Vector *v2) {
v1->x += v2->x;
v1->y += v2->y;
}
void vctSub(Vector *v1, Vector *v2) {
v1->x -= v2->x;
v1->y -= v2->y;
}
void vctMul(Vector *v1, int a) {
v1->x *= a;
v1->y *= a;
}
void vctDiv(Vector *v1, int a) {
v1->x /= a;
v1->y /= a;
}
int vctCheckSide(Vector *checkPos, Vector *pos1, Vector *pos2) {
int xo = pos2->x-pos1->x, yo = pos2->y-pos1->y;
if ( xo == 0 ) {
if ( yo == 0 ) return 0;
return checkPos->x - pos1->x;
} else if ( yo == 0 ) {
return pos1->y - checkPos->y;
} else {
if ( xo*yo > 0 ) {
return (checkPos->x-pos1->x)/xo - (checkPos->y-pos1->y)/yo;
} else {
return -(checkPos->x-pos1->x)/xo + (checkPos->y-pos1->y)/yo;
}
}
}
int vctSize(Vector *v) {
return sqrtf(v->x*v->x + v->y*v->y);
}
int vctDist(Vector *v1, Vector *v2) {
int ax = absN(v1->x - v2->x), ay = absN(v1->y - v2->y);
if ( ax > ay ) {
return ax + (ay>>1);
} else {
return ay + (ax>>1);
}
}