-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector2.cs
74 lines (68 loc) · 2.02 KB
/
Vector2.cs
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
using System.Runtime.InteropServices;
namespace PiGameSharp
{
/// <summary>
/// A two dimensional vector
/// </summary>
[StructLayout(LayoutKind.Explicit, Size=8, Pack=0)]
public struct Vector2
{
/// <summary>
/// The zero vector
/// </summary>
public readonly static Vector2 Zero = new Vector2();
/// <summary>
/// The unit vector for the X axis
/// </summary>
public readonly static Vector2 UnitX = new Vector2(1, 0);
/// <summary>
/// The unit vector for the Y axis
/// </summary>
public readonly static Vector2 UnitY = new Vector2(0, 1);
/// <summary>
/// The x element
/// </summary>
[FieldOffset(0)]
public readonly uint x;
/// <summary>
/// The y element
/// </summary>
[FieldOffset(4)]
public readonly uint y;
/// <summary>
/// Initializes a new instance of the <see cref="PiGameSharp.Vector2"/> struct.
/// </summary>
/// <param name="x">The x coordinate.</param>
/// <param name="y">The y coordinate.</param>
public Vector2(uint x, uint y)
{
this.x = x;
this.y = y;
}
public override string ToString() => "[" + x + ", " + y + "]";
/// <summary>
/// Shift left all elements of a vector
/// </summary>
/// <remarks>
/// In some cases we need fixed point math vectors. Having a bit shift operator makes dealing with this much easier
/// </remarks>
/// <param name="item">The vector to shift left</param>
/// <param name="bits">How many bits to shift left</param>
public static Vector2 operator << (Vector2 item, int bits)
{
return new Vector2(item.x << bits, item.y << bits);
}
/// <summary>
/// Shift right all elements of a vector
/// </summary>
/// <remarks>
/// In some cases we need fixed point math vectors. Having a bit shift operator makes dealing with this much easier
/// </remarks>
/// <param name="item">The vector to shift right</param>
/// <param name="bits">How many bits to shift right</param>
public static Vector2 operator >> (Vector2 item, int bits)
{
return new Vector2(item.x >> bits, item.y >> bits);
}
}
}