简体   繁体   中英

Trying to calculate the distance between the position of two objects

I have these two instance variables which are set within the constructor of the object to be:

    (int)(Math.random()*100);

The two instance variables are:

    private double xPos;
    private double yPos;

The class is called Soldier and I have three other classes that inherit from Soldier, (only the constructor at the moment)

The output I am getting at the moment is:

My name is: Cavalier I am at position: (45.0,56.0)

My name is: Crossbowman I am at position: (15.0,91.0)

My name is: Halberdier I am at position: (67.0,8.0)

I am trying to calculate the distance in the x and y positions of the objects

The method I am currently trying is:

    protected Soldier distanceBetween(Soldier x, Soldier y){
        double distBetween = (x.xPos - y.xPos)+(x.yPos-y.yPos);
        return this;

    }

The method I am trying to achieve is where two objects that inherit from Soldier are taken into the distBetween paremeters, for example I use the names: halberdier, cavalier and crossbowman.

When I call this method:

    cavalier.distanceBetween(cavalier,crossbowman);

I want it to calculate the distance between the x and y coordinates

How would I achieve this?

Completely wrong.

You need to return a value, not a Soldier.

This is more general:

public static double distanceBetween(double x1, double y1, double x2, double y2) {
    double dx = Math.abs(x2-x1);
    double dy = Math.abs(y2-y1); 
    if (dx > dy) {
        double r = dy/dx;
        return dx*Math.sqrt(1.0 + r*r); 
    } else {
        double r = dx/dy;
        return dy*Math.sqrt(1.0 + r*r); 
    }
}

The ^ operator is not exponentiation; it's XOR.

You can override this method this way:

public static double distanceBetween(Soldier s1, Solder s2) {
    return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
}

Since you are having trouble, I'll spell it out for you:

/**
 * Distance calc
 * User: mduffy
 * Date: 11/1/2015
 * Time: 9:58 AM
 * @link http://stackoverflow.com/questions/33462961/trying-to-calculate-the-distance-between-the-position-of-two-objects/33463222?noredirect=1#comment54712511_33463222
 */
public class Soldier {

    public final double xPos;
    public final double yPos;

    public static void main(String[] args) {
        Soldier s = new Soldier(0, 0);
        Cavalier c = new Cavalier(3, 4);
        System.out.println(String.format("distance: %f10.3", s.distanceBetween(c)));
    }

    public Soldier(double xPos, double yPos) {
        this.xPos = xPos;
        this.yPos = yPos;
    }

    public double distanceBetween(Soldier s) {
        return distanceBetween(this.xPos, this.yPos, s.xPos, s.yPos);
    }

    public static double distanceBetween(double x1, double y1, double x2, double y2) {
        double dx = Math.abs(x2-x1);
        double dy = Math.abs(y2-y1);
        if (dx > dy) {
            double r = dy/dx;
            return dx*Math.sqrt(1.0 + r*r);
        } else {
            double r = dx/dy;
            return dy*Math.sqrt(1.0 + r*r);
        }
    }

    public static double distanceBetween(Soldier s1, Soldier s2) {
        return distanceBetween(s1.xPos, s1.yPos, s2.xPos, s2.yPos);
    }
}

class Cavalier extends Soldier {
    public Cavalier(double x, double y) {
        super(x, y);
    }
}

If you want different distance calculation methods (eg Euclidian, Manhattan, Pearson, spherical, etc.) you can have an interface that lets you change it by adding a new implementation:

public interface DistanceCalculator {
    double distance(double x1, double y1, double x2, double y2);
}

Now you can easily switch by adding new code rather than modifying existing code.

You need something like this member function in your Soldier class.

public double ManhattanDistance (Soldier other)
{
    return Math.Abs(this.xPos - other.xPos) + Math.Abs(this.xPos - other.xPos);
}

If you want Cartesian -- crow-flies -- distance you need this member function.

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

To use these member functions to get the distance between your cavalier and your crossbowman you do this kind of thing.

 double howFar = cavalier.Distance(crossbowman);

or

 double howManyBlocks = crossbowman.ManhattanDistance(cavalier);

You have to imagine a right angle triangle ABC. You have the coordinates for A (45.0,56.0) and C(15.0,91.0). You have to get coordinate of B first which is holding the right angle.

Coordinate of B is

(45.0,91.0)

So AC will be :

Math.sqrt(Math.pow(91-56)+Math.pow(45-15))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM