Making circle in C++ without using and Graphics libraies

Making a circle or any geometrical object by using graphics libraries is not a big deal but if we can create the geometrical object without using any graphics libraries is a little bit tricky.
Today, I made a circle without using any graphics libraries. This can be done by using the two-dimensional array and parametric equation of a circle. Here is the given below code:

#include<iostream>
#include<cmath>

using namespace std;

int main(){
    int width = 80, height = 25, radius;
    char ch;
    cout << "Enter the radius of circles less than or equal to 12: "; cin >> radius;
    cout << "\nEnter the character by which you make a circle: "; cin >>  ch;   

    // Initialise a two dimensional array
    char display[width][height];

    // Create a empty display 
    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
           display[i][j] = ' '; // Every element of display equal to empty
        };
    };
    
    // Create a circle by using parametric equations of circle
    for(int angle = 0; angle < 360; angle += 1){
        // Calculate x and y by using parametic formulas of circle
        int x = (radius * cos(angle)); 
        int y = (radius * sin(angle));
        
        // Moving cordinates toward center of the display
        int cx = height/2 + x;
        int cy = width/2 + y;
        
        // Character which made the circle
        display[cx][cy] = ch;
    }

    // Display all elements present in display array
    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
           cout << display[i][j];
        };
        cout << endl;
    };

    return 0;
}

Source:
http://www.mathopenref.com/coordparamcircle.html
http://stackoverflow.com/questions/15072025/c-how-to-draw-a-point-set-a-pixel-without-using-graphics-library-or-any-other
https://github.com/amrit3701/Programs/blob/master/circle.cpp

FollowFollow on FacebookFollow on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *