Object Composition Using C/C++ – Computer Graphics

Object Composition Using C/C++ – Computer Graphics

BASIC

In computer graphics we have to design many object. We can design them with programming. In this approach each pixel on the computer is filled with a point and form an object. such as point, line, triangle, rectangle, circle etc.

C PROGRAM

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main() {
   int gd = DETECT, gm;
   initgraph(&gd, &gm, "");
   line(100, 200, 500, 200);
   line(300, 050, 500, 200);
   line(300, 050, 100, 200);
   line(100, 200, 500, 200);
   line(500, 200, 500, 400);
   line(500, 400, 100, 400);
   line(100, 400, 100, 200);
   circle(300, 300, 075);
   getch();
   closegraph();
   return 0;
}

OUTPUT

EXPLANATION

The first three lines is to include three different header file. <stdio.h> is for standard input output. <conio.h> is for console input output. <graphics.h> is for drawing graph.

#include<stdio.h>
#include<conio.h>
#include<graphics.h>

Then we started the main function that is mandatory in C/C++. We declared two integer variables to draw graph. These are parameter of graph function. Where gd and gm stands for graphics driver and graphics mode respectively.

int main(){
    int gd = DETECT, gm;

We call the function intigraph to draw the graph.

initgraph(&gd, &gm, "");

Draw a triangle taking three intersecting lines. Line is a graphics built function it take four parameters to draw a line. These are (x1, y1, x2, y2).

line(100, 200, 500, 200);
line(300, 050, 500, 200);
line(300, 050, 100, 200);

Draw a triangle taking three intersecting lines.

line(100, 200, 500, 200);
line(500, 200, 500, 400);
line(500, 400, 100, 400);
line(100, 400, 100, 200);

You can also draw the same rectangle using the function below. Where the parameters are left, top, right, bottom respectively.

rectangle(100, 200, 500, 400;

Draw a circle using function below. Here the parameters are x-coordinate of center, y-coordinate of center and radius respectively.

circle(300, 300, 075);

Give getch() command so that graph waits for terminal or console. give closegraph() command to close the graph with output terminal.

getch();
closegraph();

Return the main function to 0. End the function with bracket.

    return 0;
}

I Think Your Experience with this tutorial is quite good. If You have to ask anything comment. Thank You.

Leave a Reply

Your email address will not be published. Required fields are marked *