1 回答
TA贡献1804条经验 获得超3个赞
您所要做的就是在连续点之间画线。
点的下一个索引i是i+1。%对于最后一个点,它是 0。这可以通过模 ( ) 运算符 (h )来实现(i+1) % points.size()。
获取循环中的 2 个连续点并在它们之间画一条线:
for (int i = 0; i < points.size(); ++ i) {
Point p1 = points.get(i);
Point p2 = points.get((i+1) % points.size());
line(p1.x, p1.y, p2.x, p2.y);
}
参见示例:
void draw () {
background (255);
int gridW = width/n_part;
int gridH = height/n_part;
strokeWeight(1);
stroke(210);
noFill();
for (int row = 0; row < n_part; row++){
int gridY = 0 + row*gridH;
for (int col = 0; col < n_part; col++) {
int gridX = 0+ col* gridW;
rect (gridX, gridY, gridW, gridH);
}
}
stroke(0, 0, 0);
fill(0);
for (int i = 0; i < points.size(); ++ i) {
Point p = points.get(i);
p.Draw();
}
strokeWeight(3);
stroke(255, 0, 0);
for (int i = 0; i < points.size(); ++ i) {
Point p1 = points.get(i);
Point p2 = points.get((i+1) % points.size());
line(p1.x, p1.y, p2.x, p2.y);
}
}
添加回答
举报