2014-02-07 25 views
0

我正在使用libGDX。 當我知道x位置時,我想獲得它們的多邊形高度。Java在某個x位置獲取多邊形高度

這張照片能更好地解釋:

enter image description here

+2

你想要哪個高度?有2個是可能的,你的照片很好地證明了這一點。 – Obicere

+0

其中任何一個,我認爲你說一個高度是從地面的0座標和一個從多邊形的開始 –

+0

那麼,最大的問題是決定你想要什麼。從那裏開始,就像按順序迭代點並使用線攔截一樣簡單。你將有垂直線,用'x'表示,線由兩點產生。 – Obicere

回答

0

我想你爲座標的一類:

public class Coordinate 
{ 
    public double x; 
    public double y; 

    public Coordinate(int x, int y) 
    { 
     this.x = x; 
     this.y = y; 
    } 
} 

讓我們假設您擁有一個包含多邊形的一類座標:

public class Polygon 
{ 
    protected Coordinate[] points; 

    public Polygon(Coordinate[] points) 
    { 
     this.points = points; 
    } 

    protected doulbe getMaxHeight(double x) 
    { 
     double height = 0; 
     for (int pointIndex = 0; pointIndex < points.length; pointIndex++) 
     { 
      int nextIndex = (pointIndex + 1) % points.length; 
      if ((points[pointIndex] >= x) && (points[nextIndex] <= x) || 
       (points[nextIndex] >= x) && (points[pointIndex] <= x)) 
      { 
       height = Math.max(height, Math.max(points[pointIndex].y, points[nextIndex].x)); 
      } 
     } 
     return height; 
    } 
} 

該代碼未經測試,但你可以嘗試一下,請讓我知道結果。