summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended/Shapes/Polygon.cs
blob: 841ce2be6da3c96c4a6782b981ca5cbe69d97d45 (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;

namespace MonoGame.Extended.Shapes
{
    public class Polygon : IEquatable<Polygon>
    {
        public Polygon(IEnumerable<Vector2> vertices)
        {
            _localVertices = vertices.ToArray();
            _transformedVertices = _localVertices;
            _offset = Vector2.Zero;
            _rotation = 0;
            _scale = Vector2.One;
            _isDirty = false;
        }

        private readonly Vector2[] _localVertices;
        private Vector2[] _transformedVertices;
        private Vector2 _offset;
        private float _rotation;
        private Vector2 _scale;
        private bool _isDirty;

        public Vector2[] Vertices
        {
            get
            {
                if (_isDirty)
                {
                    _transformedVertices = GetTransformedVertices();
                    _isDirty = false;
                }

                return _transformedVertices;
            }
        }

        public float Left
        {
            get { return Vertices.Min(v => v.X); }
        }

        public float Right
        {
            get { return Vertices.Max(v => v.X); }
        }

        public float Top
        {
            get { return Vertices.Min(v => v.Y); }
        }

        public float Bottom
        {
            get { return Vertices.Max(v => v.Y); }
        }

        public void Offset(Vector2 amount)
        {
            _offset += amount;
            _isDirty = true;
        }

        public void Rotate(float amount)
        {
            _rotation += amount;
            _isDirty = true;
        }

        public void Scale(Vector2 amount)
        {
            _scale += amount;
            _isDirty = true;
        }

        private Vector2[] GetTransformedVertices()
        {
            var newVertices = new Vector2[_localVertices.Length];
            var isScaled = _scale != Vector2.One;

            for (var i = 0; i < _localVertices.Length; i++)
            {
                var p = _localVertices[i];

                if (isScaled)
                    p *= _scale;

                // ReSharper disable once CompareOfFloatsByEqualityOperator
                if (_rotation != 0)
                {
                    var cos = (float) Math.Cos(_rotation);
                    var sin = (float) Math.Sin(_rotation);
                    p = new Vector2(cos*p.X - sin*p.Y, sin*p.X + cos*p.Y);
                }

                newVertices[i] = p + _offset;
            }

            return newVertices;
        }

        public Polygon TransformedCopy(Vector2 offset, float rotation, Vector2 scale)
        {
            var polygon = new Polygon(_localVertices);
            polygon.Offset(offset);
            polygon.Rotate(rotation);
            polygon.Scale(scale - Vector2.One);
            return new Polygon(polygon.Vertices);
        }

        public RectangleF BoundingRectangle
        {
            get
            {
                var minX = Left;
                var minY = Top;
                var maxX = Right;
                var maxY = Bottom;
                return new RectangleF(minX, minY, maxX - minX, maxY - minY);
            }
        }

        public bool Contains(Vector2 point)
        {
            return Contains(point.X, point.Y);
        }

        public bool Contains(float x, float y)
        {
            var intersects = 0;
            var vertices = Vertices;

            for (var i = 0; i < vertices.Length; i++)
            {
                var x1 = vertices[i].X;
                var y1 = vertices[i].Y;
                var x2 = vertices[(i + 1)%vertices.Length].X;
                var y2 = vertices[(i + 1)%vertices.Length].Y;

                if ((((y1 <= y) && (y < y2)) || ((y2 <= y) && (y < y1))) && (x < (x2 - x1)/(y2 - y1)*(y - y1) + x1))
                    intersects++;
            }

            return (intersects & 1) == 1;
        }

        public static bool operator ==(Polygon a, Polygon b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(Polygon a, Polygon b)
        {
            return !(a == b);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return obj is Polygon && Equals((Polygon) obj);
        }

        public bool Equals(Polygon other)
        {
            return Vertices.SequenceEqual(other.Vertices);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return Vertices.Aggregate(27, (current, v) => current + 13*current + v.GetHashCode());
            }
        }
    }
}