Draw Circle With Stars C++
The code below is a elementary way of drawing a circle without using fancy functions to jump to any capricious chosen 10 or y position.
#include <iostream> #include <math.h> using namespace std; int pth (int x,int y) { return sqrt (pow(x,ii)+prisoner of war(y,ii)); } int primary ( ) { int c=0; int r=ten; const int width=r; const int length=r*i.five; for (int y=width;y >= -width;y-=two) { for (int ten=-length;ten <= length;x++) { if ((int) pth(10,y)==r) cout << "*"; else cout << " "; } cout << "\north"; } cin.get(); return 0; }
If you lot want more advanced code and you lot are on the windows platform and you utilise Visual Studio to create programs then the code below would be of intrest.
It uses functions similar SetConsoleCursorPosition()
to gear up x
and y
positions, SetConsoleTextAttribute()
to set colors. The code draws lines , pixels, rectangles and circles in the console text window.
#include<stdio.h> #include <iostream> #include <stdio.h> #include <math.h> #include <time.h> #include <windows.h> using namespace std; void gotoxy(int x, int y); void setcolor(Word colour); void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor); void clearscreen(); void drawpixel( unsigned char 10, unsigned char y, unsigned char Colour); void drawpixel2( unsigned char 10, unsigned char y, unsigned char Color, char graphic symbol); void drawcircle(int 10, int y, int a, int b, int color); void drawline(int x0, int y0, int x1, int y1, int color); void drawfilledrectangle(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol); void drawframe(unsigned ten,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]); void drawwindow(unsigned 10,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,unsigned char bkcol,char text_[]); void drawcolorpalette(); int main(void){ setcolor(15); clearscreen(); drawwindow(3,ii,77,15,31,31,31,"a window is drawn"); int X = twoscore; int Y = 12; int rad = 8; int col = fifteen; double deg = 0; // draw a circle using sin( ) and cos( ) do { X = (int) (rad * cos(deg)); Y = (int) (rad * sin(deg)); drawpixel2 (40+X, 12+Y, col, '*' ); deg += 0.005; } while (deg <= vi.iv); drawcircle(60, ten, eight, 8, fifteen); drawline(ane,1,77,22,xv); gotoxy(40,iv); cout<<"Another circle is fatigued."; gotoxy(60,22); cout<<"A Line is fatigued."; gotoxy(20,13); cout<<"A circumvolve is drawn."; setcolor(vii); gotoxy(i,23); cin.ignore(); cin.go(); return 0; } //***************************************************************************** void gotoxy(int 10, int y){ COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); render; } //***************************************************************************** void setcolor(WORD color){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color); return; } // // colors: // 0 = Black // 1 = Blueish // two = Greenish // 3 = Cyan // 4 = Red // 5 = Magenta // vi = Yellow // seven = LightGray // 8 = DarkGray // ix = LightBlue // 10 = LightGreen // 11 = LightCyan // 12 = LightRed // 13 = LightMagenta // 14 = LightYellow // fifteen = White // //***************************************************************************** void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor){ int color=16*BackGroundColor+ForeGroundColor; setcolor(color); } //***************************************************************************** void clearscreen(){ COORD coordScreen = { 0, 0 }; DWORD cCharsWritten; CONSOLE_SCREEN_BUFFER_INFO csbi; DWORD dwConSize; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); GetConsoleScreenBufferInfo(hConsole, &csbi); dwConSize = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); GetConsoleScreenBufferInfo(hConsole, &csbi); FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); SetConsoleCursorPosition(hConsole, coordScreen); render; } //***************************************************************************** void drawpixel( unsigned char x, unsigned char y, unsigned char Colour){ setcolor(Colour); gotoxy(x,y);printf("Û"); } //***************************************************************************** void drawpixel2( unsigned char x, unsigned char y, unsigned char Colour, char character){ setcolor(Color); gotoxy(x,y);printf("%c",character); } //***************************************************************************** void drawcircle(int 10, int y, int a, int b, int color){ int wx, wy; int thresh; int asq = a * a; int bsq = b * b; int xa, ya; drawpixel(ten, y+b, color); drawpixel(x, y-b, color); wx = 0; wy = b; xa = 0; ya = asq * ii * b; thresh = asq / 4 - asq * b; for (;;) { thresh += xa + bsq; if (thresh >= 0) { ya -= asq * ii; thresh -= ya; wy--; } xa += bsq * 2; wx++; if (xa >= ya) break; drawpixel(x+wx, y-wy, color); drawpixel(x-wx, y-wy, color); drawpixel(x+wx, y+wy, color); drawpixel(x-wx, y+wy, color); } drawpixel(x+a, y, color); drawpixel(x-a, y, color); wx = a; wy = 0; xa = bsq * 2 * a; ya = 0; thresh = bsq / 4 - bsq * a; for (;;) { thresh += ya + asq; if (thresh >= 0) { xa -= bsq * 2; thresh = thresh - xa; wx--; } ya += asq * 2; wy++; if (ya > xa) break; drawpixel(ten+wx, y-wy, color); drawpixel(x-wx, y-wy, colour); drawpixel(x+wx, y+wy, color); drawpixel(x-wx, y+wy, color); } } //***************************************************************************** void drawline(int x0, int y0, int x1, int y1, int color){ int pix = colour; int dy = y1 - y0; int dx = x1 - x0; int stepx, stepy; if (dy < 0) { dy = -dy; stepy = -1; } else { stepy = 1; } if (dx < 0) { dx = -dx; stepx = -1; } else { stepx = one; } dy <<= ane; // dy is now 2*dy dx <<= 1; // dx is now 2*dx drawpixel( x0, y0,pix); if (dx > dy) { int fraction = dy - (dx >> one); // aforementioned every bit 2*dy - dx while (x0 != x1) { if (fraction >= 0) { y0 += stepy; fraction -= dx; // same equally fraction -= 2*dx } x0 += stepx; fraction += dy; // same as fraction -= 2*dy drawpixel( x0, y0,pix); } } else { int fraction = dx - (dy >> i); while (y0 != y1) { if (fraction >= 0) { x0 += stepx; fraction -= dy; } y0 += stepy; fraction += dx; drawpixel( x0, y0,pix); } } } //***************************************************************************** void drawframe(unsigned x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]) { unsigned i,j,chiliad; { g=(sx-ten); //differential j=m/8; //adjust j=j-ane; //more aligning gotoxy(x,y);printf("É"); //Elevation left corner of box gotoxy(sx,y);printf("»"); //Top correct corner of box gotoxy(x,sy);printf("È"); //Bottom left corner of box gotoxy(sx,sy);printf("¼"); //Bottom correct corner of box for (i=x+ane;i<sx;i++) { gotoxy(i,y);printf("Í"); // Height horizontol line gotoxy(i,sy);printf("Í"); // Bottom Horizontal line } for (i=y+1;i<sy;i++) { gotoxy(ten,i);printf("º"); //Left Vertical line gotoxy(sx,i);printf("º"); //Right Vertical Line } gotoxy(x+j,y);printf(text_); //put Title gotoxy(1,24); } } //***************************************************************************** void drawfilledrectangle(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2,unsigned char bkcol) { int x,y; setcolor(bkcol); //Set to color bkcol for (y=y1;y<y2;y++) //Fill Y Region Loop { for (x=x1;ten<x2;ten++) //Fill Ten region Loop { gotoxy(x,y);printf(" "); //Describe Solid space } } } //***************************************************************************** void drawwindow(unsigned x,unsigned y,unsigned sx,unsigned sy, unsigned char col, unsigned char col2,unsigned char bkcol,char text_[]) { drawfilledrectangle(x,y,sx,sy,bkcol); drawframe(x,y,sx,sy,col,col2,text_); } void drawcolorpalette() { for (int i=0;i<16;i++) { for (int j=0;j<sixteen;j++) { setForeGroundAndBackGroundColor(i,j); gotoxy(i*4,j);printf("%d",(i*j)+1); } } }
Source: https://stackoverflow.com/questions/38843226/how-to-draw-a-circle-with-stars-in-c
Post a Comment for "Draw Circle With Stars C++"