Thread: Drawing circle into a bitmap file

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Unhappy 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 header[1078];
    	unsigned char data[640][480];
    	BMP()
    	{
    		for(int i = 0; i<1078; i++)
    			header[i] = 0;
    
    		header[0] = 'B';
    		header[1] = 'M';
    		unsigned char* p = &header[2];	
    		*(int*)(p) = 0x4b436; 
    		p = &header[6]; 
    		*(int*)(p) = 0x0;
    		p = &header[10]; 
    		*(int*)(p) = 0x436;
    		p = &header[14]; 
    		*(int*)(p) = 0x28;
    		p = &header[18]; 
    		*(int*)(p) = 0x280;
    		p = &header[22]; 
    		*(int*)(p) = 0x1e0;
    		p = &header[26]; 
    		*(unsigned short*)(p) = 0x1;
    		p = &header[28]; 
    		*(unsigned short*)(p) = 0x8;
    		p = &header[30]; 
    		*(int*)(p) = 0x0;
    		p = &header[34]; 
    		*(int*)(p) = 0x4b000;
    		p = &header[38]; 
    		*(int*)(p) = 0x0;
    		p = &header[42]; 
    		*(int*)(p) = 0x0;
    		p = &header[46]; 
    		*(int*)(p) = 0x0;
    		p = &header[50]; 
    		*(int*)(p) = 0x0;
    
    		p = &header[54];
    		*(int*)(p) = 0x0;
    		p = &header[58];
    		*(int*)(p) = 0x800000;
    		p = &header[62];
    		*(int*)(p) = 0x8000;
    		p = &header[66];
    		*(int*)(p) = 0x808000;
    		p = &header[70];
    		*(int*)(p) = 0x80;
    		p = &header[74];
    		*(int*)(p) = 0x800080;
    		p = &header[78];
    		*(int*)(p) = 0x8080;
    		p = &header[82];
    		*(int*)(p) = 0x808080;
    		p = &header[86];
    		*(int*)(p) = 0xC0C0C0;
    		p = &header[90];
    		*(int*)(p) = 0xFF00000;
    		p = &header[94];
    		*(int*)(p) = 0xFF00;
    		p = &header[98];
    		*(int*)(p) = 0xFFFF00;
    		p = &header[102];
    		*(int*)(p) = 0xFF;
    		p = &header[106];
    		*(int*)(p) = 0xFF00FF;
    		p = &header[110];
    		*(int*)(p) = 0xFFFF;
    		p = &header[114];
    		*(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++)
    		f<<header[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. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    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. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM