Advertisement
Guest User

Untitled

a guest
Jan 15th, 2015
3,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  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<Vector2>
  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(Vector2 first, Vector2 second)
  41.         {
  42.             return IsClockwise(first, second, m_Origin);
  43.         }
  44.  
  45.         #endregion
  46.  
  47.         /// <summary>
  48.         ///     Returns 1 if first comes before second in clockwise order.
  49.         ///     Returns -1 if second comes before first.
  50.         ///     Returns 0 if the points are identical.
  51.         /// </summary>
  52.         /// <param name="first">First.</param>
  53.         /// <param name="second">Second.</param>
  54.         /// <param name="origin">Origin.</param>
  55.         public static int IsClockwise(Vector2 first, Vector2 second, Vector2 origin)
  56.         {
  57.             if (first == second)
  58.                 return 0;
  59.  
  60.             Vector2 firstOffset = first - origin;
  61.             Vector2 secondOffset = second - origin;
  62.  
  63.             float angle1 = Mathf.Atan2(firstOffset.x, firstOffset.y);
  64.             float angle2 = Mathf.Atan2(secondOffset.x, secondOffset.y);
  65.  
  66.             if (angle1 < angle2)
  67.                 return -1;
  68.  
  69.             if (angle1 > angle2)
  70.                 return 1;
  71.  
  72.             // Check to see which point is closest
  73.             return (firstOffset.sqrMagnitude < secondOffset.sqrMagnitude) ? -1 : 1;
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement