# Solved Slope Help

Discussion in 'Plugin Development' started by messageofdeath, Jul 24, 2014.

Not open for further replies.
1. Offline

### messageofdeath

How would I be able to get the blocks within two points like this.
The Green squares being the points and the red ones are the ones that the method gets.
I have no idea where to start.

Also this will be on a flat surface like the ground #1
2. Offline

### DannyDog

messageofdeath
We'll what you can do is start with a method that gets the two point's location.
Then you'll need some good maths wiz to get those red blocks.

#2
JBoss925 likes this.
3. Offline

### messageofdeath

DannyDog
I already have the two locations in my saved class, but I just need to get the red blocks.

#3
4. Offline

### Dragonphase

messageofdeath
I think the process you want to achieve is known as Pathfinding. There's a pretty decent resource here, which allows you to find a path from a start location and a target location.

#4
5. Offline

### TheHandfish

Get the vector between two locations, then try using a block iterator.

#5
6. Offline

### JBoss925

This is very easy.
delta z
----------
delta x
In order to do this, store 3 vars, the basex, basey, and basez.

Then find the delta z divide it by whatever the x is and multiply it by the ratio.

Example:

basex/basey/basez are the lower values.

2 points:
point 1: 10, 60, 10
point 2: 20, 60, 15

basex = 10
basey = 60
basez = 10

change in z over change in x is:
5/10 = 1/2

Now iterate through the x's going up by 1 each time and get the relative x and then the z from the relative x. An example is provided below:

Now, get the point at a certain x, let's say it's x = 15.

We do x - basex(which is 10) = 5 let's call this value newx

Now we do 1/2 * newx(newx is 5) which is 2.5 + basez(which is 10 also) = 12.5 lets call this value newz

world.getBlockAt(newx, basey, newz) and it returns the block when x = 15;

code example:
Code:java
`loc1 = your location;loc2 = your other location; double basex = Math.min(loc1.getX(), loc2.getX());double basey = Math.min(loc1.getY(), loc2.getY());double basez = Math.min(loc1.getZ(), loc2.getZ());double overx = Math.max(loc1.getX(), loc2.getX());double overy = Math.max(loc1.getY(), loc2.getY());double overz = Math.max(loc1.getZ(), loc2.getZ()); double changeinz = overz - basez;double changeinx = overx - basex; ArrayList<Block> blocks = new ArrayList<Block>(); for(int x = basex; x <= overx; x++){double currx = x - basex;double slope = changeinz/changeinx;double z = slope * currx + basez;double newx = currx + basex;blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));}`

#6
Giraffeknee likes this.
7. Offline

### messageofdeath

I will try the PathFinder solution first.

#7
8. Offline

### JBoss925

EDIT: Ok it's all good.

#8
9. Offline

### messageofdeath

I'll try this because of effort Thank you very much, I will try it now

#9
JBoss925 likes this.
10. Offline

### JBoss925

Ok cool, just make sure you use doubles for better calculation. #10
11. Offline

### Dragonphase

JBoss925

How does this compensate for changes in the Y axe though?

#11
12. Offline

### JBoss925

Lol he said it was gonna be flat, no changes in y. Plus changes in y is as simple as another for loop.

#12
13. Offline

### messageofdeath

It seems I will have to put more calculations into it due to it not calculating properly, but it is so close. Thank you.

JBoss925 likes this.
14. Offline

### Dragonphase

JBoss925
Fair do; I like the concept of little effort where required.

#14
15. Offline

### JBoss925

Why are you arguing with me about this. I did what he asked, sorry I didn't want to confuse him with more intense mathematics. In a 3D plane you must calculate z too and it get's messy.

#15
16. Offline

### messageofdeath

JBoss925 Dragonphase
It's really close, this is all thats wrong with it. I'll fix the rest.

GoldBlock = Pathfinder 17. Offline

### JBoss925

Hmmmm, what results are you getting? I think I know why this is. I can very quickly redo the correct code.

#17
18. Offline

### messageofdeath

JBoss925
At the top left hand corner. The diamond blocks are in the wrong place. It goes through the gold blocks which is the correct path. The left and right of the track don't show anything. Probably due to dividing by 0

19. Offline

### JBoss925

Code:java
`loc1 = your location;loc2 = your other location; double basex = loc1.getX()double basey = loc1.getY()double basez = loc1.getZ()double overx = loc2.getX();double overy = loc2.getY();double overz = loc2.getZ(); double changeinz = overz - basez;double changeinx = overx - basex; ArrayList<Block> blocks = new ArrayList<Block>(); if(basez<= overx){for(int x = basex; x <= overx; x++){double currx = x - basex;double slope = changeinz/changeinx;double z = slope * currx + basez;double newx = currx + basex;blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));}} if(basez > overx){for(int x = basex; x >= overx; x--){double currx = x - basex;double slope = changeinz/changeinx;double z = slope * currx + basez;double newx = currx + basex;blocks.add(loc1.getWorld().getBlockAt(newx, basey, z));}} }`

I think I fixed it. Probably cuz of my auto assigning I picked up from my cuboidsapi. Should've fixed it above.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 9, 2016
#19
20. Offline

### messageofdeath

Spins into infinite loop, looking at loops now.

Second if statement, thats where the infinite loop happens

JBoss925
Changed second for loop to " for(double x = overx; x >= basex; x--) {"

Same result as before

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 9, 2016
21. Offline

Fixed it.

#21
22. Offline

### messageofdeath

JBoss925
for(double x = basex; x >= overx; x--) {

Doesn't work because it will never go into the loop.

because basex will always be lower than overx which in turn doesn't do anything.

23. Offline

### Dragonphase

JBoss925

I wasn't arguing, I was just wondering. When I said little effort where required, I meant I liked how you recognized how he only needed a solution for a flat surface, and you didn't give him more than what he asked for.

#23
24. Offline

### JBoss925

Yes it will? basex is greater than overx in that instance. Alright I'll add you.

#24