View difference between Paste ID: ri5tfkXx and icNkfWtX
SHOW: | | - or go back to the newest paste.
1
using System.Collections;
2
using UnityEngine;
3
using System.Collections.Generic;
4
5
namespace Hydra.HydraCommon.Utils.Comparers
6
{
7
	/// <summary>
8
	/// 	ClockwiseComparer provides functionality for sorting a collection of Vector2s such
9
	/// 	that they are ordered clockwise about a given origin.
10-
	public class ClockwiseComparer : IComparer
10+
11
	public class ClockwiseComparer : IComparer<Vector2>
12
	{
13
		private Vector2 m_Origin;
14
15
		#region Properties
16
17
		/// <summary>
18
		/// 	Gets or sets the origin.
19
		/// </summary>
20
		/// <value>The origin.</value>
21
		public Vector2 origin { get { return m_Origin; } set { m_Origin = value; } }
22
23
		#endregion
24
25
		/// <summary>
26
		/// 	Initializes a new instance of the ClockwiseComparer class.
27
		/// </summary>
28
		/// <param name="origin">Origin.</param>
29
		public ClockwiseComparer(Vector2 origin)
30
		{
31
			m_Origin = origin;
32
		}
33
34
		#region IComparer Methods
35
36
		/// <summary>
37
		/// 	Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
38
		/// </summary>
39
		/// <param name="first">First.</param>
40-
		public int Compare(object first, object second)
40+
41
		public int Compare(Vector2 first, Vector2 second)
42-
			Vector2 pointA = (Vector2)first;
42+
43-
			Vector2 pointB = (Vector2)second;
43+
			return IsClockwise(first, second, m_Origin);
44
		}
45-
			return IsClockwise(pointB, pointA, m_Origin);
45+
46
		#endregion
47
48
		/// <summary>
49
		/// 	Returns 1 if first comes before second in clockwise order.
50
		/// 	Returns -1 if second comes before first.
51
		/// 	Returns 0 if the points are identical.
52
		/// </summary>
53
		/// <param name="first">First.</param>
54
		/// <param name="second">Second.</param>
55
		/// <param name="origin">Origin.</param>
56
		public static int IsClockwise(Vector2 first, Vector2 second, Vector2 origin)
57
		{
58
			if (first == second)
59
				return 0;
60
61
			Vector2 firstOffset = first - origin;
62
			Vector2 secondOffset = second - origin;
63
64
			float angle1 = Mathf.Atan2(firstOffset.x, firstOffset.y);
65
			float angle2 = Mathf.Atan2(secondOffset.x, secondOffset.y);
66
67
			if (angle1 < angle2)
68
				return -1;
69
70
			if (angle1 > angle2)
71
				return 1;
72
73
			// Check to see which point is closest
74
			return (firstOffset.sqrMagnitude < secondOffset.sqrMagnitude) ? -1 : 1;
75
		}
76-
			return (firstOffset.sqrMagnitude < secondOffset.sqrMagnitude) ? 1 : -1;
76+
77
}