0
votes

I want a program given points (any number of of points) to get the nearest point and the farthest point in a cartesian plane out of all points, and then calculate the distance of the nearest to the furthest.

I have the code bellow that finds the min x and min y, and max x and max y, but I want the two min to be pair and two max to be pair. I am not getting the correct answer on some testing data, but I am getting the correct answer on some testing data.

For simplicity I just consider positive x and y in the cartesian plane.

import java.lang.Math;
import java.util.Scanner;

class Point
{
    double x;
    double y;
}
class Compare
{
    Double maxx;
    Double maxy;
    Double minx;
    Double miny;

    public void maxx(double num)
    {
        if (maxx == null)
            maxx = num;
        else if(num > maxx)
            maxx = num;
    }
    public void maxy(double num)
    {
        if (maxy == null)
            maxy = num;
        else if(num > maxy)
            maxy = num;
    }
    public void minx(double num)
    {
        if (minx == null)
            minx = num;
        else if(num < minx)
            minx = num;
    }
    public void miny(double num)
    {
        if (miny == null)
            miny = num;
        else if(num < miny)
            miny = num;
    }
}
class Main
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        Compare compare = new Compare();

        int n = input.nextInt();

        if(n>=2 && n<=30)
        {
            for (int i = 0; i < n; i++) 
            {
                Point points = new Point();

                points.x = input.nextDouble();
                points.y = input.nextDouble();

                compare.minx(points.x);
                compare.miny(points.y);
                compare.maxx(points.x);
                compare.maxy(points.y);
            }
        }
        double dis;
        dis=Math.sqrt((compare.maxx-compare.minx)*(compare.maxx-compare.minx) + (compare.maxy-compare.miny)*(compare.maxy-compare.miny));
        System.out.printf("%.4f\n",dis);
    }
}
1
So what is your current result of the code? Why is it wrong? What are you expecting instead? - Ben
What do the "least furthest point" and "furthest point" terms mean? Distance from origin? - harold
yes from the origin - Dedicated User
distance between two point is not simple calculated based on x1 and x2 comparison. The length of line joining any two points is given by sqrt((x1=x2)^2 + (y1-y2)^2)...Also, you need to pass the x,y coordinates for both the points to the function to calculate distance between them, then you can keep storing value of point in closedDistance and farthestdistance . - akshaya pandey
If you want the nearest and furthest points it is incorrect to test x and y separately. - DavidW

1 Answers

1
votes

A use-case for Math.hypot.

When minimum and maximum are with respect to the origin (0, 0):

    Point min = null;
    double minDistance = Double.MAX_VALUE;
    Point max = null;
    double maxDistance = 0.0;
    for (int i = 0; i < n; i++) {
        Point point = new Point();

        point.x = input.nextDouble();
        point.y = input.nextDouble();

        double distanceToO = Math.hypot(point.x, point.y);
        if (min == null || minDistance > distanceToO) {
            minDistance = distanceToO;
            min = point;
        }
        if (max == null || maxDistance < distanceToO) {
            maxDistance = distanceToO;
            max = point;
         }
     }

When the minimum and maximum concern the distance between two read points:

    Point minFrom = null;
    Point minTo = null;
    double minDistance = Double.MAX_VALUE;

    Point maxFrom = null;
    Point maxTo = null;
    double maxDistance = 0.0;

    Point[] points = new Point[n];
    for (int i = 0; i < n; i++) {
        Point point = new Point();
        point.x = input.nextDouble();
        point.y = input.nextDouble();
        points[i] = point;
    }
    for (int i = 0; i < n; i++) {
        Point pointI = points[i];
        for (int j = i + 1; j < n; j++) {
            Point pointJ = points[j];
            double distance = Math.hypot(
                    pointJ.x - pointI.x,
                    pointJ.y - pointI.y);
            if (minFrom == null || minDistance > distance) {
                minDistance = distance;
                minFrom = pointI;
                minTo = pointJ;
            }
            if (maxFrom == null || maxDistance < distance) {
                maxDistance = distance;
                maxFrom = pointI;
                maxTo = pointJ;
            }
        }
    }