I'm implementing NxN puzzels in Java 2D array int[][] state. am required to use the Manhattan heuristic in the following way:
the sum of the vertical and horizontal distances from
the current node to the goal node/tile
+(plus)
the number of moves to reach the goal node from the initial position
At the moment I don't know how to go further. Am a beginner in puzzle game programming with 2D arrays so am having trouble to understand certain concepts. How can I write this code in Java?
This is more a math question, but anyways the Manhattan distance is the sum of the absolute values of the horizontal and the vertical distance
int distance = Math.abs(x1-x0) + Math.abs(y1-y0);
More info: http://en.wikipedia.org/wiki/Taxicab_geometry
Related
I'm working on a basic program where 2 players on a 2D board of size 30x30 have to reach a smaller grid of size A*A (let's call it rug) somewhere on the 2D board. I need to calculate which of the players is closer to the rug. I know how to calculate the distance to the top left corner of the rug (as i use x and y coordinates to draw it), but don't know how to calculate the distance to the nearest point of the rug, which could also e.g be the middle of the bottom of the rug.
This is more of an algorithms question than java. Could you clarify the information we have about the rug? Do we have the rug's coordinates? Do we have the player's coordinates?
Assuming you had the coordinates of the corners of a square A*A rug, you could simply compute which player has a shorter Euclidean distance to the rug corners, or its sides. This can be done by creating a list of coordinates the rug owns.
Bear in mind this is psuedo code, please do not just copy the code into your IDE and be upset it didn't pass your tests.
So again the steps are:
Create a list of square rug's coordinates
Add corners to that list
Compute the points along the sides of the rug to be added to the list
Compare the minimum euclidean distance between the players and ALL coordinates in the list. The lower distance is closest.
Let's suppose you had a unit square rug with corners at (1,1), (-1,1), (-1,-1), (1,-1).
ArrayList<String> rugCoordinates = new ArrayList<String>();
//first add all corners
rugCoordinates.add("(1,1)");
rugCoordinates.add("(-1,1)");
rugCoordinates.add("(-1,-1)");
rugCoordinates.add("(1,-1)");
Now you need to add the points along the line to the list. You can get the slope between the points via rise/run for example, between (-1,1) and (1,1) = 0 slope.
slope = (yCoordTarget-yCoordSource)/(xCoordTarget-xCoordSource);
for(int i = xCoordSource; i < xCoordTarget, i++){
rugCoordinates.add(i, i+slope);
}
Finally compare the min distances of players.
if (findMinEuclideanDistance(player1) < findMinEuclideanDistance(player2)){
return player1;
else{
return player2;
}
I was going through one of the online courses for Data strudcture and Algorithms. There I saw saw a topic "Delta based two dimensional array search". Which describes below:
A techinque to search adjoing array elements of four directions in coordinates of two directions.
Delta value is an array that saves coordinates of four direction in coordinates as well as difference between X and Y.
Delta value is employed to approach the elements located up down left right side of certain element.
As per m understanding, its basically searching neighbors in four directions of a given array. I found couple of good answers on stack overflow:
Finding the neighbors of 2D array
Finding 8 neighbours in 2d Array
Finding a neighbour in a 2d array
More efficient way to check neighbours in a two-dimensional array in Java
Finding neighbours in a two-dimensional array
However I am not clear with the 2nd point "Delta value is an array that saves coordinates of four direction in coordinates as well as difference between X and Y". Can anyone explain what does this point means? Also the stack overflow links that I mentioned above serve the purpose? Thank You
I am making my own implementation of a raycaster in a game I am making, and I have come across a very hard problem. I have a player (the black dot), and I need to find the intersection nearest to the player. In the image below, the arrow is pointing to the intersection point I need.
What I guess I am trying to say is that I need a function something like this:
// Each line would take in 2 map values for it's 2 points
// In turn, the map would have to have an even number of points
public Point getNearestIntersection(int playerX, int playerY, int lineDir, Point[] map) {
// whatever goes here
}
I am going to have to do this about 50 times every frame, with about 100 lines. I would like to get 40 fps at the least if possible... Even if I divide it up into threads I still feel that it would cause a lot of lag.
The class Point has a method called distance which calculates the distance of two points. You then could loop all points to get the nearest. Could be something like this:
Point currentNearestIntersection;
double smallestDistance;
for (Point inter : intersections) {
double distance = player.distance(inter );
if (distance < smallestDistance) {
currentNearestIntersection= inter;
smallestDistance = distance;
}
}
axis/line intersection is in reality solving:
p(t)=p0+dp*t
q(u)=q0+(q1-q0)*u
p(t)=q(u)
t=? u=?
where:
p0 is your ray start point (vector)
dp is ray direction (vector)
q0,q1 are line endpoints (vectors)
p(t),q(u) are points on axis,line
t,u are line parameters (scalars)
This is simple system of 2 linear equations (but in vectors) so it lead to N solutions where N is the dimensionality of the problem so choose the one that is not division by zero ... Valid result is if:
t>=0 and u=<0.0,1.0>
if you use unit dp vector for direction of your ray then from computing intersection between axis and line the t parameter is directly distance from the ray start point. So you can directly use that ...
if you need to speed up the intersections computation see
brute force line/line intersection with area subdivision
And instead of remebering all intersections store always the one with smallest but non negative t ...
[Notes]
if you got some lines as a grid then you can compute that even faster exploiting DDA algorithm and use real line/line intersection only for the iregular rest... nice example of this is Wolfenstein pseudo 3D raycaster problem like this
I need to find the minimum distance b/w two kdtree bounding box's of same tree in euclidean space. Suppose each box maintain a 5 elements. I need the minimum Euclidean distance using java.
double QHRect[][] = QNode.m_NodesRectBounds;
double RHRect[][] = RNode.m_NodesRectBounds;
QHRect[][]: 5.74842E-4,7.76626E-5,6.72655E-4,
0.5002329025,0.2499048942,0.25046735625
RHRect[][]:
0.75006193275,0.7495593574,0.75005675875,
0.999890963,0.999386589,0.99985146
There is nothing tricky about a Java implementation compared with any other language in this matter. You need to know the general algorithm to handle a problem like this. I believe it is this:
Enumerate all 12 vertices of the two bounding boxes (cubes).
Enumerate all 12 faces of the two bounding boxes.
Find the Euclidean distance between each vertex of one and face of the other. This is similar to the shortest distance between a point and a plane.
Of these 2*6*6=72 combinations, pick the smallest and you have your answer.
In the general version of the problem, you would also have to check for if the two bounding boxes intersect as well.
currently i have using a framework and it has a function called distance2D, and it has this description:
Calculate the Euclidean distance
between two points (considering a
point as a vector object). Disregards
the Z component of the vectors and is
thus a little faster.
and this is how i use it
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) > 1)
System.out.println("Near");
i have totally no idea what a Euclidean distance is, i am thinking that it can be used to calculate how far 2 points are? because i am trying to compare distance between 2 objects and if they are near within a certain range i want to do something. how would i be able to use this?
Euclidean distance is the distance between 2 points as if you were using a ruler. I don't know what are the dimensions of your Euclidean space, but be careful because the function you are using just takes in consideration the first two dimensions (x,y). Thus if you have a space with 3 dimensions(x,y,z) it will only use the first two(x,y of x,y,z) to calculate the distance. This may give a wrong result.
For what I understood, if you want to trigger some action when two points are within some range you should make:
<!-- language: lang-java -->
if(g.getCenterPointGlobal().distance2D(target.getCenterPointGlobal()) < RANGE)
System.out.println("Near");
The Euclidean distance is calculated tracing a straight line between two points and measuring as the hypotenuse of a imaginary isosceles triangle between the two lines and a complementary point. This measure is scalar, so it's a good metric for your calculations.
Euclidean geometry is a coordinate system in which space is flat, not curved. You don't need to care about non-Euclidean geometry unless for example you're dealing with coordinates mapped onto a sphere, such as calculating the shortest travel distance between two places on Earth.
I imagine this function will basically use Pythagoras' theorem to calculate the distance between the two objects. However, as the description says, it disregards the Z component. In otherwords, it will only give a correct answer if both points have the same Z value (aka "depth").
If you wish to compare distances and save time, use not the distance itself, but its square: (x1-x2)^2 + (y1-y2)^2. Don't take sqrt. So, your distances will work exactly as euclidian ones, but quickly.