chenxin
2020-11-18 93f67abd5fe45178f48ce7db675bbfe007bfc9e7
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
142
143
144
145
146
147
148
149
150
using System;
using UnityEngine;
 
namespace Core.Utilities
{
    /// <summary>
    /// A 2-dimensional vector with integer components
    /// </summary>
    [Serializable]
    public struct IntVector2 : IEquatable<IntVector2>
    {
        /// <summary>
        /// Vector with both components being 1
        /// </summary>
        public static readonly IntVector2 one = new IntVector2(1, 1);
 
        /// <summary>
        /// Vector with both components being 0
        /// </summary>
        public static readonly IntVector2 zero = new IntVector2(0, 0);
 
        /// <summary>
        /// The x component of this vector
        /// </summary>
        public int x;
 
        /// <summary>
        /// The y component of this vector
        /// </summary>
        public int y;
 
        /// <summary>
        /// Gets the squared magnitude of this vector
        /// </summary>
        public int sqrMagnitude
        {
            get { return (x * x) + (y * y); }
        }
 
        /// <summary>
        /// Returns the magnitude of this vector
        /// </summary>
        public float magnitude
        {
            get { return Mathf.Sqrt(sqrMagnitude); }
        }
 
        /// <summary>
        /// Gets the manhattan distance of this vector
        /// </summary>
        public int manhattanDistance
        {
            get { return Mathf.Abs(x) + Mathf.Abs(y); }
        }
 
        /// <summary>
        /// Initialize a new vector
        /// </summary>
        public IntVector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
 
        public bool Equals(IntVector2 other)
        {
            return other.x == x && other.y == y;
        }
 
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj))
            {
                return false;
            }
            return obj is IntVector2 && Equals((IntVector2) obj);
        }
 
        /// <summary>
        /// Simple hash multiplying by two primes
        /// </summary>
        public override int GetHashCode()
        {
            unchecked
            {
                return (x.GetHashCode() * 92821) ^ (y.GetHashCode() * 31);
            }
        }
 
        public override string ToString()
        {
            return string.Format("X: {0}, Y: {1}", x, y);
        }
 
        public static bool operator ==(IntVector2 left, IntVector2 right)
        {
            return left.Equals(right);
        }
 
        public static bool operator !=(IntVector2 left, IntVector2 right)
        {
            return !left.Equals(right);
        }
 
        public static implicit operator Vector2(IntVector2 vector)
        {
            return new Vector2(vector.x, vector.y);
        }
 
        public static explicit operator IntVector2(Vector2 vector)
        {
            return new IntVector2((int) vector.x, (int) vector.y);
        }
 
        public static IntVector2 operator +(IntVector2 left, IntVector2 right)
        {
            return new IntVector2(left.x + right.x, left.y + right.y);
        }
 
        public static IntVector2 operator -(IntVector2 left, IntVector2 right)
        {
            return new IntVector2(left.x - right.x, left.y - right.y);
        }
 
        public static IntVector2 operator *(int scale, IntVector2 left)
        {
            return new IntVector2(left.x * scale, left.y * scale);
        }
 
        public static IntVector2 operator *(IntVector2 left, int scale)
        {
            return new IntVector2(left.x * scale, left.y * scale);
        }
 
        public static Vector2 operator *(float scale, IntVector2 left)
        {
            return new Vector2(left.x * scale, left.y * scale);
        }
 
        public static Vector2 operator *(IntVector2 left, float scale)
        {
            return new Vector2(left.x * scale, left.y * scale);
        }
 
        public static IntVector2 operator -(IntVector2 left)
        {
            return new IntVector2(-left.x, -left.y);
        }
    }
}