I am trying to understand how I can draw a set of points (/set the pixels) that form a circle without using the library functions.
Now, getting the (x,y) co-ordinates of the points given the radius is straightforward.
for (x=-r; x <r; x=x+0.1) {
y = sqrt(r*r - x*x);
draw(x,y, 0, 0);
}
But once I have the points, how do you actually draw the circle is what is confusing to me. I can use the graphic library but I want to understand how you can do it without using the graphics library
void draw(float x, float y, float center_x, float center_y) {
//logic to set pixel given x, y and circle's center_x and center_y
// basically print x and y on the screen say print as a dot .
// u 'd need some sort of a 2d char array but how do you translate x and y
// to pixel positions
}
Could someone share any links/references or explain how this works?
putpixel() function in C h contains putpixel() function which plots a pixel at location (x, y) of specified color. Syntax : void putpixel(int x, int y, int color); where, (x, y) is the location at which pixel is to be put , and color specifies the color of the pixel.
A new pixel can be stored in an image, replacing an old one, by calling the putPixel function. This func- tion requires 6 parameters. The first parameter is the image that will be modified.
line function is used to draw a line from a point(x1,y1) to point(x2,y2) i.e. (x1,y1) and (x2,y2) are end points of the line. The code given below draws a line.
char display[80][26];
void clearDisplay(void) {
memset(display, ' ', sizeof(display));
}
void printDisplay(void) {
for (short row=0; row<26; row++) {
for (short col=0; col<80; col++) {
printf("%c", display[col][row]);
}
printf("\n");
}
}
void draw(float x, float y, float center_x, float center_y) {
if (visible(x,y)) {
display[normalize(x)][normalize(y)] = '.';
}
}
EDITH: you changed your comment, to incorporate more of your question, so I will expand my answer a bit.
you have two sets of coordinates:
you need a metric, that transforms your world coordinates into display coordinates. To make things more complicated, you need a window (the part of the world you want to show the user) to clip the things you do cannot show (like africa, when you want to show europe). And you may want to zoom your world coordinates to match your display coordinates (how much of europe you want to display)
These metrics and clippings are simple algebraic transformations
and so on. Just wikipedia for "alegebraic transformation" or "geometrics"
It's a tough question because technically C doesn't have any built-in input/output capability. Even writing to a file requires a library. On certain systems, like real-mode DOS, you could directly access the video memory and set pixels by writing values into that memory, but modern OSes really get in the way of doing that. You could try writing a bootloader to launch the program in a more permissive CPU mode without an OS, but that's an enormous can of worms.
So, using the bare mimimum, the stdio library, you can write to stdout using ascii graphics as the other answer shows, or you can output a simple graphics format like xbm which can be viewed with a separate image-viewing program. A better choice might be the netpbm formats.
For the circle-drawing part, take a look at the classic Bresenham algorithm. Or, for way too much information, chapter 1 of Jim Blinn's A Trip Down the Graphics Pipeline describes 15 different circle-drawing algorithms!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With