Java 2D: Moving point P a certain distance closer to another point?

What is the best way to move Point2D.Double x closer to another Point2D.Double?

Edit: Tried to edit, but so went for maintenance. No, this is not homework.

I need to move the plane (A) to the end of the runway (C) and point it in the right direction (angle a).

alt text http://img246.imageshack.us/img246/9707/planec.png

Here is what I have so far, but it seems messy, what is the usual way to do something like this?

//coordinate = plane coordinate (Point2D.Double) //Distance = max distance the plane can travel in this frame Triangle triangle = new Triangle(coordinate, new Coordinate(coordinate.x, landingCoordinate.y), landingCoordinate); double angle = 0; //Above to the left if (coordinate.x <= landingCoordinate.x && coordinate.y <= landingCoordinate.y) { angle = triangle.getAngleC(); coordinate.rotate(angle, distance); angle = (Math.PI-angle); } //Above to the right else if (coordinate.x >= landingCoordinate.x && coordinate.y <= landingCoordinate.y) { angle = triangle.getAngleC(); coordinate.rotate(Math.PI-angle, distance); angle = (Math.PI*1.5-angle); } plane.setAngle(angle); 

The triangular class can be found at http://pastebin.com/RtCB2kSZ

Whereas the plane can be in any position around the runway point

+6
java java-2d
source share
6 answers

Vectors to the rescue!

Data points A and B. Create a vector V from A to B (making BA). Normalize the vector V to a unit vector, and then just multiply it by the distance d, you want and finally add the resulting vector to point A. ie:

  A_moved = A + |(BA)|*d 

Java (MTF)

  Vector2D a_moved = a.add(b.subtract(a).norm().multiply(d)); 

No angles, no unpleasant trigger.

+5
source share

The shortest distance between two points is a line, so just move that x point of units along the line connecting the two points.


Edit: I didn’t want to give the specifics of the answer if this is homework, but it is simple enough to be illustrated without being too spoiler-y.

Suppose you have two points A = (x 1 , y 1 ) and B = (x 2 , y 2 ). A line including these two points has the equation

(x 1 , y 1 ) + t? (x 2 - x 1 , y 2 - y 1 )

where t is some parameter. Note that for t = 1 point indicated in the line is B , and for t = 0 point indicated in the line is A

Now you want to move B to B' , a point that is the new distance d from A :

  AB' B (+)---------------------(+)-----------(+) <========={ d }=========> 

Point B' , like any other point on the line, is also determined by the equation that we showed earlier. But what value do we use t ? Well, when t is 1, the equation points to B , which is |AB| units from A Thus, the value of t that B' indicates is t = d/|AB| .

Solution for | AB | and the inclusion of this in the above equation is left as an exercise for the reader.

+5
source share

You can minimize the difference along both axes by percent (which depend on how many points you want to move).

For example:

 Point2D.Double p1, p2; //p1 and p2 inits // you don't use abs value and use the still point as the first one of the subtraction double deltaX = p2.getX() - p1.getX(); double deltaY = p2.getY() - p1.getY(); // now you know how much far they are double coeff = 0.5; //this coefficient can be tweaked to decice how much near the two points will be after the update.. 0.5 = 50% of the previous distance p1.setLocation(p1.getX() + coeff*deltaX, p1.getY() + coeff*deltaY); 

So you moved p1 halfway to p2 . The good thing to avoid abs is that if you choose which point will be moved and which will stand still, you can avoid if the tests just use the raw coefficient.

+5
source share
 double angle = Math.atan2(landingCoordinate.y-coordinate.y, landingCoordinate.x-coordinate.x); coordinate.x += Math.cos(angle)*distance; coordinate.y += Math.sin(angle)*distance; //Add 90 degress to the plane angle plane.setAngle(angle + 1.57079633); 
+2
source share
 (point A is to be moved closer to B) if (Ax > Bx) //decrement Ax if (Ax < Bx) //increment Ax 

which may be the main idea in pseudo-code, but there is much more than that. How do you determine the "best" way?

Some things to consider:

  • Is there a specific metric / ratio that you want the dots to be separated from each other, or do you just want them to be closer?
  • Should both coordinates be changed? (for example, if you want to move (1.50) closer to (0.0), are you doing this (.5, 25) or just (1.25)?
  • if the point should be at a distance of 1 unit, should it be 1 unit horizontally? right diagonally? 1 unit on the line between two points?

Give us a little more details and we will see what we have .: D

0
source share

In this game, integral coordinates are used to represent the squares in the grid. The move(int row, int col) method moves to the specified row and column, advancing one square in one of eight semi-single directions, as shown here .

0
source share

All Articles