Well we all have seen the common questions on creating squares and hollow squares out of asterisks in C++, but how about creating a program that prints out a circle in asterisks? Obviously this would look like an oval due to line adjustment, but it would be quite interesting to see if someone could do it.

I have tried myself and I must admit it is a fairly decent challenge.

If you figure it out, post your code here!

Hint: r^2 = x^2 + y^2

Piece of cake, kinda crude approximation tho.

``````#include <iostream>
#include <cmath>
#include <string>
#include <vector>

using namespace std;

const unsigned CANVAS_WIDTH = 20;
const unsigned CANVAS_HEIGHT = 20;
const unsigned char BACKGROUND_FILL = '#';
const char POINT = '*';
const float PI = 3.14159265f;

typedef std::vector<std::string> Canvas;

int roundPositive(const float n){ return int(n + 0.5f); }

Canvas createDefaultCanvas(){
return Canvas(CANVAS_HEIGHT, std::string(CANVAS_WIDTH,BACKGROUND_FILL) );
}

const int canvasMidX = canvas.size() / 2; //assume same width for each height
const int canvasMidY = canvas.size() / 2;
const int STEP_SIZE = 1;
for(int deg = 0; deg < 360; deg += STEP_SIZE){
x = roundPositive(x);
y = roundPositive(y);
canvas[y][x] = POINT;
}
}

void showCanvas(const Canvas& canvas){
for(unsigned height = 0; height < canvas.size(); ++height){
for(unsigned width = 0; width < canvas[height].size(); ++width){
cout << canvas[height][width];
}
cout << endl;
}
}
int main(){
using namespace std;

Canvas canvas = createDefaultCanvas();

showCanvas(canvas);

return 0;
}``````

example output :

``````####################
####################
####################
####################
####################
########*****#######
######***###***#####
######*#######*#####
#####**#######**####
#####*#########*####
#####*#########*####
#####*#########*####
#####**#######**####
######*#######*#####
######***###***#####
########*****#######
####################
####################
####################
####################``````

Would look more like a circle if the characters were square and line spacing was the same as character spacing.

``````#include <iostream>
using namespace std;

int main()
{
double r = 1.2;

for( double y = -r-0.1; y <= r+0.2; y += 0.1 )
{
for( double x = -r-0.1; x <= r+0.2; x += 0.1 )
{
if( (x*x + y*y <= (r*r)*1.1) && (x*x + y*y >= (r*r)*0.9) )
cout << "*";
else
cout << "#";
}
cout << endl;
}
return 0;
}``````

Output:

``````###########################
##########*******##########
#######****#####****#######
######**###########**######
#####**#############**#####
####*#################*####
###**#################**###
##**###################**##
##*#####################*##
##*#####################*##
#**#####################**#
#*#######################*#
#*#######################*#
#*#######################*#
#*#######################*#
#*#######################*#
#**#####################**#
##*#####################*##
##*#####################*##
##**###################**##
###**#################**###
####*#################*####
#####**#############**#####
######**###########**######
#######****#####****#######
##########*******##########
###########################``````

Very Cool. Now how about a solid circle? =o

C'mon, lets address the aspect ratio.

``````#include <iostream>

int foo(double a, double b, double x, double y) {
return ((x*x)/(a*a) + (y*y)/(b*b));
}

int main () {
int b = 4, a = 10;
const int min = -15, max = 15;
int x = min, y = min;
for (; y < max; ++y) {
for (x = min; x < max; ++x)
std::cout << (foo(a,b,x,y) == 1 ? '*' : '#');
std::cout << std::endl;
}
return 0;
}``````
``````##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
#########*************########
######*******************#####
####*****#############*****###
##*****#################*****#
##****###################****#
#*****###################*****
##****###################****#
##*****#################*****#
####*****#############*****###
######*******************#####
#########*************########
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################``````
``````#include <iostream>
#include <cmath>
#include <sstream>

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

int main (int argc,char* argv[])  {
int c=0;
int r;
std::istringstream i(argv);
i >> r;

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) std::cout << "*";
else std::cout << " ";
}
std::cout << "\n";
}
std::cin.get();
}
***************
***                     ***
**                             **
**                                   **
*                                         *
*                                             *
**                                               **
*                                                   *
*                                                     *
*                                                       *
*                                                         *
*                                                         *
*                                                           *
*                                                           *
*                                                           *
*                                                           *
*                                                           *
*                                                           *
*                                                           *
*                                                         *
*                                                         *
*                                                       *
*                                                     *
*                                                   *
**                                               **
*                                             *
*                                         *
**                                   **
**                             **
***                     ***
***************``````
``````#include <iostream>
#include <cmath>
#include <sstream>

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

