source: OniSplit/Math/Vector2.cs@ 1126

Last change on this file since 1126 was 1114, checked in by iritscen, 5 years ago

Adding OniSplit source code (v0.9.99.0). Many thanks to Neo for all his work over the years.

File size: 2.7 KB
RevLine 
[1114]1using System;
2
3namespace Oni
4{
5 internal struct Vector2 : IEquatable<Vector2>
6 {
7 public float X;
8 public float Y;
9
10 public Vector2(float x, float y)
11 {
12 X = x;
13 Y = y;
14 }
15
16 public Vector2(float all)
17 {
18 X = all;
19 Y = all;
20 }
21
22 public static Vector2 operator +(Vector2 v1, Vector2 v2) => new Vector2
23 {
24 X = v1.X + v2.X,
25 Y = v1.Y + v2.Y
26 };
27
28 public static Vector2 operator -(Vector2 v1, Vector2 v2) => new Vector2
29 {
30 X = v1.X - v2.X,
31 Y = v1.Y - v2.Y
32 };
33
34 public static float Dot(Vector2 v1, Vector2 v2) => v1.X * v2.X + v1.Y * v2.Y;
35
36 public static Vector2 Normalize(Vector2 v) => v * (1.0f / v.Length());
37
38 public void Normalize()
39 {
40 float f = 1.0f / Length();
41
42 X *= f;
43 Y *= f;
44 }
45
46 public float Length() => FMath.Sqrt(X * X + Y * Y);
47
48 public static Vector2 operator *(Vector2 v, float s)
49 {
50 v.X *= s;
51 v.Y *= s;
52
53 return v;
54 }
55
56 public static Vector2 operator *(float s, Vector2 v)
57 {
58 v.X *= s;
59 v.Y *= s;
60
61 return v;
62 }
63
64 public static Vector2 operator /(Vector2 v, float s) => v * (1.0f / s);
65
66 public static Vector2 Min(Vector2 v1, Vector2 v2) => new Vector2
67 {
68 X = (v1.X < v2.X) ? v1.X : v2.X,
69 Y = (v1.Y < v2.Y) ? v1.Y : v2.Y
70 };
71
72 public static Vector2 Max(Vector2 v1, Vector2 v2) => new Vector2
73 {
74 X = (v1.X > v2.X) ? v1.X : v2.X,
75 Y = (v1.Y > v2.Y) ? v1.Y : v2.Y
76 };
77
78 public static bool operator ==(Vector2 v1, Vector2 v2) => v1.X == v2.X && v1.Y == v2.Y;
79 public static bool operator !=(Vector2 v1, Vector2 v2) => v1.X != v2.X || v1.Y != v2.Y;
80 public bool Equals(Vector2 other) => X == other.X && Y == other.Y;
81 public override bool Equals(object obj) => obj is Vector2 && Equals((Vector2)obj);
82 public override int GetHashCode() => X.GetHashCode() ^ Y.GetHashCode();
83
84 public override string ToString() => $"{{{X} {Y}}}";
85
86 private static Vector2 zero = new Vector2(0.0f, 0.0f);
87 private static Vector2 one = new Vector2(1.0f, 1.0f);
88 private static Vector2 unitX = new Vector2(1.0f, 0.0f);
89 private static Vector2 unitY = new Vector2(0.0f, 1.0f);
90
91 public static Vector2 Zero => zero;
92 public static Vector2 One => one;
93 public static Vector2 UnitX => unitX;
94 public static Vector2 UnitY => unitY;
95 }
96}
Note: See TracBrowser for help on using the repository browser.