Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 seandolan · Jan 04, 2018 at 12:32 PM · arraycircleinteger

Clear a circle on a game map build from an int[x,y] array

I have a game map created from int[,] map that contains either a 1 or a 0 in an x and y coordinate. I want to clear an area of the map (i.e. set all coordinates in a circle pattern to 0) regardless of what it was set to before.

I figure I could run through each x y coordinate on the map and work out if it's distance from the x,y center coordinate is in range of the size - however I am unsure if my math calculation is the right away to do it and if the code is correct.

To demonstrate I have created an image to show (1) the existing int array (2) the coordinates I want to be changed by sending an x,y and an int circleSize (3) the result by changing all of these x,y's to zeros.

I have also supplied the function I have tried and confused myself on. The int[,] map is already assigned 1's and 0's.

alt text

 void ClearCircleOnMap(int startX, int startY, int circleSize){
         for (int x = startX - circleSize; x < startX + circleSize; x++){
             for (int y = startY - circleSize; y < startY + circleSize; y++){
                 map[x,y] = (Mathf.Sqrt((Mathf.Pow(startX - x,2f)) + (Mathf.Pow(startY - y,2f)) < circleSize)) ? 1 : map[x,y];
             }
         }
     }

circleclear.png (35.3 kB)
Comment
Add comment · Show 1
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 seandolan · Jan 04, 2018 at 12:36 PM 0
Share

I was trying to use the a squared + b squared = c squared and seeing if that is less that the circleSize (radius from center). I realise now too one of the problems is I should be getting the magnitude and comparing it to the circleSize value - but I think my logic may be flawed from the start and there would be an easier way to do it anyway.

1 Reply

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

Answer by seandolan · Jan 04, 2018 at 01:42 PM

Here was the solution:

 void ClearCircleOnMap(int startX, int startY, int circleSize){
         Debug.Log (startX - circleSize);
         Debug.Log (startX + circleSize);
         for (int x = startX - circleSize; x < startX + circleSize; x++){
             for (int y = startY - circleSize; y < startY + circleSize; y++){
                 //map[x,y] = (Mathf.Sqrt(Mathf.Pow(x,2f) + (Mathf.Pow(y,2f))) < circleSize) ? 1 : map[x,y];
                 if (x > 0 && x < mapWidth - 1 && y > 0 && y < mapHeight - 1) {
                     if (Mathf.Sqrt (Mathf.Pow (startX - x, 2f) + Mathf.Pow (startY - y, 2f)) < circleSize) {
                         map [x, y] = 0;
                     }
                 }
             }
         }
     }
Comment
Add comment · 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

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

81 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 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 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

Checking to see if a loaded level is equal to a member of an integer array 1 Answer

Randoim values in a dynamic array C# 1 Answer

How to convert and integer into an Array of his digits 4 Answers

Javascript array questions. 1 Answer

Error with an 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