2014-09-26 54 views
0

我試圖在加工過程中畫熊,(只是簡單的圓圈),我怎樣才能讓熊得到相等的空間距離,並且從屏幕邊緣到熊的兩側有相同的空間?以及垂直。加工間距

我知道這是模糊的,但我在可怕的解釋事物

回答

1

因爲你不提供任何代碼或例子,我只會告訴你如何放置圓草圖的中間。

爲了簡單起見想象此設置:

void setup(){ 
    size(400, 400); 
} 

1)非常基本的方法將是該圓到橢圓繪製函數的硬碼位置。

ellipse(200, 200, 50, 50); 

其中前兩個參數是圓心的座標。從400x400大小簡單找出,中間座標爲200x200。這是不好的方法,你應該避免使用它。

2)更好的辦法是使用全局變量來計算中心座標widthheight

ellipse(width/2, height/2, 50, 50); 

3)當要繪製或移動更復雜的對象,優選的是使用一些函數來在我們的示例中總是以相同的固定位置繪製此對象

void draw_circle(){ 
    ellipse(0, 0, 50, 50); 
} 

而只是移動中心使用transformations繪製所以我們draw函數將看起來像這樣

void draw(){ 
    pushMatrix(); 
    translate(width/2, height/2); 
    draw_circle(); 
    popMatrix(); 
} 

使用這個你可以能夠得出熊等距隔開,並從側面。

1

這聽起來像你想要一個等間距的圓形網格。爲此,你只需要將你的空間分成x和y方向的網格。做到這一點的最簡單方法是將Majlik在雙循環內顯示的內容包裝在「虛擬」網格中,從單元格移動到單元格。爲了更清楚地看到這一點,在下面的代碼中有一個額外的一點點,所以如果你按下'g'鍵(對於網格),你會看到網格單元格,每個單元格都有一個圓心。您可以按其他任何鍵使網格消失。

你可以看到,每路提供了相同的結果:平局內()取消註釋您想要的並註釋掉其它2.

int nx = 4; // number of circles horizontally 
int ny = 5; // number of circles vertically 
int divx; 
int divy; 
int diameter = 40; 

void setup() { 
    size(600, 600); 
    // calculate width and hegith of each cell of the grid 
    divx = width/nx; 
    divy = height/ny; 
} 

// 3 ways to draw a regular grid of circles 
void draw() { 
    background(200); 
    // show the cell layout if the g key was typed, otherwise don't 
    if(key == 'g') 
    drawGrid(); 

    // 1 way 
    for(int i = 0; i < nx; i++) { 
    for(int j = 0; j < ny; j++) {  
     ellipse(i * divx + divx/2, j * divy + divy/2, diameter, diameter); 
    } 
    } 

    // another way 
    // for(int i = divx/2; i < width; i += divx) { 
    // for(int j = divy/2; j < height; j += divy) {  
    //  ellipse(i, j, diameter, diameter); 
    // } 
    // } 

    // yet another way 
    // for(int i = divx/2; i < width; i += divx) { 
    // for(int j = divy/2; j < height; j += divy) { 
    //  pushMatrix(); 
    //  translate(i, j); 
    //  ellipse(0, 0, diameter, diameter); 
    //  popMatrix(); 
    // } 
    // } 
} 

void drawGrid() { 
    // draw vertical lines 
    for(int i = 1; i < nx; i++) { 
     line(i * divx, 0, i * divx, height); 
    } 

    // draw horizontal lines 
    for(int j = 1; j < ny; j++) {  
     line(0, j * divy, width, j * divy); 
    } 
}