改变这个使用直线?进行中

时间:2012-12-04 22:40:41

标签: java processing

我有一个必要的程序,使三角螺旋。有谁知道我怎么能改变这个程序,而不是让整条线成为螺旋线,让它由直线段制成?以下是我到目前为止的代码......

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);
        } 
}

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

2 个答案:

答案 0 :(得分: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);
   } 
}

答案 1 :(得分: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 
    } 
  } 
} 

我希望这会有所帮助。