Subversion Repositories AndroidProjects

Rev

Rev 1423 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
782 chris 1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
 
7
namespace BauzoidNET.math
8
{
787 chris 9
    public class Vector2
782 chris 10
    {
1423 chris 11
        public float X
12
        {
13
            get { return x; }
14
            set { x = value; }
15
        }
16
 
17
        public float Y
18
        {
19
            get { return y; }
20
            set { y = value; }
21
        }
22
 
782 chris 23
        public float x;
24
        public float y;
25
 
1423 chris 26
        public override string ToString()
27
        {
28
            return "Vector2";
29
        }
30
 
31
 
782 chris 32
        public Vector2()
33
        {
34
        }
35
 
1421 chris 36
        public Vector2(Vector2 o)
37
        {
38
            set(o.x, o.y);
39
        }
40
 
782 chris 41
        public Vector2(float _x, float _y)
42
        {
1405 chris 43
            set(_x, _y);
44
        }
45
 
46
        public void set(float _x, float _y)
47
        {
782 chris 48
            x = _x;
49
            y = _y;
50
        }
51
 
1441 chris 52
        public void setFrom(Vector2 other)
53
        {
54
            x = other.x;
55
            y = other.y;
56
        }
57
 
782 chris 58
        public void normalize()
59
        {
60
            float len = length();
61
            if (len != 0.0f)
62
            {
63
                x /= len;
64
                y /= len;
65
            }
66
        }
67
 
68
        public float length()
69
        {
70
            float squared = squaredLength();
71
            return (float)Math.Sqrt(squared);
72
        }
73
 
74
        public void setLength(float newLength)
75
        {
76
            normalize();
77
            x *= newLength;
78
            y *= newLength;
79
        }
80
 
81
        public bool isZero()
82
        {
83
            return ((x == 0.0f) && (y == 0.0f));
84
        }
85
 
86
        public float squaredLength()
87
        {
88
            return (x * x + y * y);
89
        }
90
 
91
        public void addVector(Vector2 v)
92
        {
93
            x += v.x;
94
            y += v.y;
95
        }
96
 
97
        public void subtractVector(Vector2 v)
98
        {
99
            x -= v.x;
100
            y -= v.y;
101
        }
102
 
103
        public float getAngle()
104
        {
105
            float angle = (float)Math.Atan2(x, y);
106
            return MathUtil.stayInDegrees0to360(MathUtil.radToDeg(angle));
107
        }
108
 
109
        public float dotProduct(Vector2 v)
110
        {
111
            return dotProduct(this, v);
112
        }
113
 
114
        static public float dotProduct(Vector2 a, Vector2 b)
115
        {
116
            return (a.x * b.x + a.y * b.y);
117
        }
118
 
119
        static public float distance(Vector2 a, Vector2 b)
120
        {
121
            Vector2 c = new Vector2(b.x - a.x, b.y - a.y);
122
            return c.length();
123
        }
124
 
125
        static public Vector2 fromRotation(float angle, float length)
126
        {
127
            Vector2 result = fromRotation(angle);
128
            result.setLength(length);
129
            return result;
130
        }
131
 
132
        static public Vector2 fromRotation(float angle)
133
        {
134
            return new Vector2((float)Math.Cos(MathUtil.degToRad(angle)), (float)Math.Sin(MathUtil.degToRad(angle)));
135
        }
136
 
137
        /** Return a copy of this Vector2. */
138
        public Vector2 copy()
139
        {
140
            return new Vector2(x, y);
141
        }
811 chris 142
 
143
        /// <summary>
144
        /// Return the largest component of the Vector3.
145
        /// </summary>
146
        public float max()
147
        {
148
            return Math.Max(x, y);
149
        }
150
 
151
        /// <summary>
152
        /// Return the smallest component of the Vector3.
153
        /// </summary>
154
        public float min()
155
        {
156
            return Math.Min(x, y);
157
        }
782 chris 158
    }
159
}