You are on page 1of 7

COMPUTER GRAPHICS AND MULTEMEDIA

(DCO-511)
Practical Number: 8

Program name: write a program in c to


1) Translate any 2-D object.
2) Rotate any 2-D object abject along any arbitrary chosen pivoted point.
3) Scale any 2-D object with respect to any arbitrary point.

Submitted by:
SAUD AHMAD KHAN
Roll. No.
16-DCS-055

Diploma in Computer Engineering- IV Semester

Computer Engineering Section


University Polytechnic, Faculty of Engineering and Technology
Jamia Millia Islamia (A Central University)
New Delhi-110025
Session 2017-2018
TRANSLATION:

A translation is applied to an object by repositioning it along a straight line path from one
coordinate point to another. We translate a two dimensional point by adding translation distance,
tx and ty, to the original coordinate position to (x,y) to move the point to a new position ( x ' , y').

ALGORITHM:

1. Start
2. Initialize the graphics mode.
3. Construct a 2D object
4. Get the translation value tx, ty
5. Move the 2d object with tx, ty (x’=x+tx,y’=y+ty)
6. Plot (x’,y’)

SOURCE CODE:
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y;
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("Enter translation co-ordinates ");
printf("x,y");
scanf("%d%d",&x,&y);
x1=x1+x;
y1=y1+y;
x2=x2+x;
y2=y2+y;
printf("Line after translation");
line(x1,y1,x2,y2);
getch();
closegraph();
}
//OUTPUT:
ROTATION:

A two dimensional rotation is applied to an abject by reapportioning it along the circular path in
the xy plane. To generate a rotation, we specify the rotation angle α and the position (xr,yr) of the
rotation point (or pivot point) about which the abject is to be rotated.

ALGORITHM:

1. Start
2. Initialize the graphics mode.
3. Construct a 2D object
4. Get the Rotation angle
5. Rotate the object by the angle ф
x’=x cos ф - y sin ф
y’=x sin ф - y cosф
6. Plot (x’,y’)

SOURCE CODE:

#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
void main()
{
int graphdriver=DETECT,graphmode,errorcode;
int i;
int x2,y2,x1,y1,x,y,xn,yn;
double r11,r12,r21,r22,th;
clrscr();
printf("Enter the 2 line end points:");
printf("x1,y1,x2,y2");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");
line(x1,y1,x2,y2);
printf("\n\n\n[ Enter the angle");
scanf("%lf",&th);
r11=cos((th*3.1428)/180);
r12=sin((th*3.1428)/180);
r21=(-sin((th*3.1428)/180));
r22=cos((th*3.1428)/180);
//printf("%lf %lf %lf %lf",r11,r12,r21,r22);
xn=((x2*r11)-(y2*r12));
yn=((x2*r12)+(y2*r11));
line(x1,y1,xn,yn);
getch();
closegraph();
}

//OUTPUT:
SCALING:

A scaling transformation alters the size of an object. The operation can be carried out for polygons
by multiplying the coordinate value (x,y) of each vertex by scaling factors sx, sy to produce the
transformed the coordinate (x', y'):

ALGORITHM:

1. Start
2. Initialize the graphics mode.
3. Construct a 2D object
4. Get the scaling value Sx,Sy
5. Resize the object with Sx,Sy (x’=x*Sx,y’=y*Sy)
6. Plot (x’,y’)

SOURCE CODE:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main() {
int gdriver = DETECT, gmode;
int point[] = {70,70,70,80,100,80,70,70};
int i,sx,sy;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
drawpoly(4,point);
printf("\n\nEnter the scaling factor of X - axis : ");
scanf("%d",&sx);
printf("Enter the scaling factor of Y - axis : ");
scanf("%d",&sy);
for(i=0;i<8;i++)
{
if(i%2==0)
point[i] = point[i] * sx;
else
point[i] = point[i] * sy;
}
drawpoly(4,point);
getch();
closegraph();
}
//OUTPUT:

You might also like