Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C how to draw a point / set a pixel without using graphics library or any other library functions

Tags:

c

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?

like image 331
user999755 Avatar asked Feb 25 '13 16:02

user999755


People also ask

How do you draw pixels in computer graphics?

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.

How many parameters are there in Putpixel () function?

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.

Which function is used to draw a line?

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.


2 Answers

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:

  • world coordinates (like scaled longitude and latitude on a world map or femtometers on a electromagnet microscope) these are mostly your x and y
  • display coordinates: these are the representation of your displaying device, like a Nexus 7 or a Nexus 10 Tablet, with its physical dimensions (pixels or pels or dots per inch)

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

  • zoom the world-coordinate to display-coordinate: display-x = world-x * factor (femtometer or kilometer to pixel)
  • translate the world-center to display-center: display-X + adjustment

and so on. Just wikipedia for "alegebraic transformation" or "geometrics"

like image 132
Peter Miehle Avatar answered Oct 21 '22 05:10

Peter Miehle


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!

like image 29
luser droog Avatar answered Oct 21 '22 05:10

luser droog