-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntVector2Util.cs
More file actions
122 lines (110 loc) · 2.91 KB
/
IntVector2Util.cs
File metadata and controls
122 lines (110 loc) · 2.91 KB
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
public class IntVector2Util
{
/**
* Takes the given array of vectors and adds the given offest to them.
* This is useful if you have a set of points, say a movement path, but
* need to apply it to the current unit's position.
*/
public static IntVector2[] Offset(IntVector2[] input, IntVector2 offset)
{
IntVector2[] retval = new IntVector2[input.Length];
for(int i=0;i < input.Length; i++)
{
IntVector2 pos = input[i];
retval[i] = new IntVector2(offset.x + pos.x, offset.y + pos.y);
}
return retval;
}
public static IntVector2 Offset(IntVector2 pos, IntVector2 offset)
{
return new IntVector2(pos.x + offset.x, pos.y + offset.y);
}
public static IntVector2 xRot0 = new IntVector2(1,0);
public static IntVector2 yRot0 = new IntVector2(0,1);
public static IntVector2 xRot90 = new IntVector2(0,-1);
public static IntVector2 yRot90 = new IntVector2(1,0);
public static IntVector2 xRot180 = new IntVector2(-1,0);
public static IntVector2 yRot180 = new IntVector2(0,-1);
public static IntVector2 xRot270 = new IntVector2(0,1);
public static IntVector2 yRot270 = new IntVector2(-1,0);
/**
* Rotates in 90 degree increments and then translates the points with the
* given offset.
*/
public static IntVector2[] Rotate(IntVector2[] input, int rotation, IntVector2 offset)
{
IntVector2 xRot = xRot0;
IntVector2 yRot = yRot0;
switch(rotation)
{
case 0:
//default
break;
case 90:
case -270:
xRot = xRot90;
yRot = yRot90;
break;
case 180:
xRot = xRot180;
yRot = yRot180;
break;
case 270:
case -90:
xRot = xRot270;
yRot = yRot270;
break;
default:
throw new System.Exception("Rotate() requires 90 degree rotations.");
}
IntVector2[] retval = new IntVector2[input.Length];
for(int i=0;i < input.Length; i++)
{
IntVector2 pos = input[i];
int x = pos.x * xRot.x + pos.y * xRot.y;
int y = pos.x * yRot.x + pos.y * yRot.y;
retval[i] = new IntVector2(x + offset.x, y + offset.y);
}
return retval;
}
public static IntVector2 GetDirection(IntVector2 start, IntVector2 end)
{
if(start.x == end.x)
{
//no east/west
if(start.y == end.y)
{
return IntVector2.zero;
}else if(start.y < end.y)
{
return IntVector2.north;
}else{
return IntVector2.south;
}
}else if(start.x < end.x)
{
//east
if(start.y == end.y)
{
return IntVector2.east;
}else if(start.y < end.y)
{
return IntVector2.northeast;
}else{
return IntVector2.southeast;
}
}else
{
//west
if(start.y == end.y)
{
return IntVector2.west;
}else if(start.y < end.y)
{
return IntVector2.northwest;
}else{
return IntVector2.southwest;
}
}
}
}