1

我試圖瞭解給出here(此代碼也可作爲小程序here)的二進制空間分區方法實現。我已經明白大部分代碼,但無法理解的方法:瞭解Java代碼背後的數學:2D二進制空間分區

public void renderLine(int[] l){ 
     double x1=l[2]; 
     double y1=l[3]; 
     double x2=l[0]; 
     double y2=l[1]; 
     double pCos = Math.cos(eye_angle); 
     double pSin = Math.sin(eye_angle); 
     int[] x = new int[4]; 
     int[] y = new int[4]; 
     double pD=-pSin*eye_x+pCos*eye_y; //What is this line doing? 
     double pDp=pCos*eye_x+pSin*eye_y; //And this? 
     double rz1,rz2,rx1,rx2; 
     int Screen_x1=0,Screen_x2=0; 
     double Screen_y1,Screen_y2,Screen_y3,Screen_y4; 
     rz1=pCos*x1+pSin*y1-pDp;  //perpendicular line to the players 
     rz2=pCos*x2+pSin*y2-pDp;  //view point 
     if((rz1<1) && (rz2<1)) 
      return; 
     rx1=pCos*y1-pSin*x1-pD; 
     rx2=pCos*y2-pSin*x2-pD; 
     double pTan = 0; 
     if((x2-x1) == 0) 
      pTan = Double.MAX_VALUE; 
     else 
      pTan = (y2-y1)/(x2-x1); 
     pTan = (pTan-Math.tan(eye_angle))/(1+ 
      (pTan*Math.tan(eye_angle))); 
     if(rz1 < 1){ 
      rx1+=(1-rz1)*pTan; 
      rz1=1; 
     }if(rz2 < 1){ 
      rx2+=(1-rz2)*pTan; 
      rz2=1; 
     } 
     double z1 = m_width/2/rz1; 
     double z2 = m_width/2/rz2; 
     Screen_x1=(int)(m_width/2-rx1*z1); 
     Screen_x2=(int)(m_width/2-rx2*z2); 
     if(Screen_x1 > m_width) 
      return; 
     if(Screen_x2<0) 
      return; 
     int wt=88; 
     int wb=-40; 
     Screen_y1=(double)m_height/2-(double)wt*z1; 
     Screen_y4=(double)m_height/2-(double)wb*z1; 
     Screen_y2=(double)m_height/2-(double)wt*z2; 
     Screen_y3=(double)m_height/2-(double)wb*z2; 
     if(Screen_x1 < 0){ 
      Screen_y1+=(0-Screen_x1)*(Screen_y2-Screen_y1) 
       /(Screen_x2-Screen_x1); 
      Screen_y4+=(0-Screen_x1)*(Screen_y3-Screen_y4) 
       /(Screen_x2-Screen_x1); 
      Screen_x1=0; 
     }if(Screen_x2 > (m_width)){ 
      Screen_y2-=(Screen_x2-m_width)*(Screen_y2-Screen_y1) 
       /(Screen_x2-Screen_x1); 
      Screen_y3-=(Screen_x2-m_width)*(Screen_y3-Screen_y4) 
       /(Screen_x2-Screen_x1); 
      Screen_x2=m_width; 
     }if((Screen_x2-Screen_x1) == 0) 
      return; 
     x[0] = (int)Screen_x1; 
     y[0] = (int)(Screen_y1); 
     x[1] = (int)Screen_x2; 
     y[1] = (int)(Screen_y2); 
     x[2] = (int)Screen_x2; 
     y[2] = (int)(Screen_y3); 
     x[3] = (int)Screen_x1; 
     y[3] = (int)(Screen_y4); 
     double_graphics.setColor(new Color(l[4])); 
     double_graphics.fillPolygon(x,y,4); 
    } 

我不能夠把握座標幾何背後此方法的實現。你能向我解釋嗎?

+0

沒有人是每個人都會閱讀該代碼imho。它太瘋狂了。 –

+0

這段代碼和描述不是我見過的最好的。令人驚訝的是,他們會嘗試解釋,甚至沒有一個單一的圖。看看我爲學校BSP課程鏈接的參考資料:(鏈接)[http://sha.nnoncarey.com/page.php?page=physics]如果你願意,我可以設置你的源代碼:它是在C++中,但它應該有體面的評論。 – Shannon

回答

3

上半部分計算線相對於虛擬眼睛的角度。它還使用切線計算它是否可見。 第二部分調整行的大小,使其與屏幕成比例。 在我看來,他們將座標系的原點設置在屏幕中間。

我只剔除了代碼,所以其中一些細節可能會關閉。

看看這個。它可能會啓動你在正確的方向 http://www.cs.uic.edu/~jbell/CourseNotes/ComputerGraphics/2DTransforms.html