# Getting blocks in radius around a "center"

Discussion in 'Plugin Development' started by MrJossy, May 22, 2015.

Not open for further replies.
1. Offline

### MrJossy

Hello!

Today I'm here hoping that someone helps me with something! Getting the blocks in a certain radius.
I'm going to use this for my BlockUtil.
So if someone knows how to get blocks in a radius, could you help me?

Last edited: May 23, 2015
#1
2. Offline

### mine-care

Okay I made such a thing a little while ago, I know how you can do it by the one side to the diagonal one and have a nested loop do the rest, now if you want to calculate these 2 points from a center location you need to take the center location and add the radius to x,y,z and then do the same for another location where instead of adding the radius to x ,z and y, you subtract it so you have the two diagonal points (as seen on world edit )
And then a triple nested loop for x,y,z :- ) I came throw a tutorial since I'm on phone :3

#2
3. Offline

### MrJossy

Now...
Can you say that in English?

#3
4. Offline

### mythbusterma

@MrJossy

The simplest way (not necessarily the fastest by any means, however) would be to make nested for-loops that iterate in x, y, and z from each edge of the cube that encompasses the sphere and check the distance from that block to the centre, seeing if it's greater than the radius.

Again, this is not very fast, but it is the simplest to understand.

#4
5. Offline

### 567legodude

@mythbusterma So you mean something like this, or did I do it wrong?
Code:
```public static ArrayList<Block> getBlocksAroundCenter(Location loc, int radius) {
ArrayList<Block> blocks = new ArrayList<Block>();

for (int x = (loc.getBlockX()-radius); x <= (loc.getBlockX()+radius); x++) {
for (int y = (loc.getBlockY()-radius); y <= (loc.getBlockY()+radius); y++) {
for (int z = (loc.getBlockZ()-radius); z <= (loc.getBlockZ()+radius); z++) {
Location l = new Location(loc.getWorld(), x, y, z);
if (l.distance(loc) <= radius) {
}
}
}
}

return blocks;
}```

#5
6. Offline

### Zombie_Striker

snip

Last edited: May 22, 2015
#6
7. Offline

### 567legodude

@Zombie_Striker I don't need to use it, and I don't know how I would test it.

#7
8. Offline

#8
9. Offline

### 567legodude

@Zombie_Striker What do you mean, before the message was edited you said you thought the code was correct, and that I should test it.

#9
10. Offline

### MrJossy

Thanks. I will try this

#10
11. Offline

#11
12. Offline

### _Filip

Do you need a spherical or a circular selection?

#12
13. Offline

### MrJossy

Circular and/or spherical.

#13
14. Offline

### RingOfStorms

I would recommend squaring your radius once and doing distanceSquared instead of distance. It will drastically improve the speed in large radii

#14
15. Offline

### mythbusterma

@RingOfStorms

Not really, Bukkit docs lied on that one. On every Java environment I have tested it on, the difference between taking a square root and not taking one is negligible at best.

#15
16. Offline

### RingOfStorms

If you are looking at a radius of about 100, then you are going to be doing a distance check on 100^3 blocks using the nested for loop method. Using the sqrt function a million times does actually have a very very noticeable difference on performance. It can make a difference of even 13ms which is a huge amount of time when it comes to the performance of a program.

Here is some evidence on just one system, but you can easily make some more elaborate tests and you will still see a performance reduction http://ideone.com/mHBQPn
Obviously if you're at a radius of less than 50 then the performance isn't very noticeable, but if you ever start doing larger things it can mean the world of difference.

#16
KingFaris11 likes this.
17. Offline

### Forge_User_62502025

I hope your results are correct because I hate using distanceSquared since I have to remember to square all the variables, which I usually forget to do, leading to me getting frustrated about why the plugin won't work and finally realising after a few minutes... I'd much prefer to use distance instead, just didn't want to as I was always told distanceSquared is quicker.

Edit: Well... RingOfStorms just ninja'd and said something against this soooo... Nvm xD

18. Offline

#18