1

I need to work out the distance between each of the soldiers but I cant work out how to because they all have come from the same xPos and yPos and everything i find online is for x1, y1, x2, y2 sort of situations.

the idea is that they spawn randomly and then can move to attack eachother but i need to know the distance between them before i can go on

public class Soldier {

    double xPos;
    double yPos;

    public Soldier(/*double distance, double speed*/) {
        double lower = 0;
        double upper = 100; //setting the upper and lower limits for the soldiers
        xPos = Math.random() * (upper - lower) + lower;
        yPos = Math.random() * (upper - lower) + lower; //creating x and y values
        xPos = Math.round(xPos * 10) / 10.0d;
        yPos = Math.round(yPos * 10) / 10.0d; //making sure the x and y value is to 1dp
        System.out.println("(" + xPos + ", " + yPos + ")"); //printing the location
    }
}

and my main class is,

public class Main {                                   
   public static void main(String[] args) {           
       Soldier Cavalier = new Soldier(/*5.9, 1*/);    
       Soldier Pikeman = new Soldier(/*10.3, 12.6*/); 
       Soldier Crossbowman = new Soldier(/*4.9, 3*/); 
       System.out.println();                          
   }         
}     
ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
  • 5
    `everything i find online is for x1, y1, x2, y2 sort of situations` - your soldiers both have x and y, how comes this is not a `x1,y1,x2,y2` situation? – bezmax Nov 05 '15 at 15:52
  • 4
    Read this: http://stackoverflow.com/questions/929773/calculating-the-distance-between-two-points – Dean Meehan Nov 05 '15 at 15:53
  • Although the soldiers are initialized in the same way, since random numbers are involved they will be at different locations. Try generating a few soldiers and find distance using vector math. – Tony Ruth Nov 05 '15 at 15:53
  • Create the get methods of xPos and yPos.. it would then look like `pikeman.getX();` – Kevin Mee Nov 05 '15 at 15:55
  • This is an "x1, y1, x2, y2 sort of situation". x1 and y1 are the `xPos` and `yPos` of the first `Soldier`. x2 and y2 are the `xPos` and `yPos` of the second `Soldier`. – Bethany Louise Nov 05 '15 at 15:59

1 Answers1

3

Add a distanceTo method on a soldier class:

public class Soldier {
    ....

    public double distanceTo(Soldier other) {
        return Math.sqrt(Math.pow(other.xPos-this.xPos,2) + Math.pow(other.yPos-this.yPos,2));
    }
}

Then use it like:

   Soldier Cavalier = new Soldier();    
   Soldier Pikeman = new Soldier(); 
   System.out.println(Cavalier.distanceTo(Pikeman)); 

Added due to commentors' suggestion:

Just a mention, that Math.pow is a general implementation of power function able to calculate power of any kinds of arguments. Therefore, it is a lot slower than simple x*x type multiplication. The optimized version of this code would look like this:

    public double distanceTo(Soldier other) {
        double dx = other.xPos-this.xPos;
        double dy = other.yPos-this.yPos;
        return Math.sqrt(dx*dx + dy*dy);
    }

You can use whatever version is more readable for you if you need no performance optimizations.

bezmax
  • 25,562
  • 10
  • 53
  • 84
  • I would not use Math.pow to square a value. – AlexWien Nov 05 '15 at 16:00
  • Why that ? is it because it's too big for just a square ? – Asoub Nov 05 '15 at 16:04
  • @AlexWien In this specific case code readability is more important than micro-optimizations. And by "the case" I mean the fact that author is clearly a beginner Java programmer. – bezmax Nov 05 '15 at 16:05
  • no because it's a special advanced mathematic which is able to use double values as power, like x power 3.42; Which is much slower for integral values. There is not any graphics lib I know that uses Math.pow() to square. (see also Point2D.distance) – AlexWien Nov 05 '15 at 16:07
  • 1
    @Asoub `Math.pow` solves a general case of power function, that is, it is able to calculate 2.598^-92.389. For that task it uses complex mathematical formulas. While for this case a simple multiplication would be much faster. But once again, to make the answer more clear, I believe that sacrificing several nanoseconds for readability of the answer is ok. – bezmax Nov 05 '15 at 16:07
  • That is the reason that for distance calculations always dx*dx is used and not Math.pow(dx, 2); – AlexWien Nov 05 '15 at 16:08
  • @AlexWien I'll update my answer to mention this discussion in a moment. – bezmax Nov 05 '15 at 16:08
  • @bezmax distance = Math.sqrt(dx * dx + dy * dy); is much more readable – AlexWien Nov 05 '15 at 16:11
  • thanks for all your help guys –  Nov 05 '15 at 16:51