Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by R4VANG3R · Jan 15, 2015 at 11:13 AM · arraysort

Vector2 array sort clockwise

So I have an array of Vector2s but these are in the wrong order. I need to have them clockwise to generate a mesh based on these coords.

Is there a quick way of doing this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by VesuvianPrime · Jan 15, 2015 at 01:13 PM

I wrote the following comparer to do this:

http://pastebin.com/icNkfWtX

You can use it in array sort methods to reorder the points to clockwise.

Edit:

I used @ArkaneX's answer to tidy mine a little: http://pastebin.com/1RkaP28U

My solution handles an origin offset, as well as points that occupy the same angle from the origin.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image R4VANG3R · Jan 15, 2015 at 02:08 PM 0
Share

The return values are flipped in your IsClockwise function but other than that thanks!

avatar image
4

Answer by MachinesOfN · Sep 10, 2015 at 02:31 PM

The above isn't quite right. The trig solution below is a bit cleaner and more accurate.

     public class ClockwiseVector3Comparer : IComparer<Vector3>
     {
         public int Compare(Vector3 v1, Vector3 v2)
         {
             return Mathf.Atan2(v1.x, v1.z).CompareTo(Mathf.Atan2(v2.x, v2.z));
         }
      }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image krisventure · Oct 05, 2017 at 07:52 PM 0
Share

Awesome code

avatar image Bunny83 · Oct 05, 2017 at 09:15 PM 1
Share

Actually the accepted answer is more correct. First of all you're dealing with Vector3 values, not Vector2. You only loop at the x-z plane and not the 2d x-y plane.

Also this basically the same as the solution in the accepted answer but less flexible. The points need to be located around the origin in order to properly sort them. The accepted solution allows to specify a center point.

avatar image krisventure Bunny83 · Oct 06, 2017 at 01:49 AM 0
Share

Good points! I guess I was just lucky because I wanted something real quick to match the user defined points into a polygon clockwise and my code had already Vector3 and the points aligned along the same x-z plane (on the surface of the room floor in my AR app). So this was the quickest copy-paste solution in my case lol. But at least I know where to look if I experience any issue due to the origin thing. Thanks!

avatar image
1

Answer by ArkaneX · Jan 15, 2015 at 01:10 PM

To achieve this, you need custom comparer, which you can pass to sort method. C# example:

 public class ClockwiseVector2Comparer : IComparer<Vector2>
 {
     public int Compare(Vector2 v1, Vector2 v2)
     {
         if (v1.x >= 0)
         {
             if (v2.x < 0)
             {
                 return -1;
             }
             return -Comparer<float>.Default.Compare(v1.y, v2.y);
         }
         else
         {
             if (v2.x >= 0)
             {
                 return 1;
             }
             return Comparer<float>.Default.Compare(v1.y, v2.y);
         }
     }
 }

and then sort using the above comparer:

 Array.Sort(vectorArray, new ClockwiseVector2Comparer());

Please note, that above comparer is very basic. For example, it assumes that center of your 'clock' is (0,0) and will not sort vectors with the same direction (and vector (0,0)).

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VesuvianPrime · Jan 15, 2015 at 01:14 PM 0
Share

Oh cool, I didn't realize IComparer could be generic!

avatar image R4VANG3R · Jan 15, 2015 at 01:45 PM 0
Share

$$anonymous$$y Visual Studio says it's a non-generic type. Am I doing something wrong?

avatar image VesuvianPrime · Jan 15, 2015 at 01:50 PM 1
Share

Need to use the System.Collections.Generic module

avatar image R4VANG3R · Jan 15, 2015 at 02:09 PM 0
Share

Also thank you @ArkaneX, learned something new.

avatar image ArkaneX · Jan 15, 2015 at 02:13 PM 0
Share

@R4VANG3R - you're welcome :)

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Matching Index of two Arrays after one Array is sort 3 Answers

Sorting builtin arrays 2 Answers

Sorting Array 1 Answer

Extend Unity ObjectField / Array function/Gui interface 1 Answer

C# copy Sorting data of An Array to Another Array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges