I'm trying to use a comparer that sorts a List of Vector2 points by their clockwise position from an origin. I got the comparer code from here https://pastebin.com/1RkaP28U#, and its class is called ClockwiseComparer. Due to the Hydra.HydraCommon.Utils.Comparers line, I figured I would have to add
using Hydra.HydraCommon.Utils.Comparers;
to the script that I use the Comparer in, which is called Player. So, I create the `ClockwiseComparer comparer` object within Player, and set it to `new ClockwiseComparer(origin)` However, when I try to use it at list.Sort(comparer), the compiler gives this complaint.
Cannot convert from Hydra.HydraCommon.Utils.Comparers to System.Collections.Generic.IComparer
______________________________________________________
That was the original problem. Afterward I tried to solve it by making the ClockwiseComparer class into something more resembling `System.Collections.Generic.IComparer`. I changed the namespace to `namespace System.Collections.Generic.IComparer`, and likewise added `using System.Collections.Generic.IComparer;` to the Player class. This simply resulted in
cannot convert from System.Collections.Generic.IComparer.ClockwiseComparer to System.Collections.Generic.IComparer
Here are both the Player and CounterClockwise scripts from the latter case.
using System.Collections.Generic.IComparer;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
[SerializeField] float moveSpeed = 10f;
[SerializeField] float padding = 1f;
[SerializeField] GameObject projectile;
[SerializeField] GameObject flag;
[SerializeField] float projectileSpeed =10f;
ClockwiseComparer comp;
Vector2 origin;
float xMin;
float xMax;
float yMin;
float yMax;
MeshFilter filter;
List flags = new List();
// Use this for initialization
void Start () {
SetUpMoveBoundaries();
StartCoroutine(delayedLog());
InitializeBaseMesh();
}
// Update is called once per frame
void Update () {
FireLaser();
PlaceFlag();
}
private void FindFlagOrigin()
{
float totalX = 0;
float totalY = 0;
foreach (GameObject flag in flags)
{
totalX += flag.transform.position.x;
totalY += flag.transform.position.y;
}
float centerX = totalX / flags.Count;
float centerY = totalY / flags.Count;
origin = new Vector2(centerX, centerY);
if (comp == null)
comp = new ClockwiseComparer(origin);
}
private void ReorderFlags()
{
flags.Sort(comp);
/*for (int i=0; i
{
private Vector2 m_Origin;
#region Properties
///
/// Gets or sets the origin.
///
/// The origin.
public Vector2 origin { get { return m_Origin; } set { m_Origin = value; } }
#endregion
///
/// Initializes a new instance of the ClockwiseComparer class.
///
/// Origin.
public ClockwiseComparer(Vector2 origin)
{
m_Origin = origin;
}
#region IComparer Methods
///
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
///
/// First.
/// Second.
public int Compare(Vector2 first, Vector2 second)
{
return IsClockwise(first, second, m_Origin);
}
#endregion
///
/// Returns 1 if first comes before second in clockwise order.
/// Returns -1 if second comes before first.
/// Returns 0 if the points are identical.
///
/// First.
/// Second.
/// Origin.
public static int IsClockwise(Vector2 first, Vector2 second, Vector2 origin)
{
if (first == second)
return 0;
Vector2 firstOffset = first - origin;
Vector2 secondOffset = second - origin;
float angle1 = Mathf.Atan2(firstOffset.x, firstOffset.y);
float angle2 = Mathf.Atan2(secondOffset.x, secondOffset.y);
if (angle1 < angle2)
return -1;
if (angle1 > angle2)
return 1;
// Check to see which point is closest
return (firstOffset.sqrMagnitude < secondOffset.sqrMagnitude) ? -1 : 1;
}
}
}
Thank you for reading.
↧