Hy easyz!
So it was an ASCII spiral.
Here's a small code that makes a quarter circle.I knopw you wanted a spiral, but a spiral is just a circle whit variable radius.You can change the program to fit your needs.
The idea is that the user gives the radius and then the program makes a quarter of a circle of R points.For example, if R=10, then the program will make a quarter circle of 10 points.
The coordinates are calculated in cartezian
system, not polar, because polar isn't suited.And the x coordinates are always 1 apart.That means that for R=10, the x coordinates are 0,1,2,3,4,5,6,7,8,9.So they are equally distanced.But the y ones are not of course.Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int R=0; // the radius
int i=0;
int j=0;
int yc[100]; // the y coordinates, the x coordinates are i as it increments
int dy[100]; // the distance between the y coordinates, to know how many endl are between them
cout<<"Introduce the radius of the spiral"<<endl;
cin>>R;
dy[0]=0; // the first difference was 0
cout<<endl<<"The y coordinates"<<endl; // just to see the values calculated
for(i=0;i<=R;i++) // calculating y coordinates, as you can see i represents x
{
yc[i] = (int) sqrt( R*R - i*i );
cout<<yc[i]<<",";
}
cout<<endl;
cout<<"The dy coordinates"<<endl; // to see the values calculated
for(i=0;i<R;i++) // calculating the difference between 2 consecutive y coordinates
{
dy[i+1] = yc[i] - yc[i+1];
cout<<dy[i]<<",";
}
cout<<endl;
for(i=0;i<R;i++) // displaying the quarter circle
{
for(j=0;j<=i;j++) // adding the spaces before the *
{
cout<<" "; // there were more spaces added because one doesn't seem enough
}
cout<<"*"; // ading the *
for(j=0;j<=dy[i];j++) // adding the empty lines to create a hight
{
cout<<endl;
}
}
return 0;
}
| |
You probably will need to adjust the number of empty spaces and of new lines(end of lines) you display.
For example if you want 2 more empty spaces just ad 2 to i.Like this:
1 2 3 4
|
for(j=0;j<=i + 2;j++) // adding the spaces before the *
{
cout<<" "; // there were more spaces added because one doesn't seem enough
}
| |
The program does have some errors sometimes, maybe those aren't errors but are related to the fact that the screen is limited.For small R it work pretty well.
Anyway, why such a weird program??What do you need for??
Console graphics is very limited.Don't waste your time on it.Instead, use that time to learn a GUI library such WxWidgets, FLTK, gtkmm, even QT.That is if you don't already nkow one and your are doing this program as an experiment.