Convert a square array selection to circular selection
Hi Everyone,
I've got a bit of a problem when trying to edit my terrain.
I've got a square object which makes it easy to select an float array of heights under it based on the objects height etc.
The problem i have is if i want to edit the terrain but rather edit everything in the array (giving a square edit), I was wanting to edit the terrain in a more circular fashion but im not sure how to do it in code.
The idea if if I have an 8x8 int[] array I'd like to flatten a circle within that selection.
e.g. Array position to edit
01 02 03 04 05 06 07 08
09 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
25 26 27 28 29 30 31 32
33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48
49 50 51 52 53 54 55 56
57 58 59 60 61 62 63 64
I Hope that makes sense :)
Answer by NoseKills · Jan 18, 2016 at 12:04 AM
You can calculate the x and y coordinates of each of those cells in your rectangular selection and use distance from center to select a round area from it.
for (int i = 0; i < array.Length; i++)
{
var x = i % width; // means rect grid width : 8
var y = i / width;
var halfWidth= width / 2;
var thisPos = new Vector2(x, y);
var center = new Vector2(halfWidth, halfWidth);
var distanceSq = (thisPos - center).sqrMagnitude;
if (distanceSq < halfWidth*halfWidth) // compare squares to avoid slow/heavy Mathf.sqrt()
{
// index is within radius of halfWidth
}
}
This isn't exactly right and won't give you the result shown in the pic but you should get there by modifying the formula to measure distance from between the center cells (4.5f, 4.5f) and perhaps by loosening the range condition if (distanceSq < (halfWidth*halfWidth) + 0.5f)
Thanks Nose$$anonymous$$ills,
Yeah a little modification but i understand how it works, I'll put the final result up once I test at home :D
Your answer
Follow this Question
Related Questions
Unity Desert Sand Dunes 0 Answers
Array[] of children of a child 3 Answers
How to play a random audio clip from an array in C#? 3 Answers
how to not repeat random array 1 Answer
Change Terrain Heightmap Resolution without Resizing Terrain 1 Answer