-1

我有一个程序,它必须制作一个三角形螺旋。有谁知道我可以如何改变这个程序,而是让一条整条线形成螺旋,它是由直线段组成的吗?以下是我到目前为止的代码...

void setup(){
  int len=100;
  int startx =250;
  int starty= 250;
  float angle = PI;
  size (500,500);
  spiral_triangle(len, startx,starty,angle);

}

void spiral_triangle(int len, int startx, int starty, float angle){

   if (len>1) 
        {line( startx, starty ,int(startx+len*cos(angle)),int(starty + len*sin(angle))  );
        int new_startx = int(startx+len*cos(angle));
        int new_starty = int(starty+len*sin(angle));
        int new_len = len -10;
        float new_angle = angle + PI/1.5;
        spiral_triangle(new_len, new_startx,new_starty,new_angle);
        } 
}

示例......而不是每边一条线,我需要这个--------每边......单独的线段(有点像虚线或虚线)

4

2 回答 2

1

如果我理解正确,您需要圆形螺旋而不是三角形螺旋。下面的代码应该实现这一点。

void spiral_triangle(float len, int startx, int starty, float angle){
   if (len>1) {
      float new_len = len - 0.1;
      float new_angle = angle + PI/100;
      int oldx = int(startx+len*cos(angle));
      int oldy = int(starty+len*sin(angle));
      int newx = int(startx+new_len*cos(new_angle));
      int newy = int(starty+new_len*sin(new_angle));
      line( oldx,oldy,newx,newy);   
      spiral_triangle(new_len, startx,starty,new_angle);
   } 
}
于 2012-12-04T23:03:09.783 回答
0

如果您要绘制虚线而不是实线,则必须根据此线程编写自己的代码:

http://processing.org/discourse/beta/num_1202486379.html

奇怪的是,库中没有虚线函数。使用该线程中的一个函数,您可以执行以下操作:

void setup() {
  int len=100;
  int startx =250;
  int starty= 250;
  float angle = PI;
  size (500, 500);
  spiral_triangle(len, startx, starty, angle);
}

void spiral_triangle(int len, int startx, int starty, float angle) {
  if (len>1) {
    float[] spacing = {2,2};
    dashline( startx, starty, int(startx+len*cos(angle)), int(starty + len*sin(angle)), spacing);
    int new_startx = int(startx+len*cos(angle));
    int new_starty = int(starty+len*sin(angle));
    int new_len = len -10;
    float new_angle = angle + PI/1.5;
    spiral_triangle(new_len, new_startx, new_starty, new_angle);
  }
}

/* 
 * Draw a dashed line with given set of dashes and gap lengths. 
 * x0 starting x-coordinate of line. 
 * y0 starting y-coordinate of line. 
 * x1 ending x-coordinate of line. 
 * y1 ending y-coordinate of line. 
 * spacing array giving lengths of dashes and gaps in pixels; 
 *  an array with values {5, 3, 9, 4} will draw a line with a 
 *  5-pixel dash, 3-pixel gap, 9-pixel dash, and 4-pixel gap. 
 *  if the array has an odd number of entries, the values are 
 *  recycled, so an array of {5, 3, 2} will draw a line with a 
 *  5-pixel dash, 3-pixel gap, 2-pixel dash, 5-pixel gap, 
 *  3-pixel dash, and 2-pixel gap, then repeat. 
 */ 
void dashline(float x0, float y0, float x1, float y1, float[ ] spacing) 
{ 
  float distance = dist(x0, y0, x1, y1); 
  float [ ] xSpacing = new float[spacing.length]; 
  float [ ] ySpacing = new float[spacing.length]; 
  float drawn = 0.0;  // amount of distance drawn 

  if (distance > 0) 
  { 
    int i; 
    boolean drawLine = true; // alternate between dashes and gaps 

    /* 
      Figure out x and y distances for each of the spacing values 
      I decided to trade memory for time; I'd rather allocate 
      a few dozen bytes than have to do a calculation every time 
      I draw. 
    */ 
    for (i = 0; i < spacing.length; i++) 
    { 
      xSpacing[i] = lerp(0, (x1 - x0), spacing[i] / distance); 
      ySpacing[i] = lerp(0, (y1 - y0), spacing[i] / distance); 
    } 

    i = 0; 
    while (drawn < distance) 
    { 
      if (drawLine) 
      { 
        line(x0, y0, x0 + xSpacing[i], y0 + ySpacing[i]); 
      } 
      x0 += xSpacing[i]; 
      y0 += ySpacing[i]; 
      /* Add distance "drawn" by this line or gap */ 
      drawn = drawn + mag(xSpacing[i], ySpacing[i]); 
      i = (i + 1) % spacing.length;  // cycle through array 
      drawLine = !drawLine;  // switch between dash and gap 
    } 
  } 
} 

我希望这有帮助。

于 2012-12-05T04:45:20.237 回答