# Thread: Drawing circle into a bitmap file

1. ## Drawing circle into a bitmap file

I wrote a c++ program in order to draw some shapes bit by bit. But I have difficulties in drawing circle. I couldn't give it's original shape. I think about finding any point on a circle by using the formulas,but i have two unknowns.

for ex: r^2=(x-a)^2-(y-b)^2

where:
x and y are the x,y coordinates for any point on the circle.
a and b are the x,y coordinates for the center of the circle.
r is the radius of the circle

I give the center cordinates,i have the radius... But there are two variables (x,y) which is unknown.How can i get points.

Here is the code that I work for other shapes:

Code:
```#include <iostream>
#include <fstream>

class BMP
{
public:
unsigned char data[640][480];
BMP()
{
for(int i = 0; i<1078; i++)

*(int*)(p) = 0x4b436;
*(int*)(p) = 0x0;
*(int*)(p) = 0x436;
*(int*)(p) = 0x28;
*(int*)(p) = 0x280;
*(int*)(p) = 0x1e0;
*(unsigned short*)(p) = 0x1;
*(unsigned short*)(p) = 0x8;
*(int*)(p) = 0x0;
*(int*)(p) = 0x4b000;
*(int*)(p) = 0x0;
*(int*)(p) = 0x0;
*(int*)(p) = 0x0;
*(int*)(p) = 0x0;

*(int*)(p) = 0x0;
*(int*)(p) = 0x800000;
*(int*)(p) = 0x8000;
*(int*)(p) = 0x808000;
*(int*)(p) = 0x80;
*(int*)(p) = 0x800080;
*(int*)(p) = 0x8080;
*(int*)(p) = 0x808080;
*(int*)(p) = 0xC0C0C0;
*(int*)(p) = 0xFF00000;
*(int*)(p) = 0xFF00;
*(int*)(p) = 0xFFFF00;
*(int*)(p) = 0xFF;
*(int*)(p) = 0xFF00FF;
*(int*)(p) = 0xFFFF;
*(int*)(p) = 0xFFFFFF;

for(int i= 0; i<640; i++)
for(int j= 0; j<480; j++)
data[i][j] = 15;
}

void save(const std::string str="");

void line(int x1, int y, int length, int color)
{
for(int i=x1;i<x1+length;i++)
data[i][y] =  color;
}

void point(int x, int y, int color)
{
data[x][y] = color;
}

void rectangle(int x1, int x2, int y1, int y2,int color)
{
for(int i=x1;i<x2;i++)
data[i][y1] =  color;

for(int y=y1;y<y2;y++)
data[x1][y] =  color;

for(int i=x1;i<x2;i++)
data[i][y2] =  color;

for(int y=y1;y<y2;y++)
data[x2][y] =  color;

}
void square(int x1, int y1, int length,int color)
{
for(int i=x1;i<x1+length;i++)
data[i][y1] =  color;

for(int y=y1;y<y1+length;y++)
data[x1][y] =  color;

for(int i=x1;i<x1+length;i++)
data[i][y1+length] =  color;

for(int y=y1;y<y1+length;y++)
data[x1+length][y] =  color;

}

};

void BMP::save(const std::string str)
{
int i = 0;

std::ofstream f(str.c_str());

for(i = 0; i<1078; i++)

for(int i= 0; i<480; i++)
for(int j= 0; j<640; j++)
f<<data[j][i];

f.close();
}

int main()
{
BMP bitmap;

bitmap.line(100, 150, 150, 12);
bitmap.rectangle(200, 400, 200, 300,12);
bitmap.square(300, 50, 100,1);
bitmap.save("renk.bmp");

return 0;
}```

2. Code:
```r^2 = (x - a)^2 - (y - b)^2

(y - b)^2 = (x - a)^2 - r^2

y - b = +- ((x - a)^2 - r^2)^0.5

y = b +- ((x - a)^2 - r^2)^0.5```
Then you can plug in points for x from r to -r (x is not unknown, it has a domain that you plug in stuff for). This is not a very good algorithm though, and the circle will be splotchy. Google circle drawing algorithms.

3. 1. For each point
2. If it's distance to the center (Pythagoras's) is less than/equals radius
3. It's in the circle
4. Otherwise
5. It is outside the circle

Draws a solid circle.