forked from twobitcoder101/Flat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlatTransform.cs
111 lines (91 loc) · 3.32 KB
/
FlatTransform.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
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
using System;
using Microsoft.Xna.Framework;
namespace Flat
{
public readonly struct FlatTransform
{
internal readonly float PositionX;
internal readonly float PositionY;
internal readonly float CosScaleX;
internal readonly float SinScaleX;
internal readonly float CosScaleY;
internal readonly float SinScaleY;
private readonly float ScaleX;
private readonly float ScaleY;
private readonly float angle;
public static readonly FlatTransform Identity = new FlatTransform(Vector2.Zero, 0f, Vector2.One);
public static readonly int SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(FlatTransform));
public FlatTransform(Vector2 position, float angle, Vector2 scale)
{
this.ScaleX = scale.X;
this.ScaleY = scale.Y;
this.angle = angle;
float sin = MathF.Sin(angle);
float cos = MathF.Cos(angle);
this.PositionX = position.X;
this.PositionY = position.Y;
this.CosScaleX = cos * scale.X;
this.SinScaleX = sin * scale.X;
this.CosScaleY = cos * scale.Y;
this.SinScaleY = sin * scale.Y;
}
public FlatTransform(Vector2 position, float angle, float scale)
{
this.ScaleX = scale;
this.ScaleY = scale;
this.angle = angle;
float sin = MathF.Sin(angle);
float cos = MathF.Cos(angle);
this.PositionX = position.X;
this.PositionY = position.Y;
this.CosScaleX = cos * scale;
this.SinScaleX = sin * scale;
this.CosScaleY = cos * scale;
this.SinScaleY = sin * scale;
}
public Vector2 GetScale()
{
return new Vector2(this.ScaleX, this.ScaleY);
}
public float GetRotation()
{
return this.angle;
}
public Vector2 GetTranslation()
{
return new Vector2(this.PositionX, this.PositionY);
}
public Matrix ToMatrix()
{
Matrix result = Matrix.Identity;
result.M11 = this.CosScaleX;
result.M12 = this.SinScaleX;
result.M21 = -this.SinScaleY;
result.M22 = this.CosScaleY;
result.M41 = this.PositionX;
result.M42 = this.PositionY;
return result;
}
public bool Equals(FlatTransform other)
{
return this.PositionX == other.PositionX &&
this.PositionY == other.PositionY &&
this.CosScaleX == other.CosScaleX &&
this.SinScaleX == other.SinScaleX &&
this.CosScaleY == other.CosScaleY &&
this.SinScaleY == other.SinScaleY;
}
public override bool Equals(object obj)
{
if(obj is FlatTransform other)
{
return this.Equals(other);
}
return false;
}
public override int GetHashCode()
{
return new { this.PositionX, this.PositionY, this.CosScaleX, this.SinScaleX, this.CosScaleY, this.SinScaleY }.GetHashCode();
}
}
}