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 :slight_smile:

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)