2017-09-26 93 views
1

我試圖從其4個角生成網格點。由於這個角落可以自由放置,它會看起來像網格有一個透視。從4個角度創建網格透視

我已經寫在處理中,其中角部按順時針順序將下面的代碼(起始於左上角)

PVector[][] setGrid(PVector[] corners, int cols, int rows) { 
    PVector[][] grid = new PVector[rows][cols]; 
    for(int y = 0; y < rows; y++) { 
     float fY = (float)y/(rows - 1); 
     PVector p1 = PVector.lerp(corners[0], corners[3], fY); 
     PVector p2 = PVector.lerp(corners[1], corners[2], fY); 
     for(int x = 0; x < cols; x++) { 
      grid[y][x] = PVector.lerp(p1, p2, (float)x/(cols-1)); 
     } 
    } 
    return grid; 
} 

這產生具有內插點的網格,但它不對應於透視網格。所有的在線點都是等距的,而在透視圖中,最接近點應該比最遠點更分離。

我希望得到一些方向,如果可能的話,在Java /加工

編輯

爲了澄清我的答案。我定義了4個隨機角點,我想獲得所有創建perspective deformed grid的點。請注意,因爲透視圖dX1!= dX2以及dY1!= dY2。我寫的代碼不會產生這種效果(我知道這一點,但我不知道該如何去做)因爲點是插值的,導致dX1 = dX2 = ... = dXi和dY1 = dY2 = ... = dYi

我已閱讀有關視角變換,但我不需要變換圖像,我只需要得到grid points coordinates

+0

我真的不知道是什麼你問。你可以請發表[mcve]而不是斷開連接的功能嗎?你可以發表你想要做的模擬嗎?哪一行代碼的行爲與您的預期不同? –

+0

我試圖解釋我自己一點點更好,並添加了我想要實現的所需結果的繪圖 – markusand

回答

0

在您的示例圖像中,透視效果是通過保持沿不同長度邊緣的線數不變來實現的。這就是你的實現,所以我真的沒有看到這個問題。

這裏是呼喚你的setGrid()草圖:

PVector[] corners; 

void setup(){ 
    size(150,100); 
    corners = new PVector[4]; 
    corners[0] = new PVector(35,20); 
    corners[1] = new PVector(15,height-30); 
    corners[2] = new PVector(width-10,height-10); 
    corners[3] = new PVector(width-30,10); 
    noLoop(); 
} 
void draw(){ 
    background(255); 
    PVector[][] results = setGrid(corners, 9, 9); 
    for(PVector[] pvs : results){ 
    for(PVector pv : pvs){ 
     ellipse(pv.x,pv.y,5,5); 
    } 
    } 
} 

PVector[][] setGrid(PVector[] corners, int cols, int rows) { 
    PVector[][] grid = new PVector[rows][cols]; 
    for(int y = 0; y < rows; y++) { 
     float fY = (float)y/(rows - 1); 
     PVector p1 = PVector.lerp(corners[0], corners[3], fY); 
     PVector p2 = PVector.lerp(corners[1], corners[2], fY); 
     for(int x = 0; x < cols; x++) { 
      grid[y][x] = PVector.lerp(p1, p2, (float)x/(cols-1)); 
     } 
    } 
    return grid; 
} 

...,結果看起來非常像你的目標圖像。如果您看到不同的東西,也許您正在創建邊緣長度非常相似的網格?

如果要投影的角度對規則的梯形 - 就像一個人行道後退到距離 - 再考慮這種方法代替:

+0

我共享的方法的結果並不是我真正需要的。同一列或行中的點之間的距離是等距的,而點應該距離觀看者越近,而「越遠」越小。我不確定在2D場景中是否有某種模擬此3D效果的算法 – markusand

+0

有一種算法 - 就像我在上面鏈接的math.stackexchange答案中一樣 - 但是您必須提供僞3D信息。在那裏看到自我回答。 – JeremyDouglass