开发者

Make A Spiral With One for/While or do while loop

Is it possible to make a spiral开发者_开发知识库 with single loop? I've made spirals before, but only with multiple loops. Bonus for code that will show the output.

#include "conio.h"
#include "dos.h"
#include "stdlib.h"

void main()
{       
    int p,q,r,s,t;
clrscr();

for(p=8; p<14; p++)
{
    for(q=5  ; q<26; q++)
    {
        gotoxy(5,q);
        printf("Û");
    }
    for(r=5; r<50; r++)
    {
        printf("Ü");
    }
    for(s=25; s>4; s--)
    {
        gotoxy(50,s);
        printf("Û");
    }
    for(t=50; t>6; t--)
    {
        gotoxy(t,4);
        printf("Ü");
    }
    for(q=5; q<25; q++)
    {
        gotoxy(7,q);
        printf("Û");
            }
            // etc. . . I didn't write the full code.
}


One possible approach:

const float centerX = 10, centerY = 10;
const float speed = 0.1;
const float max_angle = 10;
const float angleStep = 0.1;

for (float angle = 0; angle < max_angle; angle += angleStep) {
    float radius = angle*speed;
    float sX = centerX+cos(angle)*radius;
    float sY = centerY+sin(angle)*radius;
    createPoint(sX,sY);
}

Substitute any drawing function for createPoint and play with the parameters.

One approach (similar to what's above) for createPoint would be:

void createPoint(int x, int y) {
    gotoxy(x,y);
    putchar('*');
}

If you want to use a graphics library later, you can change just this method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