2013-02-15 68 views
0

我有這個程序,我正在讀一行x和y座標,並且必須找到最長的共線點。我正在閱讀它們,並將所有xCoordinates存儲在ArrayList中,並將所有yCoordinate存儲在另一個列表中。我需要比較斜坡並打印出最長的線。我計算了斜率,但無法弄清楚如何將它們相互比較。這是我到目前爲止:在Java中匹配兩個座標

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

線路最長最長的點?使用距離公式來計算行的長度 – Geros 2013-02-15 02:29:12

+0

您的意思是d = sqrt((x2-x1)^ 2 +(y2-y1)^ 2)? 如何比較環路中的兩個距離? – 2013-02-15 02:29:42

+0

您是否試圖找到由給定x和y內的任何共線點形成的最長線?你是?因爲你顯示的代碼不會那樣做! – Shivam 2013-02-15 02:50:28

回答

-1

首先聲明一個變量來保存到目前爲止找到的最長的行。說最長的時間。將它設置爲一個小數字,比如0,因爲你不會得到任何小於0的行。在循環中,當你找到一行的長度時,只需要將它與longestSoFar進行比較。

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

當然,你能不能讓這給了最長行迄今點。

在循環的結尾處,無論存儲在該變量中,都是您的答案。

+0

不是答案。他問他如何使用斜率值來找出最長的線。 – Shivam 2013-02-15 02:55:48

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 
    } 
}