我有这个程序,我正在读取一行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坐标(我有那个工作)时,程序就会终止。
答案 0 :(得分: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
}
}
答案 1 :(得分:-1)
首先声明一个变量来保存到目前为止找到的最长行。说longestSoFar。将其设置为较小的数字,例如0,因为您不会得到任何短于0的行。在循环中,当您找到行的长度时,只需将其与longestSoFar进行比较。
if(lengthOfCurrentLine > longestSoFar){
longestSoFar = lengthOfCurrentLine;
}
当然,到目前为止,你将保留给出最长线的点数。
在循环结束时,存储在该变量中的是你的答案。