如何简化这个绘制雪花的 Blockly 程序?
How to simplify this Blockly program drawing a snowflake?
问题是这样说的:
The program shown below on the left draws a snowflake. It uses 33 code blocks.
Can you re-write it so that it uses at least one function and less than 30 blocks.
如何使用 Blockly 绘制这个?
经过一些研究,我认为递归函数不是这种情况下的解决方案,但在 python 中,我会尝试这样的方法
def drawLeaf(direction):
// Draw your leaf in this direction using your blocks code
for direction in [0, 72, 144, 216, 288]:
drawLeaf(direction)
首先,请看一下Finding coordinates of Koch Curve
这是一个 Java 代码片段(针对 Android),用于绘制 koch snowflake.It 的简单递归函数
public void drawfractal1(Canvas canvas , float x1 , float y1 , float x2 , float y2, int level){
Paint paint = new Paint();
int r = ThreadLocalRandom.current().nextInt(0, 256);
int g = ThreadLocalRandom.current().nextInt(0, 256);
int b = ThreadLocalRandom.current().nextInt(0, 256);
paint.setColor(Color.rgb(r,g,b));
paint.setStrokeWidth(7);
if(level==0) {
canvas.drawLine(x1,y1,x2,y2,paint);
}
else{
float ux = x2-x1;
float uy = y2-y1;
float vx = y1-y2;
float vy = x2-x1;
float xa = x1 + ux/3;
float ya = y1 + uy/3;
float xb = x1 + ux/2 + (float) sqrt(3)*vx/6;
float yb = y1 +uy/2 + (float)sqrt(3)*vy/6;
float xc = x1 + 2*ux/3;
float yc = y1 + 2*uy/3;
drawfractal1(canvas , x1,y1,xa,ya,level-1);
drawfractal1(canvas , xa,ya,xb,yb,level-1);
drawfractal1(canvas , xb,yb,xc,yc,level-1);
drawfractal1(canvas , xc,yc,x2,y2,level-1);
}
}
问题是这样说的:
The program shown below on the left draws a snowflake. It uses 33 code blocks. Can you re-write it so that it uses at least one function and less than 30 blocks.
如何使用 Blockly 绘制这个?
经过一些研究,我认为递归函数不是这种情况下的解决方案,但在 python 中,我会尝试这样的方法
def drawLeaf(direction):
// Draw your leaf in this direction using your blocks code
for direction in [0, 72, 144, 216, 288]:
drawLeaf(direction)
首先,请看一下Finding coordinates of Koch Curve
这是一个 Java 代码片段(针对 Android),用于绘制 koch snowflake.It 的简单递归函数
public void drawfractal1(Canvas canvas , float x1 , float y1 , float x2 , float y2, int level){
Paint paint = new Paint();
int r = ThreadLocalRandom.current().nextInt(0, 256);
int g = ThreadLocalRandom.current().nextInt(0, 256);
int b = ThreadLocalRandom.current().nextInt(0, 256);
paint.setColor(Color.rgb(r,g,b));
paint.setStrokeWidth(7);
if(level==0) {
canvas.drawLine(x1,y1,x2,y2,paint);
}
else{
float ux = x2-x1;
float uy = y2-y1;
float vx = y1-y2;
float vy = x2-x1;
float xa = x1 + ux/3;
float ya = y1 + uy/3;
float xb = x1 + ux/2 + (float) sqrt(3)*vx/6;
float yb = y1 +uy/2 + (float)sqrt(3)*vy/6;
float xc = x1 + 2*ux/3;
float yc = y1 + 2*uy/3;
drawfractal1(canvas , x1,y1,xa,ya,level-1);
drawfractal1(canvas , xa,ya,xb,yb,level-1);
drawfractal1(canvas , xb,yb,xc,yc,level-1);
drawfractal1(canvas , xc,yc,x2,y2,level-1);
}
}