これがあなたのコードです。私はいくつかのマイナーな(?)変更を行い、さらにいくつかの新しいコードを追加して、ライン上の最長ポイントを取得しました
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
}
}