CG Assignment-3: Meenu Maria Giby U18CO106
CG Assignment-3: Meenu Maria Giby U18CO106
Assignment-3
Meenu Maria Giby
U18CO106
1. Write a program to design a House and color it using pre-defined functions of graphics.h.
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
setfillstyle(SLASH_FILL, BLUE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
getch();
closegraph();
return 0;
2. Write a program to draw a Kite and color it using pre-defined functions of graphics.h.
#include<graphics.h>
#include<conio.h>
int main()
int a,b,gd=DETECT,gm,i;
initgraph(&gd,&gm,"X:\\TC\\BGI");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380);
setcolor(4);
getch();
closegraph();
3. Write a program for drawing India's National Flag and Color it properly using pre-defined functions of
graphics.h.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int main()
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setfillstyle(SOLID_FILL,RED);
rectangle(110,40,220,58);
floodfill(111,43,15);
setfillstyle(SOLID_FILL,15);
rectangle(110,58,220,78);
floodfill(111,59,15);
setfillstyle(SOLID_FILL,GREEN);
rectangle(110,78,220,98);
floodfill(111,79,15);
//Ashok chakra
//
a=160;
b=68;
r=13;
setcolor(BLUE);
circle(a,b,r);
for(i=0;i<=360;i=i+25)
x=r*cos(i*3.14/180);
y=r*sin(i*3.14/180);
line(a,b,a+x,b-y);
getch();
return 0;
closegraph();
4. Write a program for displaying a Moving Car using pre-defined functions of graphics.h.
#include <graphics.h>
#include <stdio.h>
void draw_moving_car(void) {
setcolor(RED);
// body of car
delay(100);
setcolor(BLACK);
getch();
closegraph();
// Driver code
int main()
draw_moving_car();
return 0;
I. DDA Algorithm
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
setbkcolor(WHITE);
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
steps = dx;
else
steps = dy;
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
getch();
closegraph();
}
II. Bresenham's Line Algorithm.
#include<stdio.h>
#include<graphics.h>
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
if(p>=0)
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
else
putpixel(x,y,7);
p=p+2*dy;}
x=x+1;
}
int main()
return 0;