2014-09-23 128 views
-1

我有一個名爲SimplePolygon的類,它創建一個由用戶提供座標的多邊形。我正在嘗試定義一種計算多邊形面積的方法。這是一項任務,課程講師希望我們使用以下公式來計算面積。Java中多邊形的計算區域

Formula for polygon area

我可以使用任一式。我選擇了正確的一個。

我的代碼給了我錯誤的地方。我不知道什麼是錯的。

public class SimplePolygon implements Polygon { 

    protected int n; // number of vertices of the polygon 
    protected Point2D.Double[] vertices; // vertices[0..n-1] around the polygon 

    public double area() throws NonSimplePolygonException { 
    try 
    { 
     if(isSimple()==false) 
      throw new NonSimplePolygonException(); 
     else 
     { 
      double sum = 0; 
      for(int i = 0; i < vertices.length - 1; i++) 
       if(i == 0) 
        sum += vertices[i].x * (vertices[i+1].y - vertices[vertices.length - 1].y); 
       else 
        sum += vertices[i].x * (vertices[i+1].y - vertices[i-1].y); 

      double area = 0.5 * Math.abs(sum); 
      return area; 
     } 
    } 

    catch(NonSimplePolygonException e) 
    { 
     System.out.println("The Polygon is not simple."); 
    } 

    return 0.0; 

} 

以下是測試儀代碼。多邊形與區域2的長方形,但輸出爲2.5

Point2D.Double a = new Point2D.Double(1,1); 
    Point2D.Double b = new Point2D.Double(3,1); 
    Point2D.Double c = new Point2D.Double(3,2); 
    Point2D.Double d = new Point2D.Double(1,2); 

    SimplePolygon poly = new SimplePolygon(4); 
    poly.vertices[0] = a; 
    poly.vertices[1] = b; 
    poly.vertices[2] = c; 
    poly.vertices[3] = d; 

    System.out.println(poly.area()); 
+0

請向我們提供的樣本數據和預期的輸出。 – Compass 2014-09-23 05:25:52

+0

'isSimple()'方法來自哪裏? – 2014-09-23 05:28:41

回答

3

現在你已經固定的瑣碎邊界的情況下,你錯過了另一個邊界和你的循環是錯誤的。更正後的代碼與調試:

public double area() 
    { 
    double sum = 0; 
    for (int i = 0; i < vertices.length ; i++) 
    { 
     if (i == 0) 
     { 
     System.out.println(vertices[i].x + "x" + (vertices[i + 1].y + "-" + vertices[vertices.length - 1].y)); 
     sum += vertices[i].x * (vertices[i + 1].y - vertices[vertices.length - 1].y); 
     } 
     else if (i == vertices.length - 1) 
     { 
     System.out.println(vertices[i].x + "x" + (vertices[0].y + "-" + vertices[i - 1].y)); 
     sum += vertices[i].x * (vertices[0].y - vertices[i - 1].y); 
     } 
     else 
     { 
     System.out.println(vertices[i].x + "x" + (vertices[i + 1].y + "-" + vertices[i - 1].y)); 
     sum += vertices[i].x * (vertices[i + 1].y - vertices[i - 1].y); 
     } 
    } 

    double area = 0.5 * Math.abs(sum); 
    return area; 

    } 
+0

你說得對。但我在4頂點多邊形上使用了該方法。所以在這種情況下,3與(vertices.length - 1)相同。 – 2014-09-23 05:34:20

+0

我想說@亨利的答案然後 - 你似乎錯過了另一個邊緣情況的術語。 – 2014-09-23 05:38:01

+0

@Fedora - 你的循環也是錯誤的。我們陷入了複雜而錯過了簡單。 – 2014-09-23 05:45:28

1

有從和一個缺失的條件:vertices[n-1].x * (vertices[0].y - vertices[n-2].y)

的問題進行編輯之前也有與第一項問題:

而且,如果我== 0期限爲vertices[i].x * (vertices[i+1].y - vertices[n-1].y)

假設n等於vertices.length

編寫循環的最簡單的方法可能是:

n = vertices.length; 
sum =0; 
for (int i = 0; i < n; i++) { 
    sum += vertices[i].x * (vertices[(i + 1) % n].y - vertices[(i + n - 1) % n].y); 
} 
+0

你是從哪裏得到的:頂點[n-1] .x *(頂點[0] .y - 頂點[n-2] .y)。 ?公式沒有這樣說。 – 2014-09-23 05:42:37

+0

這是另一種邊界情況,其中i = n-1,所以i + 1大於總頂點。我用更正的代碼更新了我的答案。 – 2014-09-23 05:45:05

+2

這是一個術語,我i = n-1,你把它留在你的循環中。 – Henry 2014-09-23 05:45:12

0

我找到了另一種方式,

再添加第一個元素爲多邊形陣列

這樣我們就能夠避免「出境」情況以及許多如果條件

這裏是我的解決方案:

public class PolygonArea { 

public static void main(String[] args) { 
    PolygonArea p = new PolygonArea(); 
    System.out.println(p.calculateArea()); 
} 

Point[] points = new Point[5]; 
public double calculateArea() { 
    points[0] = new Point("A", 4, 10); 
    points[1] = new Point("B", 9, 7); 
    points[2] = new Point("C", 11, 2); 
    points[3] = new Point("D", 2, 2); 

    /** Add first entry again to polygon */ 
    points[4] = new Point("A", 4, 10); 

    double sum = 0.0; 

    for (int i = 0; i < points.length - 1; ++i) { 
     sum += (points[i].X * points[i + 1].Y) - (points[i + 1].X * points[i].Y); 
    } 

    return Math.abs(sum/2); 
} 

class Point { 
    final String _ID; 
    final int X; 
    final int Y; 

    public Point(String id, int x, int y) { 
     _ID = id; 
     X = x; 
     Y = y; 
    } 

    } 
}