how can i draw a circle divided to many sectors ,each secteor area deppends on specific angle for each sector in the circle ,
thanx for help ,
regards
you will have to use some sort of gui libraries and/or compiler. Depends on your operating system.
If you don't know it already, start a course in trigonometry. It might help.
Hello is spelled without the w. =)
Also, trigonometry is the way to go. You can probably find a tutorial or something: www.google.com/search?q=Trigonometry+Tutorial
trig will help for the math part but is useless for the gui part. What you want is an easy way to draw pie charts in C or C++. Read some of these google links.
(x - a) ^ 2 + (y - b) ^ 2 = r ^ 2
Where a and b are the center coordinates, and r is the radius. This should help you solve for x, and y, given one or the other.
This is useful for plotting, but sin, cos, and tan will be useful for splitting it up into sectors. (Remember a full rotation is 2 * pi)
Trig isn't all that difficult.. I put together a small example of how to create a bitmap, draw a circle and a sector in it (with a 50 deg angle). I tried to keep the code as short as possible, but it is still over 200 lines, so I will post the important part and attach the whole code. It should draw on the bitmap and save it as circle.bmp in the project folder.
const double PI = 3.141592;
const double deg_to_rad = PI / 180;
const double rad_to_deg = 180 / PI;
void draw_sector(Bitmap &bmp, int deg_startangle, int deg_finangle, int radius) {
int centre_x = bmp.width / 2;
int centre_y = bmp.height / 2;
// Start Angle
bmp.line(
centre_x, // x1
centre_y, // y1
int(centre_x + cos(deg_startangle * deg_to_rad) * radius), // x2
int(centre_y + sin(deg_startangle * deg_to_rad) * radius) // y2
);
// Finish Angle
bmp.line(
centre_x, // x1
centre_y, // y1
int(centre_x + cos(deg_finangle * deg_to_rad) * radius), // x2
int(centre_y + sin(deg_finangle * deg_to_rad) * radius) // y2
);
}
int main() {
// Create Bitmap object
Bitmap bmp(500, 500);
// Centre of bitmap
int centre_x = bmp.width / 2;
int centre_y = bmp.height / 2;
// Radius of circle
int radius = 150;
int x1, y1, x2, y2; // Temp location variables
// Set default old coordient
x2 = centre_x + radius;
y2 = centre_y;
cout << "Drawing circle with a sector of 50 deg\n";
// Draw a circle ( loop from 0 to (2*PI) ) with 1deg accuracy
for (double i = 0; i < (360 * deg_to_rad); i += (1 * deg_to_rad)) {
// Get next coordients to draw a line to
x1 = int(centre_x + cos(i) * 150);
y1 = int(centre_y + sin(i) * 150);
// Draw line from old coordients to new ones
bmp.line(x1, y1, x2, y2);
// Set old coordients
x2 = x1;
y2 = y1;
}
// Draw a sector between angle 0 and 50
draw_sector(bmp, 0, 50, radius);
bmp.save("circle.bmp");
cout << "\nDone!\n\nPress <ENTER> to continue..\n";
cin.ignore();
return 0;
}
And here are some short tutorials to help :)
Radians & Degrees
Trigonometry
Hope all this helps :]
#include <iostream>
#include <cmath>
#include <windows.h>
using namespace std;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned char uchar;
struct Pixel {
union {
ulong rgb;
struct {
unsigned char b, g, r;
};
};
Pixel() {
rgb = 0;
}
Pixel(byte _r, byte _g, byte _b) {
r = _r;
g = _g;
b = _b;
}
inline operator ulong() {
return rgb;
}
inline Pixel &operator =(ulong _RGB) {
rgb = _RGB;
return *this;
}
} white(255, 255, 255);
class Bitmap {
public:
Pixel *pixels;
short width;
short height;
Bitmap(uint _Width = 0, uint _Height = 0) {
width = _Width;
height = _Height;
pixels = new Pixel[width * height];
}
~Bitmap() {
delete[] pixels;
}
inline void line(int x1, int y1, int x2, int y2, Pixel &col = white) {
if (x1 < 0) { x1 = 0; }
else if (x1 > width) { x1 = width; }
if (x2 < 0) { x2 = 0; }
else if (x2 > width) { x2 = width; }
if (y1 < 0) { y1 = 0; }
else if (y1 > height) { y1 = height; }
if (y2 < 0) { y2 = 0; }
else if (y2 > height) { y2 = height; }
y1 = height - y1;
y2 = height - y2;
int dx, dy, dx2, dy2, x_inc, y_inc, error, index;
Pixel *vb_start = pixels + x1 + y1 * width;
dx = x2-x1;
dy = y2-y1;
if (dx >= 0) { x_inc = 1; }
else { x_inc = -1; dx = -dx; }
if (dy >= 0) { y_inc = width; }
else { y_inc = -width; dy = -dy; }
dx2 = dx << 1;
dy2 = dy << 1;
if (dx > dy) {
error = dy2 - dx;
for (index = 0; index <= dx; ++index) {
*vb_start = col;
if (error >= 0) {
error -= dx2;
vb_start+=y_inc;
}
error+=dy2;
vb_start+=x_inc;
}
} else {
error = dx2 - dy;
for (index = 0; index <= dy; ++index) {
*vb_start = col;
if (error >= 0) {
error -= dy2;
vb_start += x_inc;
}
error+=dx2;
vb_start+=y_inc;
}
}
}
void save(const char *_FileName) {
BITMAPINFOHEADER bmpInfoHeader = {0};
bmpInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfoHeader.biBitCount = sizeof(Pixel) * 8;
bmpInfoHeader.biClrImportant = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biCompression = BI_RGB;
bmpInfoHeader.biHeight = height;
bmpInfoHeader.biWidth = width;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biSizeImage = width * height * sizeof(Pixel);
BITMAPFILEHEADER bfh = {0};
bfh.bfType = 0x4D42;
bfh.bfOffBits = sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER);
bfh.bfSize = bfh.bfOffBits + bmpInfoHeader.biSizeImage;
HANDLE hFile = CreateFile( _FileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (!hFile) {
return;
}
DWORD dwWritten = 0;
WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
WriteFile( hFile, &bmpInfoHeader, sizeof(bmpInfoHeader), &dwWritten, NULL );
WriteFile( hFile, &pixels[0], bmpInfoHeader.biSizeImage, &dwWritten, NULL );
CloseHandle( hFile );
}
};
const double PI = 3.141592;
const double deg_to_rad = PI / 180;
const double rad_to_deg = 180 / PI;
void draw_sector(Bitmap &bmp, int deg_startangle, int deg_finangle, int radius) {
int centre_x = bmp.width / 2;
int centre_y = bmp.height / 2;
// Start Angle
bmp.line(
centre_x, // x1
centre_y, // y1
int(centre_x + cos(deg_startangle * deg_to_rad) * radius), // x2
int(centre_y + sin(deg_startangle * deg_to_rad) * radius) // y2
);
// Finish Angle
bmp.line(
centre_x, // x1
centre_y, // y1
int(centre_x + cos(deg_finangle * deg_to_rad) * radius), // x2
int(centre_y + sin(deg_finangle * deg_to_rad) * radius) // y2
);
}
int main() {
// Create Bitmap object
Bitmap bmp(500, 500);
// Centre of bitmap
int centre_x = bmp.width / 2;
int centre_y = bmp.height / 2;
// Radius of circle
int radius = 150;
int x1, y1, x2, y2; // Temp location variables
// Set default old coordient
x2 = centre_x + radius;
y2 = centre_y;
cout << "Drawing circle with a sector of 50 deg\n";
// Draw a circle ( loop from 0 to (2*PI) ) with 1deg accuracy
for (double i = 0; i < (360 * deg_to_rad); i += (1 * deg_to_rad)) {
// Get next coordients to draw a line to
x1 = int(centre_x + cos(i) * 150);
y1 = int(centre_y + sin(i) * 150);
// Draw line from old coordients to new ones
bmp.line(x1, y1, x2, y2);
// Set old coordients
x2 = x1;
y2 = y1;
}
// Draw a sector between angle 0 and 50
draw_sector(bmp, 0, 50, radius);
bmp.save("circle.bmp");
cout << "\nDone!\n\nPress <ENTER> to continue..\n";
cin.ignore();
return 0;
}
thanks all my freinds , you response is realy helpful....
Best regards
extrov
Hello is spelled without the w. =)
Also, trigonometry is the way to go. You can probably find a tutorial or something: www.google.com/search?q=Trigonometry+Tutorial
My fingers get out of sync with my brain so often, every time I try to demo "Hello world" it comes out "Hellow orld"
Or maybe it's just an emphatic variant of Hello?
My fingers get out of sync with my brain so often, every time I try to demo "Hello world" it comes out "Hellow orld"
Or maybe it's just an emphatic variant of Hello?
yes thats it , u gut it ...
my be sometimes when people miss something ,anothers add it ...
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.