2012-12-04 2 views
-1

나는 필수적인 삼각형 나선을 만드는 프로그램을 가지고 있습니다. 누구든지이 프로그램을 어떻게 바꿀 수 있는지 아는 사람이 하나의 전체 라인을 나선형으로 만들었고 직선 세그먼트로 만들었습니까? 다음 대신 면당 1 개 라인, I (이 -------- 면당 ... 개별 선분 필요 ... 나 지금까지가 코드 ...직선을 사용하려면이 값을 변경 하시겠습니까? 처리 중에

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

예제 점선 또는 점선과 유사)

+0

나는 혼란 스럽다. 게시 한 코드는 개별 선분을 만든다. spiral_triangle에 대한 호출은 재귀 적이며 각 호출 중에 별도의 행을 그립니다. 그것은 구부러진 하나의 선이 아니라 개별 선분을 그리는 것입니다. 귀하의 요청을 명확히하실 수 있습니까? – spex

+0

나는 질문을 편집했다 :) – choloboy

+0

나는 아직도 혼란 스럽다. 코드가 이미 직선 부분에서 나선형을 만듭니다. – spex

답변

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

나는 빠른 대답을 고맙게 생각한다. .. 그러나 내가 원했던 것은 삼각형을 형성하기 위해 구부러진 채로있는 1 개의 선을 오히려 가지고 있었지만 삼각형의 나선형이었다. 그것은 삼각형의 나선형을 형성하고있는 개인의 선분 일 것이다. .. 어떤 혼란 내 게시물에. – choloboy

+0

질문에 대한 답변이 아닌 경우 답변으로 표시하거나 다른 독자를 혼란스럽게 할 수 있습니다. – spex

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

이 정보가 도움이되기를 바랍니다.

관련 문제