2014-06-12 20 views
-2

對於我的計算機類科學類中的最後一個項目,我需要創建一個具有2個類的項目,並讀取帶有矩形(x,y,寬度,高度)座標的CSV一個數組列表並打印距離它們的拐角(x,y)最小距離的2個矩形我已經設法打印出最小的矩形,但我現在不知道如何打印第二小矩形。來自一個arraylist的最小的2個值

這裏是我的代碼

public class Week09 { 
    public static void main(String[] args)throws IOException { 
     String theFile; 
     theFile = getTheFileName(); 
     ArrayList<Rectangle> arrayRectangle; 
     arrayRectangle = getArraylist(theFile); 
     displaySameArea(arrayRectangle,"Rectangles with same area: "); 
     displaysmallDist(arrayRectangle,"Recangles with smallest distance: "); 
    } 
    public static String getTheFileName(){ 
     String theFile; 
     JFileChooser jfc = new JFileChooser(); 
     jfc.showOpenDialog(null); 
     return theFile = jfc.getSelectedFile().getAbsolutePath(); 
    } 
    public static ArrayList<Rectangle> getArraylist(String s) throws IOException { 
     ArrayList <Rectangle> arrayRectangle = new ArrayList <Rectangle>(); 
     FileReader fr = new FileReader(s); 
     BufferedReader br = new BufferedReader(fr); 
     int x = 0; 
     int y = 0; 
     int width = 0; 
     int height = 0; 
     String aLine; 
     String arrayLine = "[,]"; 
     try{ 
     while ((aLine=br.readLine()) != null){ 
      String[] a = aLine.split(arrayLine); 
      x = Integer.parseInt(a[0]); 
      y = Integer.parseInt(a[1]); 
      width = Integer.parseInt(a[2]); 
      height = Integer.parseInt(a[3]); 
      Rectangle b = new Rectangle(x,y,width,height); 
      arrayRectangle.add(b); 
      } 
     } 
     catch (IOException e) {    
      e.printStackTrace(); 
     } 
     return arrayRectangle; 
    } 
public static void displaysmallDist(ArrayList<Rectangle> arrayRectangle, String s) { 
    int x = 0; 
    int y = 0; 
    int width = 0; 
    int height = 0; 
    int count1 = 0; 
    int count2 = 1; 
    Rectangle c = new Rectangle (x,y,width,height); 
    Rectangle d = new Rectangle (x,y,width,height); 
    double lowestDistance = Double.MAX_VALUE; 

    for(int n = 0; n < arrayRectangle.size(); n++) { 
     c = arrayRectangle.get(n); 

     for(int j = n+1; j < arrayRectangle.size(); j++) { 
      double nextDistance; 
      d = arrayRectangle.get(j); 
      nextDistance = c.distance(d); 
      if (nextDistance < lowestDistance) 
      { 
       lowestDistance = nextDistance; 
       count1 = n; 
       count2 = j;   
      }                
     } 
    } 
    System.out.print(s + arrayRectangle.get(count1).toString() + arrayRectangle.get(count2).toString() + "\n"); 
} 

}

這裏是我的第二類

public class Rectangle { 
private int x; 
private int y; 
private int width; 
private int height; 

public Rectangle(int x,int y,int width,int height){ 
    this.x = x; 
    this.y = y; 
    this.width = width; 
    this.height = height;  
} 
public Rectangle(Rectangle a) { 
    x = a.x; 
    y = a.y; 
    width = a.width; 
    height = a.height; 
} 
public void setX (int x){ 
    this.x = x; 
} 
public int getX(){ 
    return x; 
} 
public void setY (int y){ 
    this.y = y; 
} 
public int getY(){ 
    return y; 
} 
public void setWidth (int width){ 
    this.width = width; 
} 
public int getWidth(){ 
    return width; 
} 
public void setHeight (int height){ 
    this.height = height; 
} 
public int getHeight(){ 
    return height; 
} 
public double getArea(){ 
    return this.width * this.height; 

} 
public double distance(Rectangle y){ 
    return Math.sqrt((this.x - y.x)*(this.x - y.x) + (this.y - y.y)*(this.y - y.y)); 
} 
public boolean equals(Rectangle a){ 
    if (this.width * this.height != a.width * a.height) return false; 
    return true; 
} 
public String toString(){ 
    return "Rectangle corner at ("+ x +"," + y + ") Width = " + width + " Height = " + height + " "; 
} 

}

+0

是否有可能使/實現您的Rectangle類可比較和使用自動比較和排序對象的TreeSet?看看http://www.programcreek.com/2009/02/implement-comparable-for-a-treeset/ –

+0

爲了澄清,你想要打印的數組中的兩個矩形的起點(x,y)是彼此最近?或者誰有最接近的角落? – Jason

+0

(x,y)是左上角的座標,它是CSV(x,y,寬度,高度)中的x和y。 –

回答

0

所以基本上你需要去通過列表,發現最小一個仍然是> lowestDistance或者,在相同的for-loop臨時你計算距離,保存2個距離,一個是最小的,一個是最小的平方(在將值設置爲最小值之前)。

令人困惑,我知道。

像這樣:假設你有5個值5,4,3,2的列表,和1

爲了讓你去一次槽列表中最小的,正確的。對於每一個號碼,你做一個

if (min > currentNumber) then min=currentNumber, 

現在想象一下,你有2個變量:min1 and min2

