Advertisement
Guest User

Vector2 Clockwise Comparer

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