int main (int argc,char* argv)  {
if (argv==NULL) argv="30";
int c=0;
int r;
std::istringstream i(argv);
i >> r;

const int width=r;
const int length=r*1.5;
bool in=1;

for (int y=width;y >= -width;y-=2)  {
for (int x=-length;x <= length;x++)  {
if (pth(x,y)==r && pth(x-1,y)!=r)  {
std::cout << "*";
in^=1;
}
else if (in) std::cout << "*";
else std::cout << " ";
}
std::cout << "\n";
}
std::cin.get();
}
***************************************
*************************
********************************
**************************************
*******************************************
***********************************************
**************************************************
*****************************************************
*******************************************************
*********************************************************
***********************************************************
***********************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
*************************************************************
***********************************************************
***********************************************************
*********************************************************
*******************************************************
*****************************************************
**************************************************
***********************************************
*******************************************
**************************************
********************************
*************************
*****************************************************``````

Using midpoint circle algorithm:

``````#include <stack>
#include <string>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

typedef std::vector<std::string> Canvas;

void setPixel(int x, int y, Canvas& canvas){
canvas[y][x] = '*';
}
//midpoint circle algorithm -- src via wiki
void renderCircle(const int x0, const int y0, const int radius,Canvas& c){
int f = 1 - radius;
int ddF_x = 1;
int ddF_y = -2 * radius;
int x = 0;

while(x < y)
{
// ddF_x == 2 * x + 1;
// ddF_y == -2 * y;
// f == x*x + y*y - radius*radius + 2*x - y + 1;
if(f >= 0)
{
y--;
ddF_y += 2;
f += ddF_y;
}
x++;
ddF_x += 2;
f += ddF_x;
setPixel(x0 + x, y0 + y, c);
setPixel(x0 - x, y0 + y, c);
setPixel(x0 + x, y0 - y, c);
setPixel(x0 - x, y0 - y, c);
setPixel(x0 + y, y0 + x, c);
setPixel(x0 - y, y0 + x, c);
setPixel(x0 + y, y0 - x, c);
setPixel(x0 - y, y0 - x, c);
}
}

void show(const Canvas& c){
struct _show{
void operator()(const std::string& str){ cout << str << endl; }
};
std::for_each(c.begin(),c.end(),_show());
}
int main(){
Canvas c = Canvas(50,string(50,' ') );
show(c);
return 0;
}``````
``````*******
**       **
*           *
*             *
*               *
*                 *
*                 *
*                   *
*                   *
*                   *
*                   *
*                   *
*                   *
*                   *
*                 *
*                 *
*               *
*             *
*           *
**       **
*******``````

C'mon, lets address the aspect ratio.

``````#include <iostream>

int foo(double a, double b, double x, double y) {
return ((x*x)/(a*a) + (y*y)/(b*b));
}

int main () {
int b = 4, a = 10;
const int min = -15, max = 15;
int x = min, y = min;
for (; y < max; ++y) {
for (x = min; x < max; ++x)
std::cout << (foo(a,b,x,y) == 1 ? '*' : '#');
std::cout << std::endl;
}
return 0;
}``````
``````##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
#########*************########
######*******************#####
####*****#############*****###
##*****#################*****#
##****###################****#
#*****###################*****
##****###################****#
##*****#################*****#
####*****#############*****###
######*******************#####
#########*************########
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################``````

@L7Sqr: Cool program man, could be please also comment the code so that beginners like me would know what is happening in every line of code, thanks in advance!

You can read about the equation here - that is what the `foo` function does. The rest of the program just loops over the bounds of our drawing area evaluating that formula and drawing a `*` when it evaluates to 1.

my first post here:

``````#include <cmath>
#include <iostream>
#include <functional>
using namespace std;

struct coord { int x, y; };

struct pixel { char c; };
ostream& operator<<(ostream& s, pixel p) { return s << p.c << p.c; }

void canvas(ostream &s, function<pixel(coord)> F, coord size, coord center) {
for (int y = 0; y < size.y; ++y) {
for (int x = 0; x < size.x; ++x)
s << F({center.x - x, center.y - y});
s << endl;
}
}

int main() {
auto circle = [&](coord c) {
return int(sqrt(c.x * c.x + c.y * c.y)) == radius ? pixel {'#'} : pixel {' '};
};
canvas(cout, circle, size, {size.x / 2, size.y / 2});
}
``````

\$ ./circle

``````                                ##################
####                  ####
####                      ####
##                              ##
####                              ####
##                                  ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                      ##
##                                  ##
####                              ####
##                              ##
####                      ####
####                  ####
##################
``````