我正在制作一個腳本原型,以在旋轉平面周圍繪制等距點,而處理會產生意外的結果?這是我的代碼:int WHITE = 255;int BLACK = 0;void setup() { size(500, 500);}void draw() { background(WHITE); translate(width/2, height/2); // move origin to center of window // center ellipse noStroke(); fill(255, 0, 0); ellipse(0, 0, 10, 10); // center point, red // satellite ellipses fill(BLACK); int points = 4; for (int i = 0; i < points; i++) { rotate(i * (TWO_PI / points)); ellipse(0, 100, 10, 10); // after plane rotation, plot point of size(10, 10), 100 points above y axis }}當points = 4我得到我期望的輸出時points = 5 // also when points = 3 or > 4,但是當 時,我得到的輸出缺少繪制點,但間距仍然正確。為什么會發生這種情況?
1 回答

莫回無
TA貢獻1865條經驗 獲得超7個贊
你旋轉太多了:你不想i * angle
在每次迭代時都旋轉,因為如果我們這樣做,我們最終會旋轉太多,以至于點最終會重疊。例如,對于原樣的代碼,我們希望將 3 個點放置在 0、120 和 240 度(或 120、240、360)處。但事實并非如此:
當 i=0 時,我們旋轉 0 度。到目前為止,一切都很好。
當 i=1 時,我們在 0 的基礎上旋轉 120 度。還是不錯的。
當 i=2 時,我們在 120 度的基礎上旋轉 240 度。這 120 度太遠了!
這顯然不是我們想要的,所以只需旋轉固定角度TAU / points
,事情就會按預期工作:
for (int i = 0; i < points; i++) {
rotate(TAU / points);
ellipse(0, 100, 10, 10);
}
或者,保持增量角度,但然后通過使用三角學來計算放置rotate(),而不使用 來放置點:
float x = 0, y = 100, nx, ny, angle;
for (int i = 0; i < points; i++) {
angle = i * TAU / points;
nx = x * cos(a) - y * sin(a);
ny = x * sin(a) + y * cos(a);
ellipse(nx, ny, 10, 10);
}
添加回答
舉報
0/150
提交
取消