2016-08-09 49 views
2

Ich bin neu in C++ und mache eine Übungsfrage. Was ich tat:Wie zeichne ich einen Kreis mit Sternen in C++?

for (int i = 0; i < 2 * r; i++) { 
    for (int j = 0; j < 2 * r; j++) { 
     if (i == sqrt((static_cast<int> (r))^2 - j^2)) { 
      cout << "*"; 
     } else { 
      cout << " "; 
     } 
    } 
    cout << endl; 
} 

Aber es zeichnen mir nichts. :(

+0

Haben Sie versucht, in einem Debugger durch das Programm treten? Machst du diese Schleifen eigentlich? Können Sie bitte versuchen, ein [minimales, ** vollständiges ** und verifizierbares Beispiel] (http://stackoverflow.com/help/mcve) zu erstellen und uns zu zeigen? –

+0

Was ist 'r'? Wie wird erklärt? Was ist sein Wert? – LPs

+2

Ich vermute, dass sqrt einen Gleitkommawert zurückgibt, der nicht genau mit i gleichgesetzt wird. Verwenden Sie einen Debugger? Wenn nicht, dann tun Sie es und gehen Sie durch Ihren Code und prüfen Sie die Werte, die berechnet werden. – Tarik

Antwort

1
.

enter image description here

Der folgende Code ist eine einfache Methode zum Zeichnen eines Kreises ohne Verwendung von ausgefallenen Funktionen, um zu einer beliebigen gewählten x- oder y-Position zu springen.

#include <iostream> 
#include <math.h> 

using namespace std; 

int pth (int x,int y) { 
    return sqrt (pow(x,2)+pow(y,2)); 
} 

int main () { 

    int c=0; 
    int r=10; 

    const int width=r; 
    const int length=r*1.5; 

    for (int y=width;y >= -width;y-=2) { 
     for (int x=-length;x <= length;x++) { 

      if ((int) pth(x,y)==r) cout << "*"; 
      else cout << " "; 

     } 
     cout << "\n"; 
    } 
    cin.get(); 

return 0; 
} 

Wenn Sie erweiterte Code wollen, und Sie sind auf der Windows-Plattform und verwenden Sie Visual Studio-Programme zu erstellen, wird der folgende Code von intrest wäre.

Es nutzt Funktionen wie SetConsoleCursorPosition()x und y Positionen zu setzen, SetConsoleTextAttribute() Farben einzustellen. Der Code zeichnet Linien, Pixel, Rechtecke und Kreise im Textfenster der Konsole.

enter image description here

#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 color); 
void setForeGroundAndBackGroundColor(int ForeGroundColor,int BackGroundColor); 
void clearscreen(); 
void drawpixel(unsigned char x, unsigned char y, unsigned char Color); 
void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character); 
void drawcircle(int x, 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 x,unsigned y,unsigned sx,unsigned sy,unsigned char col,unsigned char col2,char text_[]); 
void drawwindow(unsigned x,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,2,77,15,31,31,31,"a window is drawn"); 

    int X = 40; 
    int Y = 12; 
    int rad = 8; 
    int col = 15; 
    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 <= 6.4); 




    drawcircle(60, 10, 8, 8, 15); 

    drawline(1,1,77,22,15); 

    gotoxy(40,4); 
    cout<<"Another circle is drawn."; 


    gotoxy(60,22); 
    cout<<"A Line is drawn."; 

    gotoxy(20,13); 
    cout<<"A circle is drawn."; 



    setcolor(7); 
    gotoxy(1,23); 
    cin.ignore(); 
    cin.get(); 

return 0; 
} 


//***************************************************************************** 

void gotoxy(int x, int y){ 
    COORD coord; 
    coord.X = x; coord.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); 
    return; 
} 

//***************************************************************************** 

void setcolor(WORD color){ 
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color); 
    return; 
} 


// 
//  colors: 
//  0 = Black 
//  1 = Blue 
//  2 = Green 
//  3 = Cyan 
//  4 = Red 
//  5 = Magenta 
//  6 = Yellow 
//  7 = LightGray 
//  8 = DarkGray 
//  9 = LightBlue 
//  10 = LightGreen 
//  11 = LightCyan 
//  12 = LightRed 
//  13 = LightMagenta 
//  14 = LightYellow 
//  15 = 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); 
    return; 
} 

//***************************************************************************** 

void drawpixel(unsigned char x, unsigned char y, unsigned char Color){ 
     setcolor(Color); 
     gotoxy(x,y);printf("Û"); 
} 

//***************************************************************************** 

void drawpixel2(unsigned char x, unsigned char y, unsigned char Color, char character){ 
     setcolor(Color); 
     gotoxy(x,y);printf("%c",character); 
} 

//***************************************************************************** 

void drawcircle(int x, 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(x, y+b, color); 
    drawpixel(x, y-b, color); 

    wx = 0; 
    wy = b; 
    xa = 0; 
    ya = asq * 2 * b; 
    thresh = asq/4 - asq * b; 

    for (;;) { 
     thresh += xa + bsq; 

     if (thresh >= 0) { 
      ya -= asq * 2; 
      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(x+wx, y-wy, color); 
     drawpixel(x-wx, y-wy, color); 
     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 = color; 
    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 = 1; } 
    dy <<= 1;             // dy is now 2*dy 
    dx <<= 1;             // dx is now 2*dx 

    drawpixel(x0, y0,pix); 
    if (dx > dy) { 
     int fraction = dy - (dx >> 1);       // same as 2*dy - dx 
     while (x0 != x1) { 
      if (fraction >= 0) { 
       y0 += stepy; 
       fraction -= dx;        // same as fraction -= 2*dx 
      } 
      x0 += stepx; 
      fraction += dy;         // same as fraction -= 2*dy 
      drawpixel(x0, y0,pix); 
     } 
    } else { 
     int fraction = dx - (dy >> 1); 
     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,m; 
    { 

     m=(sx-x);      //differential 
     j=m/8;       //adjust 
     j=j-1;       //more adjustment 
     gotoxy(x,y);printf("É");  //Top left corner of box 
     gotoxy(sx,y);printf("»");  //Top right corner of box 
     gotoxy(x,sy);printf("È");  //Bottom left corner of box 
     gotoxy(sx,sy);printf("¼");  //Bottom right corner of box 

     for (i=x+1;i<sx;i++) 
     { 
      gotoxy(i,y);printf("Í");  // Top horizontol line 
      gotoxy(i,sy);printf("Í"); // Bottom Horizontal line 
     } 

     for (i=y+1;i<sy;i++) 
     { 
      gotoxy(x,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;x<x2;x++)    //Fill X region Loop 
     { 
      gotoxy(x,y);printf(" ");  //Draw 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<16;j++) 
     { 
      setForeGroundAndBackGroundColor(i,j); 
      gotoxy(i*4,j);printf("%d",(i*j)+1); 


     } 

    } 

} 
0

Sie können nicht mit cout an beliebigen Stellen ziehen - es ist für Zeile basierend auf der Konsole (Natürlich können Sie einen Kreis Zeile für Zeile ziehen, wenn das ist, was Sie wollen)

Sie ncurses verwenden könnte. Konsolen Zeichnung

Wenn Sie in der Lage sein wollen, auf eine „Leinwand“ an beliebigen chungen zu ziehen, dann gibt es viele Bibliotheken, die Sie dafür verwenden können - z. B. SFML, SDL und Qt und viele mehr

+0

Es sieht so aus, als ob die Übung es auf der Standardausgabe machen soll. – Tarik

+0

Ja. Es fordert mich auf, es mit std zu tun –