if (min1 > currentNumber) then { 
    min2 = min1; 
    min1 = currentNumber; 
} 

最後,MIN1將是 「1」 和MIN2將是 「2」。

0
Rectangle firstRectangle = null, secondRectangle = null; 
double lowestDistance = -1.0; 
for(int index1 = 0; index1 < arrayRectangle.size() - 1; index1++) { 
    Rectangle r1 = arrayRectangle.get(index1); 
    for(int index2 = index1 + 1; index2 < arrayRectangle.size(); index2++) { 
     Rectangle r2 = arrayRectangle.get(index2); 
     if(lowestDistance == -1 || r1.distance(r2) < lowestDistance) { 
      firstRectangle = r1; 
      secondRectangle = r2; 
      lowestDistance = r1.distance(r2); 
    } 
} 

當這個代碼執行,兩個矩形與最接近的起源將在firstRectanglesecondRectangle

編輯:因爲這是一個循環內的循環,我已經優化它,以便每對矩形只檢查一次。

0

好吧,我不知道你問什麼,但在這裏是要找到2最接近(X,Y)點的矩形方式:

ArrayList<Rectangle> rects = new ArrayList<Rectangle>(); 
    double minDistance = Double.MAX_VALUE; 
    Rectangle r1; 
    Rectangle r2; 
    for(Rectangle rect1 : rects){ 
     for(Rectangle rect2 : rects){ 
      if(!rect1.equals(rect2)){ 
       if(distance(rect1.x, rect1.y, rect2.x, rect2.y)<minDistance){ 
        minDistance = distance(rect1.x, rect1.y, rect2.x, rect2.y); 
        r1 = rect1; 
        r2 = rect2; 
       } 
      } 
     } 
    } 

    public double distance(double x1, double y1, double x2, double y2){ 
     return Math.pow(Math.pow(x2-x1, 2)+Math.pow(y2-y1, 2), 0.5); 
    } 

注意:如果你想提高效率,然後像以前一樣做:

for(int n = 0; n < rects.size(); n++){ 
     Rectangle rect1 = rects.get(n); 
     for(int m = n +1; m < rects.size(); m++){ 
      Rectangle rect2 = rects.get(m); 
     } 
    } 
0

我想你看起來像這樣。這沒有經過測試。相應地改變。 「打印」矩形你的方式:)

package com; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 

public class Main { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     List<Rectangle> collRectangles = new ArrayList<Rectangle>(); 
     //Please check co-ordinates (if this form co-ordinate these are random values 
     collRectangles.add(new Rectangle(new Point(0,0), new Point(0,1), new Point(1,1), new Point(1,0))); 
     collRectangles.add(new Rectangle(new Point(2,2), new Point(2,4), new Point(6,4), new Point(6,2))); 
     collRectangles.add(new Rectangle(new Point(3,3), new Point(3,5), new Point(8,5), new Point(8,3))); 
     Collections.sort(collRectangles); 
     Rectangle smallestRectangle = collRectangles.get(collRectangles.size()-1); 
     Rectangle secondSmallestRectangle = collRectangles.get(collRectangles.size()-2); 
    } 
} 

class Rectangle implements Comparable<Rectangle> { 
    private Point pointA; 
    private Point pointB; 
    private Point pointC; 
    private Point pointD; 


    public Rectangle(){ 

    } 

public Rectangle(Point pointA, Point pointB, Point pointC, Point pointD) { 
     super(); 
     this.pointA = pointA; 
     this.pointB = pointB; 
     this.pointC = pointC; 
     this.pointD = pointD; 
    } 

public Point getPointA() { 
     return pointA; 
    } 

    public void setPointA(Point pointA) { 
     this.pointA = pointA; 
    } 

    public Point getPointB() { 
     return pointB; 
    } 

    public void setPointB(Point pointB) { 
     this.pointB = pointB; 
    } 

    public Point getPointC() { 
     return pointC; 
    } 

    public void setPointC(Point pointC) { 
     this.pointC = pointC; 
    } 

    public Point getPointD() { 
     return pointD; 
    } 

    public void setPointD(Point pointD) { 
     this.pointD = pointD; 
    } 

    public int compareTo(Rectangle rect){  
     //compare diagonals 
     return (int) Math.round(Utility.findDistanceBetweenPoints(this.pointA, this.pointC) 
              - Utility.findDistanceBetweenPoints(rect.pointA, rect.pointC)); 
    } 



} 

class Point { 
    public double coordinateX; 
    public double coordinateY; 


    public Point(double coordinateX, double coordinateY) { 
     super(); 
     this.coordinateX = coordinateX; 
     this.coordinateY = coordinateY; 
    } 
    public double getCoordinateX() { 
     return coordinateX; 
    } 
    public void setCoordinateX(double coordinateX) { 
     this.coordinateX = coordinateX; 
    } 
    public double getCoordinateY() { 
     return coordinateY; 
    } 
    public void setCoordinateY(double coordinateY) { 
     this.coordinateY = coordinateY; 
    } 



} 
class Utility { 

    public static double findDistanceBetweenPoints(Point p1, Point p2){ 
     double yCoordinateDifference = p1.getCoordinateY() - p2.getCoordinateY(); 
     double xCoordinateDifference = p1.getCoordinateX() - p2.getCoordinateX(); 
     return Math.sqrt((yCoordinateDifference*yCoordinateDifference) + (xCoordinateDifference*xCoordinateDifference)); 
    } 
}