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座標を2つ取得すると、プログラムは終了します(私はそれを機能させています)。

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

これまでに見つかった最長の行を保持する変数を宣言することから始めます。longestSoFarと言います。0より短い行は取得されないため、これを0などの小さな数値に設定します。ループ内で行の長さを見つけたら、それをlongestSoFarと比較します。

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

そしてもちろん、これまでで最も長いラインを与えたポイントを維持します。

ループの終わりに、その変数に格納されているものはすべてあなたの答えです。

于 2013-02-15T02:39:23.583 に答える