Creating two dimensional objects

2 years ago
Computer Graphics

Using certain graphic functions available in C-language for drawing lines, rectangles & circles, Write a C- Program which generates pixel activation list for drawing the following simple two-dimensional objects (Circle, Ellipse…..).

  1. House ii) Car iii) Fish iv) man?

 

Aim: To write a C-program for creating simple two-dimensional shapes of houses, cars, fish, and men using lines, circles, etc.

Description:

The following graphics functions are available for creating two dimensional shapes in C.

line

circle

ellipse

rectangle

draw poly

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.

Declaration: - line(x1, y1, x2, y2); ---line (100,200,300,400);

Circle: Circle function is used to draw a circle with center (x, y) and third parameter specifies the radius of the circle.

Declaration: circle(x, y, r)—circle (100, 200, 25) ;( 25 is radius of circle, (100,100) is center of circle).

Ellipse: Ellipse is used to draw an ellipse (x, y) are coordinates of center of the ellipse, start angle is the starting angle, the dangle is the ending angle, and fifth and sixth parameters specify the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angles should be 0 and 360 respectively.

Usage: ellipse(x, y, start angle, entangle, xradius, radius);--ellipse(100,200,0,360,25,45);((100,200) is center of ellipse, 0 is start angle, 360 is end angle, 25 is x-axis radius, 45 is radius circle).

Rectangle: rectangle function is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of the right bottom corner, bottom specifies the Y-coordinate of right bottom corner.

Syntax: rectangle(left,top,right,bottom);--rectangle(100,200,300,400);

Drawpoly: Drawpoly function is used to draw polygons i.e. triangle, rectangle, pentagon, hexagon etc.

Syntax: drawpoly( num,points );--num indicates number of vertices of polygon. Num = num+1. Example: we will draw a triangle using drawpoly, consider for example the array :-

int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point(320, 150) in array is same as first.

Number of vertices are denoted by num. for any polygon, number of vertices are (num+1). For triangle, number of vertices are 4.

 

i)       Program for creating House shape:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TURBOC3\\BGI"); setcolor(5);

rectangle(60,80,150,200);

rectangle(95,140,120,200);

line(60,80,100,15); line(100,15,150,80);

circle(100,60,10); getch(); closegraph();

}

Output:

ii)     Program for creating simple car shape:

 

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<dos.h> void main()

{

int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); cleardevice();

line( 150, 100, 242, 100);

ellipse(242, 105, 0, 90, 10, 5);

line(150, 100, 120, 150);

line(252, 105, 280, 150);

line(100, 150, 320, 150);

line(100, 150, 100, 200);

line(320, 150, 320, 200);

line(100, 200, 110, 200);

line( 320, 200, 310, 200);

arc(130, 200, 0, 180, 20);

arc( 290, 200, 0, 180, 20);

line( 270, 200, 150, 200);

circle(130, 200, 17);

circle(290, 200, 17); getch();

}

Output:

 

 

 

iii) Program for creating fish:

#include<stdlib.h>

#include<conio.h>

#include<dos.h>

#include<graphics.h>

#include<ctype.h>

void main()

{

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); cleardevice(); ellipse(520,200,30,330,90,30);

circle(450,193,3);

line(430,200,450,200);

line(597,185,630,170);

line(597,215,630,227);

line(630,170,630,227);

line(597,200,630,200);

line(597,192,630,187);

line(597,207,630,213);

line(500,190,540,150);

line(530,190,540,150);

getch();

}

Output:

 

iv) Program for creating man object:

#include<stdio.h>

#include<graphics.h>

#include<conio.h>

void main()

{

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); setcolor(9);

circle(150,150,35);

line(150,185,150,300);

line(150,200,120,230);

line(150,200,180,230);

line(150,300,120,330);

line(150,300,180,330);

outtextxy(230,350,"HI, This is Computer Graphics");

getch();

}

Sample program illustrating the use drawpoly() function:

 

Aim: To draw a triangle using drawpoly() function.

Description:

Using drawpoly (), we can draw any polygon of any number of vertices.

 

Syntax: drawpoly (num, points);--num indicates number of vertices of polygon. Num = num+1.

Example: we will draw a triangle using drawpoly, consider for example the array:- int points[] = { 320, 150, 420, 300, 250, 300, 320, 150};

  • Points array contains coordinates of triangle which are (320, 150), (420, 300) and (250, 300). Note that last point (320, 150) in array is same as
  • Number of vertices is denoted by for any polygon, numbers of vertices are (num+1). For triangle, number of vertices are 4.

 

Program:

#include <graphics.h>

#include <conio.h>

main()

{

}

int gd=DETECT,gm,points[]={320,150,420,300,250,300,320,150};

initgraph(&gd, &gm, "C:\\TurboC3\\BGI"); drawpoly(4, points);

getch();

closegraph();

return 0;

0
Rusma Khadka
Jan 4, 2023
More related questions

Questions Bank

View all Questions