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
0
Question by filler03 · Jul 07, 2014 at 10:30 PM · functionalgebra

Functionality for iterating through every 2D coordinate?

I need a way of iterating through ordered pairs in a spiral fashion like this: (0, 0) (1, 0) (1, 1) (0, 1) (-1, 1) (-1, 0) (-1, -1) (0, -1) (1, -1)

This would be one revolution of the spiral, from there it would move on in the same manner. I would like it to be able to look at the previous pair and determine the next one. Any ideas??

Comment
Add comment · Show 6
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 SirCrazyNugget · Jul 08, 2014 at 01:14 AM 0
Share

How are you creating this? Do you want to define n where n is the maximum (and -n the $$anonymous$$imum) which will always give you a square array OR where n is the maximum number of pairs to be iterated through?

avatar image Graham-Dunnett ♦♦ · Jul 08, 2014 at 10:54 AM 0
Share

Put the pairs in a list and then iterate over the list?

avatar image filler03 · Jul 08, 2014 at 10:00 PM 0
Share

There are going to potentially be hundreds of thousands of pairs total, I would like a method that can take the previous pair and output the next one.

avatar image OP_toss · Jul 08, 2014 at 11:39 PM 0
Share

$$anonymous$$ight this help?

http://stackoverflow.com/questions/3706219/algorithm-for-iterating-over-an-outward-spiral-on-a-discrete-2d-grid-from-the-or

avatar image flaviusxvii · Jul 08, 2014 at 11:58 PM 0
Share

This has nothing to do with Unity.

Show more comments

2 Replies

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

Answer by SirCrazyNugget · Jul 08, 2014 at 11:54 PM

Took a little longer than planned but:

 Vector2 NextDirection(int x, int y){
     if(x == 0 && y == 0) return new Vector2(1, 0);
     float a = Mathf.Atan2 (y, x) / Mathf.PI;
     if(a >= -0.75f && a <= -0.25f) return new Vector2(x+1, y);
     if(a >= 0.25f && a < 0.75f) return new Vector2(x-1, y);
     if(a > -0.25 && a < 0.25f) return new Vector2(x, y+1);
     return new Vector2(x, y-1);
 }
Comment
Add comment · Show 2 · 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 Bunny83 · Jul 09, 2014 at 12:54 AM 0
Share

You should change the parameter type from int to float or use a single Vector2, otherwise great solution ;)

+1

avatar image filler03 · Jul 09, 2014 at 12:58 AM 0
Share

I use a custom class called IntVec2, which is a Vector2 with integers ins$$anonymous$$d of floats. So I think this might work I will give it a try!

avatar image
0

Answer by Bunny83 · Jul 09, 2014 at 04:01 AM

It's easier to understand what happens when using Atan2, however it's even possible without any trigonomety:

     Vector2 NextSpiralStep(int x, int y){
         if(x == 0 && y == 0) return new Vector2(1, 0);
         if(Mathf.Abs(x) > Mathf.Abs(y)+0.5f*Mathf.Sign(x) && Mathf.Abs(x) > (-y+0.5f))
             y += (int)Mathf.Sign(x);
         else
             x -= (int)Mathf.Sign(y);
         return new Vector2(x,y);
     }
 
     Vector2 NextSpiralCorner(int x, int y){
         if ((x<=0 && y<=0) || (x>0 && y >0))
         {
             x = -x;
             if(x>=0)
                 x++;
         }
         else
             y = x;
         return new Vector2(x,y);
     }

NextSpiralStep returns the next point on the spiral and moves on 1 each step.

NextSpiralCorner calculates the whole edges of the spiral. So 4 steps is always one turn. I changed the parameters back to integers. You just have to replace your IntVec2. I also have such a vector (Vector3i) however it's better to keep the solutions as generic as possible ;)

I've tested SirCrazyNugget's solution and it works without any problems. Mine does the same but without atan.

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 filler03 · Jul 10, 2014 at 03:54 PM 0
Share

This looks like a good solution too. I chose to go with the atan2 solution because it is simpler, and was easier to convert to PHP.

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

25 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

Related Questions

How do you make an algebra function for this pattern? 1 Answer

how to make a save and load function 0 Answers

How to call a function from another script in C# from array? 1 Answer

How to use yield within a class function 2 Answers

Help with UnityException Error 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