0% found this document useful (0 votes)
54 views

Practical 5

The document contains code for implementing basic 2D geometric transformations - translation, scaling, and rotation - on a triangle using graphics programming in C. For translation, it gets the triangle coordinates and translation vector as input and draws the translated triangle. For scaling, it gets the triangle coordinates and scaling factors along x and y axes and draws the scaled triangle. For rotation, it gets the triangle coordinates and rotation angle, calculates the new coordinates using trigonometric functions, and draws the rotated triangle.

Uploaded by

Sunny singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Practical 5

The document contains code for implementing basic 2D geometric transformations - translation, scaling, and rotation - on a triangle using graphics programming in C. For translation, it gets the triangle coordinates and translation vector as input and draws the translated triangle. For scaling, it gets the triangle coordinates and scaling factors along x and y axes and draws the scaled triangle. For rotation, it gets the triangle coordinates and rotation angle, calculates the new coordinates using trigonometric functions, and draws the rotated triangle.

Uploaded by

Sunny singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Practical 5

Aim:- Write a program to draw a moving car.


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

// Function to draw moving car


void draw_moving_car(void) {

int i, j = 0, gd = DETECT, gm;

// Passed three arguments to initgraph


// function to initialize graphics mode
initgraph(&gd, &gm, "C:\\TC\\BGI");

for (i = 0; i <= 420; i = i + 10) {

// Set color of car as red


setcolor(YELLOW);

// Thease lines for bonnet and


// body of car
line(0 + i, 300, 210 + i, 300);
line(50 + i, 300, 75 + i, 270);
line(75 + i, 270, 150 + i, 270);
line(150 + i, 270, 165 + i, 300);
line(0 + i, 300, 0 + i, 330);
line(210 + i, 300, 210 + i, 330);

// For left wheel of car


circle(65 + i, 330, 15);
circle(65 + i, 330, 2);

// For right wheel of car


circle(145 + i, 330, 15);
circle(145 + i, 330, 2);

// Line left of left wheel


line(0 + i, 330, 50 + i, 330);

// Line middle of both wheel


line(80 + i, 330, 130 + i, 330);

EN20CS301423 Shreyash Jain


// Line right of right wheel
line(210 + i, 330, 160 + i, 330);

delay(50);

// To erase previous drawn car


// use cleardevice() function
cleardevice();
}
getch();
closegraph();
}
// Driver code
int main()
{
draw_moving_car();
return 0;
}

EN20CS301423 Shreyash Jain


Practical 6
Aim:- Write a program to draw a bouncing ball.
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>

int main() {
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm, "C:\\TC\\BGI");

/* get mid positions in x and y-axis */


x = getmaxx()/2;
y = 60;

while (!kbhit()) {
if(y >= getmaxy()-60 || y <= 60)
flag = !flag;
/* draws the gray board */
setcolor(RED);
setfillstyle(SOLID_FILL, YELLOW);
circle(x, y, 60);
floodfill(x, y, RED);

/* delay for 50 milli seconds */


delay(50);

/* clears screen */
cleardevice();
if(flag){
y = y + 5;
} else {
y = y - 5;
}
}

getch();
closegraph();
return 0;
}

EN20CS301423 Shreyash Jain


Practical 7
EN20CS301423 Shreyash Jain
Aim:- Write a program to draw a human body moving in rain with the
umbrella.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>

void main(){
int gd=DETECT,gm;
int i,x,y,j;
initgraph(&gd,&gm,"C:\\TC\\BGI");

// man
for(j=1;j<600;j=j+5)
{
line(0,400,800,400);
circle(30+j,280,20); //head
line(30+j,300,30+j,350); //body
line(30+j,330,70+j,330); //hand
if(j%2==0){
line(30+j,350,25+j,400); //left leg
line(30+j,350,10+j,400); // right
}
else{
line(30+j,350,35+j,400); //transition
delay(20);
}
//umbrela
line(70+j,250,70+j,330);
pieslice(70+j,250,180,0,80);
// rain
for(i=0;i<300;i++)
{
x=random(800);
y=random(800);
outtextxy(x,y,"/");
}
delay(170);
cleardevice();
}
getch();
closegraph();
}
EN20CS301423 Shreyash Jain
Practical 8

EN20CS301423 Shreyash Jain


Aim:- Write a program to draw a line using DDA Algorithm.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 50 , y0 = 600, x1 = 500, y1 = 200;
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, BLUE);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}

EN20CS301423 Shreyash Jain


Practical 9
EN20CS301423 Shreyash Jain
Aim:- Write a program to draw a line using Bresenham’s Algorithm.
#include<stdio.h>
#include<graphics.h>
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
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()
{
int gd=DETECT, gm, error, x0, y0, x1, y1;
initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
return 0;
}

EN20CS301423 Shreyash Jain


EN20CS301423 Shreyash Jain
Practical 10
Aim:- Write a program to draw a circle using Midpoint Algorithm.
#include<stdio.h>
#include<graphics.h>

void drawcircle(int x0, int y0, int radius)


{
int x = radius;
int y = 0;
int err = 0;

while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);

if (err <= 0)
{
y += 1;
err += 2*y + 1;
}

if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}

int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter radius of circle: ");


scanf("%d", &r);
EN20CS301423 Shreyash Jain
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);

return 0;
}

Practical 11
EN20CS301423 Shreyash Jain
Aim:- Write a program to draw a circle using Bresenham’s Algorithm.
#include <stdio.h>
#include <dos.h>
#include <graphics.h>

// Function to put pixels


// at subsequence points
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

