0

我有这个程序,我正在读取一行 x 和 y 坐标,并且必须找到最长的共线点线。我正在读取它们并将所有 xCoordinates 存储在 ArrayList 中,并将所有 yCoordinates 存储在另一个列表中。我需要比较斜率并打印出最长的线。我计算了斜率,但不知道如何将它们相互比较。这是我到目前为止所拥有的:

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        ArrayList<Double> list = new ArrayList<Double>();
        ArrayList<Double> xCoord = new ArrayList<Double>();
        ArrayList<Double> yCoord = new ArrayList<Double>();
        //      double xCoord[] = null;
        //      double yCoord[] = null;
        int i = 0;
        int count = 0; 
        double slope = 0;
        double slope2 = 0;


        while(scanner.hasNext()) {
            //grabs x and y coordinate
            xCoord.add(scanner.nextDouble());
            yCoord.add(scanner.nextDouble());

            //formatting
            if(i == 0) {
                System.out.println(" X  Y");
                System.out.println(" -  -");
            }
            System.out.print(xCoord.get(i) + " ");
            System.out.print(yCoord.get(i));
            System.out.print("\n");


            //ending case
            if((i > 0) && (xCoord.get(i).equals(xCoord.get(i-1)) && yCoord.get(i).equals(yCoord.get(i-1)))) {
                System.out.println("Hey, they matched");
                break;
            }


            if(i > 0) {
                slope = (xCoord.get(i-1) - xCoord.get(i))/(yCoord.get(i-1) - yCoord.get(i));
                System.out.println("Slope: " + slope);
            }


            i+=1;
        }




    }

}

当您获得两个相同的 x,y 坐标时,程序终止(我有这个工作)。

4

2 回答 2

0

