5つの回転した長方形のグリッドを単純に作成しようとしています。ただし、グリッドは中央に配置されません。誰かが私を助けることができますか?
int margin = 150; //padding to sides and top/bottom
int rectH = 60; // height of rectangle
int rectW = 20; // width of rectangle
int n_rectangles = 5; // 5 rectangles to draw
size(800,800);
for (int x = margin+rectW; x <= width - margin; x += (width-2*(margin+rectW))/n_rectangles) {
for (int y = margin+rectH; y <= height - margin; y += (height-2*(margin+rectH))/n_rectangles) {
fill(255);
//now rotate matrix 45 degrees
pushMatrix();
translate(x, y);
rotate(radians(45));
// draw rectangle at x,y point
rect(0, 0, rectW, rectH);
popMatrix();
}
}
単一の「中央に配置された」長方形を描画することをお勧めします。長方形の原点は(-rectW/2, -rectH/2)
次のとおりです。
rect(-rectW/2, -rectH/2, rectW, rectH);
行と列について、最初の長方形の中心から最後の長方形の中心までの距離を計算します。
int size_x = margin * (n_rectangles-1);
int size_y = margin * (n_rectangles-1);
画面の中央に変換(width/2, height/2)
、
左上の矩形の位置に(-size_x/2, -size_y/2)
、最終的にその位置に各矩形(i*margin, j*margin)
:
translate(width/2 - size_x/2 + i*margin, height/2 - size_y/2 + j*margin);
最終的なコードを参照してください。
int margin = 150; //padding to sides and top/bottom
int rectH = 60; // height of rectangle
int rectW = 20; // width of rectangle
int n_rectangles = 5; // 5 rectangles to draw
size(800,800);
int size_x = margin * (n_rectangles-1);
int size_y = margin * (n_rectangles-1);
for (int i = 0; i < n_rectangles; ++i ) {
for (int j = 0; j < n_rectangles; ++j ) {
fill(255);
pushMatrix();
translate(width/2 - size_x/2 + i*margin, height/2 -size_y/2 + j*margin);
rotate(radians(45));
rect(-rectW/2, -rectH/2, rectW, rectH);
popMatrix();
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加