// Function for circle-generation


// using Bresenham's algorithm
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
drawCircle(xc, yc, x, y);
while (y >= x)
{
// for each pixel we will
// draw all eight pixels

x++;

// check for decision parameter


// and correspondingly
// update d, x, y
if (d > 0)
{
y--;
d = d + 4 * (x - y) + 10;
}
else
d = d + 4 * x + 6;

EN20CS301423 Shreyash Jain


drawCircle(xc, yc, x, y);
delay(50);
}
}

// Driver code
int main()
{
int xc = 200, yc = 200, r = 80;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI"); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}

Practical 12
EN20CS301423 Shreyash Jain
Aim:- Write a program to implement 3 basic transformation.
Translation:-
/* translation */
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,tx,ty;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\n Please enter first coordinate of the triangle= ");
scanf("%d %d", &x,&y);
printf("\n Enter second coordinate of the trinagle = ");
scanf("%d %d",&x1,&y1);
printf("\n Enter third coordinate of the triangle = ");
scanf("%d %d",&x2,&y2);
printf("\n\t\t********** TRIANGLE before & after translation ***********");
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
printf("\n Now enter the translation vector = ");
scanf("%d %d",&tx,&ty);

setcolor(RED);
line(x+tx,y+ty,x1+tx,y1+ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
line(x2+tx,y2+ty,x+tx,y+ty);
getch();
closegraph();
}

EN20CS301423 Shreyash Jain


Scaling :-
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main(){
int x,y,x1,y1,x2,y2;
int scl_fctr_x,scl_fctr_y;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\BGI");
printf("\t\t\t********** Scaling ***********\n");
printf("\n\t\t\t Please enter first coordinate of Triangle = ");
scanf("%d %d",&x,&y);
printf("\n\t\t\t Please enter second coordinate of Triangle = ");
scanf("%d %d",&x1,&y1);
printf("\n\t\t\t Please enter third coordinate of Triangle = ");
scanf("%d %d",&x2,&y2);
line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
EN20CS301423 Shreyash Jain
printf("\n\t\t\t Now Enter Scaling factor x and y = ");
scanf("%d %d",&scl_fctr_x,&scl_fctr_y);
x = x* scl_fctr_x;
x1 = x1* scl_fctr_x;
x2 = x2* scl_fctr_x;
y = y* scl_fctr_y;
y1 = y1* scl_fctr_y;
y2= y2 * scl_fctr_y ;

line(x,y,x1,y1);
line(x1,y1,x2,y2);
line(x2,y2,x,y);
getch();
closegraph();
}

Rotation :-

#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
int gd=0,gm,x1,y1,x2,y2,x3,y3;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setcolor(RED);
printf("Enter coordinates of triangle: ");
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2, &x3, &y3);
setbkcolor(WHITE);
cleardevice();
EN20CS301423 Shreyash Jain
line(x1,y1,x2,y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
setbkcolor(BLACK);
printf("Enter rotation angle: ");
scanf("%lf", &angle);
setbkcolor(WHITE);
c = cos(angle *M_PI/180);
s = sin(angle *M_PI/180);
x1 = floor(x1 * c + y1 * s);
y1 = floor(-x1 * s + y1 * c);
x2 = floor(x2 * c + y2 * s);
y2 = floor(-x2 * s + y2 * c);
x3 = floor(x3 * c + y3 * s);
y3 = floor(-x3 * s + y3 * c);
cleardevice();
line(x1, y1 ,x2, y2);
line(x2,y2, x3,y3);
line(x3, y3, x1, y1);
getch();
closegraph();
return 0;
}

EN20CS301423 Shreyash Jain


EN20CS301423 Shreyash Jain
Practical 13
Aim:- Write a program to implement flood fill algorithm.
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
 
void floodFill(int x,int y,int oldcolor,int newcolor)
{
if(getpixel(x,y) == oldcolor)
{
putpixel(x,y,newcolor);
floodFill(x+1,y,oldcolor,newcolor);
floodFill(x,y+1,oldcolor,newcolor);
floodFill(x-1,y,oldcolor,newcolor);
floodFill(x,y-1,oldcolor,newcolor);
}
}
//getpixel(x,y) gives the color of specified pixel
 
int main()
{
int gm,gd=DETECT,radius;
int x,y;
printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle\n");
scanf("%d",&radius);
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(x,y,radius);
floodFill(x,y,0,15);
delay(5000);
closegraph();
return 0;
}

EN20CS301423 Shreyash Jain


EN20CS301423 Shreyash Jain
Practical 14
Aim:- Write a program to implement boundary fill algorithm.
#include<stdio.h>
#include<graphics.h>
#include<dos.h>

void boundaryfill(int x,int y,int f_color,int b_color)


{
if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)
{
putpixel(x,y,f_color);
boundaryfill(x+1,y,f_color,b_color);
boundaryfill(x,y+1,f_color,b_color);
boundaryfill(x-1,y,f_color,b_color);
boundaryfill(x,y-1,f_color,b_color);
}
}
//getpixel(x,y) gives the color of specified pixel

int main()
{
int gm,gd=DETECT,radius;
int x,y;
printf("Enter x and y positions for circle\n");
scanf("%d%d",&x,&y);
printf("Enter radius of circle\n");
scanf("%d",&radius);
initgraph(&gd,&gm,"c:\\tc\\bgi");
circle(x,y,radius);
boundaryfill(x,y,4,15);
delay(5000);
closegraph();
return 0;
}

EN20CS301423 Shreyash Jain


EN20CS301423 Shreyash Jain

You might also like