这是你的代码,我做了一些小的(?)修改,还添加了一些新代码来获得一条线上的最长点

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class CollinearPoints {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        List<Coordinate> coordinates = new ArrayList<Coordinate>();
        List<Slope> slopes = new ArrayList<Slope>();
        int i = 0;
        while (scanner.hasNext()) {
            // grabs x and y coordinate
            Coordinate coordinate = new Coordinate(scanner.nextDouble(), scanner.nextDouble());

            // formatting
            if (i == 0) {
                System.out.println(" X  Y");
                System.out.println(" -  -");
            }
            System.out.print(coordinate.getX() + " ");
            System.out.print(coordinate.getY());
            System.out.print("\n");

            // ending case
            if ((i > 0)
                    && (coordinate.getX() == coordinates.get(i-1).getX() && 
                            coordinate.getY() == coordinates.get(i-1).getY())) {
                System.out.println("Hey, they matched");
                break;
            }
            coordinates.add(coordinate);
            i += 1;
        }
        /* Calculate slopes between all the points */
        for(i =0; i < coordinates.size(); i++){
            for(int j = i+1; j < coordinates.size(); j++){
                Coordinate coordinate1 = coordinates.get(i);
                Coordinate coordinate2 = coordinates.get(j);
                double slope = getSlope(coordinate1, coordinate2);
                slopes.add(new Slope(coordinate1, coordinate2, slope));
            }
        }
        /* Calculate slope counts (to know which one occurs max times) */
        Map<String, Double> slopeCountsMap = new HashMap<String, Double>();
        for(Slope slope : slopes){
            slopeCountsMap.put(String.valueOf(slope.getSlope()), Double.valueOf(String.valueOf(getMatchingSlopeCount(slope.getSlope(), slopes))));
        }
        /* Sort the slope :: slope-count map */
        ValueComparator bvc =  new ValueComparator(slopeCountsMap);
        TreeMap<String,Double> sorted_map = new TreeMap<String,Double>(bvc);
        sorted_map.putAll(slopeCountsMap);

        /* get the maximum occurring slope */
        double maxSlope = Double.parseDouble(sorted_map.firstKey());

        /* Collect the list of co-ordinates having the max occurring slope and which are collinear */
        List<Coordinate> colinearPoints = new ArrayList<Coordinate>();
        for(Slope slope : slopes){
            if(maxSlope == slope.getSlope()){
                if(colinearPoints.size() < 2){
                    if(!colinearPoints.contains(slope.getCoordinate1())){
                        colinearPoints.add(slope.getCoordinate1());
                    }
                    if(!colinearPoints.contains(slope.getCoordinate2())){
                        colinearPoints.add(slope.getCoordinate2());
                    }
                } else {
                    if(colinearPoints.contains(slope.getCoordinate1()) &&
                            !colinearPoints.contains(slope.getCoordinate2())){
                        colinearPoints.add(slope.getCoordinate2());
                    } else if(colinearPoints.contains(slope.getCoordinate2()) &&
                            !colinearPoints.contains(slope.getCoordinate1())){
                        colinearPoints.add(slope.getCoordinate1());
                    }
                }
            }
        }
        System.out.println("Colinear points: ");
        System.out.println(colinearPoints);
    }

    private static int getMatchingSlopeCount(double slope, List<Slope> slopes){
        int count = 0;
        for(Slope slope2 : slopes){
            if(slope == slope2.getSlope()){
                count ++;
            }
        }
        return count;
    }
    /* Calculate slope between two points */
    private static double getSlope(Coordinate coordinate1, Coordinate coordinate2){
        return ((coordinate2.getY() - coordinate1.getY()) / (coordinate2.getX() - coordinate1.getX()));
    }
}
/* Class to hold co-ordinates */
class Coordinate {
    private double x;
    private double y;

    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
    public double getX() {
        return x;
    }
    public void setX(double x) {
        this.x = x;
    }
    public double getY() {
        return y;
    }
    public void setY(double y) {
        this.y = y;
    }
    public Coordinate() {
    }
    public Coordinate(double x, double y) {
        super();
        this.x = x;
        this.y = y;
    }
    @Override
    public boolean equals(Object o) {
        return (((Coordinate)o).getX() == this.getX() && ((Coordinate)o).getY() == this.getY());
    }
}
/* Class to hold slope between two points */
class Slope {
    private Coordinate coordinate1;
    private Coordinate coordinate2;
    private double slope;
    @Override
    public String toString() {
        return "(" + coordinate1.getX() + "," + coordinate1.getY() + ")(" + coordinate2.getX() + "," + coordinate2.getY() + ")";
    }
    public double getSlope() {
        return slope;
    }
    public void setSlope(double slope) {
        this.slope = slope;
    }
    public Coordinate getCoordinate1() {
        return coordinate1;
    }
    public void setCoordinate1(Coordinate coordinate1) {
        this.coordinate1 = coordinate1;
    }
    public Coordinate getCoordinate2() {
        return coordinate2;
    }
    public void setCoordinate2(Coordinate coordinate2) {
        this.coordinate2 = coordinate2;
    }
    public Slope() {
    }
    public Slope(Coordinate coordinate1, Coordinate coordinate2, double slope) {
        super();
        this.coordinate1 = coordinate1;
        this.coordinate2 = coordinate2;
        this.slope = slope;
    }
}
/* Comparator to sort the map */
class ValueComparator implements Comparator<String> {
    Map<String, Double> base;
    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }
    // Note: this comparator imposes orderings that are inconsistent with equals.    
    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        } // returning 0 would merge keys
    }
}
于 2013-02-15T07:38:37.117 回答
-1

首先声明一个变量来保存迄今为止发现的最长行。说最长到目前为止。将此设置为一个较小的数字,例如 0,因为您不会得到任何短于 0 的行。在循环中,当您找到行的长度时,只需将其与 longSoFar 进行比较。

  if(lengthOfCurrentLine > longestSoFar){
         longestSoFar = lengthOfCurrentLine;
  }

当然,您将保留迄今为止给出最长线的点。

在循环结束时,该变量中存储的任何内容都是您的答案。

于 2013-02-15T02:39:23.583 回答