Jump to content
Unity Insider Forum

Koordinaten im Uhrzeigersinn sortieren


Ismoh

Recommended Posts

Ola zusammen,

 

ich möchte für meine Sprites passende PolygonCollider2D generieren lassen.

 

Bisher klappt das ganz gut, jedoch muss ich die ColliderPoints noch im Uhrzeigersinn sortieren und da hapert es momentan an Ideen.

 

Würde mich über jegliche Anregung freuen.

 

Ohne Sortierung:

post-3816-0-69543600-1472409500_thumb.png

 

Sortierung:

Derzeitiger Code:

/// <summary>
///	 ClockwiseComparer provides functionality for sorting a collection of Vector2s such
///	 that they are ordered clockwise about a given origin.
/// </summary>
public class ClockwiseComparer : IComparer<Vector2>
{
private Vector2 m_Origin;

#region Properties

/// <summary>
///	 Gets or sets the origin.
/// </summary>
/// <value>The origin.</value>
public Vector2 origin { get { return m_Origin; } set { m_Origin = value; } }

#endregion

/// <summary>
///	 Initializes a new instance of the ClockwiseComparer class.
/// </summary>
/// <param name="origin">Origin.</param>
public ClockwiseComparer(Vector2 origin)
{
	m_Origin = origin;
}

#region IComparer Methods

/// <summary>
///	 Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
/// </summary>
/// <param name="first">First.</param>
/// <param name="second">Second.</param>
public int Compare(Vector2 first, Vector2 second)
{
	//return IsClockwise(first, second, m_Origin);
	return IsClockwise2(first, second, m_Origin);
}

#endregion

/// <summary>
///	 Returns 1 if first comes before second in clockwise order.
///	 Returns -1 if second comes before first.
///	 Returns 0 if the points are identical.
/// </summary>
/// <param name="first">First.</param>
/// <param name="second">Second.</param>
/// <param name="origin">Origin.</param>
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
	if(firstOffset.sqrMagnitude < secondOffset.sqrMagnitude)
	{
		return -1;
	}
	else
	{
		return 1;
	}
}
}

 

Ergebnis:

post-3816-0-69543600-1472409500_thumb.png

 

Es ist also kein Unterschied zu sehen.

 

//EDIT:

Ich habe dank Google einen Comparer gefunden, jedoch ist mir aufgefallen, dass second folgende Werte hat: (4.0, 4.0)

 

Meine Frage dazu:

first hat folgenden Wert (2.0, 3.0) - das verstehe ich auch noch: erster Eintrag aus dem Array.

second hat den Wert (4.0, 4.0) - was ich nicht verstehe, da ich eigentlich vermutet habe, dass Array[0] mit Array[1] verglichen wird.

 

Kann mir da Jemand auf die Sprünge helfen?

 

origin ist in meinem Fall: texture.width /2, texture.height /2

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...