简体   繁体   中英

Calculate distance between two points in java

I'm kind of new to Java, and trying to write a code that calculate the distance of two points 2 and 3, and scale of 10. Somehow, it does not work. Can you give me a hint, so I can fix the code?

import java.lang.Math;

public class Point {
    int x, y;

    public Point (int x, int y){
        this.x = x;
        this.y = y;
    }
    public float scale(int factor) {
        new Point(x * factor, y * factor);
        return factor;
    }
    public float distance(){
        double distance = Math.sqrt(x * x + y * y);
        return distance;
    }
    public void main(String[] args) {
        float p = new Point(2,3).scale(10);
        System.out.println(distance);
    }

    }

In scale you are creating a new point with the scaled values and doing nothing with it. You're leaving x and y of the point in question untouched.

You probably mean to multiply x and y by factor, rather than creating a new point.

Also you're printing a variable named distance, which does not exist (so this probably doesnt even compile), rather than calling the method named distance() and printing its returned value.

public class Point {
    int x, y;

    public Point (int x, int y){
        this.x = x;
        this.y = y;
    }

    public static Point scalePoint(Point p, int factor) {           //scale a given point p by a given factor 
        Point scaledPoint = new Point(p.x * factor, p.y * factor);  //by multipling the x and y value with the factor
        return scaledPoint;                                         //and return the new scaled point
    }

    public static double calculateDistance(Point p1, Point p2){ //to calculate the distance between two points 
        double distance = Math.sqrt(p1.x * p2.x + p1.y * p2.y); //send the two points as parameter to this method
        return distance;                                        //and return the distance between this two as a double value
    }

    public static void main(String[] args) {
        Point p = new Point(2,3);
        Point scaledPoint = scalePoint(p, 10);
        double distance = calculateDistance(p, scaledPoint);
        System.out.println(distance);
    }
}

At the moment your distance method is calculating the distance of a point from the origin (ie point 0,0). It would make more sense if you made that explicit:

class Point {
    private static final Point ORIGIN = new Point(0, 0);
    private final int x;
    private final int y;

    public float distanceTo(Point other) {
        float xDelta = other.x - this.x;
        float yDelta = other.y - this.y;
        return Math.sqrt(xDelta * xDelta + yDelta * yDelta);
    }

    public Point scale(float factor) {
        return new Point(x * factor, y * factor);
    }
}

Then finding the distance to the origin becomes point.distanceTo(Point.ORIGIN) which makes the intent clearer.

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