0% found this document useful (0 votes)
255 views25 pages

Write A Program To Perform Scaling of A Line, A Triangle and A Rectangle

The document contains C++ programs to demonstrate: 1) Scaling of lines, triangles, and rectangles using 2D transformations. Coordinates are scaled by multiplying with scaling factors. 2) Translation of points, lines, and rectangles. Coordinates are translated by adding translation offsets. 3) Implementation of Bresenham's line drawing algorithm to draw lines on a graph by deciding pixel positions based on decision parameter. 4) Implementation of Bresenham's circle drawing algorithm to draw circles on a graph by tracking the current pixel position in each of the 8 octants of the circle.

Uploaded by

alishasingh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
255 views25 pages

Write A Program To Perform Scaling of A Line, A Triangle and A Rectangle

The document contains C++ programs to demonstrate: 1) Scaling of lines, triangles, and rectangles using 2D transformations. Coordinates are scaled by multiplying with scaling factors. 2) Translation of points, lines, and rectangles. Coordinates are translated by adding translation offsets. 3) Implementation of Bresenham's line drawing algorithm to draw lines on a graph by deciding pixel positions based on decision parameter. 4) Implementation of Bresenham's circle drawing algorithm to draw circles on a graph by tracking the current pixel position in each of the 8 octants of the circle.

Uploaded by

alishasingh
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 25

0492073107

Write a program to perform Scaling of a line, a triangle and a


rectangle

a.) Scaling of a line


#include<stdio.h>

#include<process.h>

#include <graphics.h>

#include <iostream.h>

#include <conio.h>

void main()

// request auto detection

int gdriver = DETECT, gmode;

int pixel[20][20];

int x1,y1,x2,y2;//initial co-ordinates

float sx,sy;//scaling factors

static float p11[3],p22[3];

clrscr();

cout<<"Enter the coordinates(x1,y1,x2,y2) of the line:\n";

cin>>x1>>y1>>x2>>y2;

int p1[3]={x1,y1,1};
0492073107

int p2[3]={x2,y2,1};

flushall();

cout<<"Enter the scaling factors:\n";

cin>>sx>>sy;

int T[3][3]= {

{sx,0,0},

{0,sy,0},

{0,0,1},

};

//scaling being performed

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

p11[i]+=T[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=T[i][j]*p2[j];

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

// object before scaling

line(p1[0],p1[1],p2[0],p2[1]);
0492073107

setcolor(5);

// object after scaling

line(p11[0],p11[1],p22[0],p22[1]);

getch();

closegraph();

}
0492073107

b.) Scaling of a Triangle

#include <graphics.h>

#include <stdlib.h>

#include <stdio.h>

#include <iostream.h>

#include <conio.h>

#include <math.h>

void main()

/* request auto detection */

int gdriver = DETECT, gmode;

int x1,y1,x2,y2,x3,y3;

float Sx,Sy;

static float p11[3],p22[3],p33[3];

cout<<"please enter the co-ordinates(x1,y1,x2,y2,x3,y3) of the triangle to be scaled:";

cin>>x1>>y1>>x2>>y2>>x3>>y3;

int p1[3]={x1,y1,1};

int p2[3]={x2,y2,1};

int p3[3]={x3,y3,1};

flushall();

cout<<"\nplease enter the scaling factors(Sx,Sy):";

cin>>Sx>>Sy;
0492073107

float T[3][3]={{Sx,0,0},

{0,Sy,0},

{0,0,1},

};

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

p11[i]+=T[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=T[i][j]*p2[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p33[i]+=T[i][j]*p3[j];

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

line(p1[0],p1[1],p2[0],p2[1]);

line(p2[0],p2[1],p3[0],p3[1]);

line(p1[0],p1[1],p3[0],p3[1]);
0492073107

setcolor(5);

line(p11[0],p11[1],p22[0],p22[1]);

line(p33[0],p33[1],p22[0],p22[1]);

line(p11[0],p11[1],p33[0],p33[1]);

getch();

closegraph();

}
0492073107

Write a program to perform Translation of a line, a point and a


rectangle

//WRITE A PROGRAM TO DEMONSTRATE TRANSLATION OF OBJECTS(S)

//DATE:

#include<process.h>

#include <graphics.h>

#include <iostream.h>

#include <conio.h>

void main()

// request auto detection

int gdriver = DETECT, gmode;

int pixel[20][20];

int x1,y1,x2,y2,x3,y3,x4,y4;//initial co-ordinates

int tx,ty;//translation factors

static int p11[3],p22[3],p33[3],p44[3],p1[3],p2[3],p3[3],p4[3];

int choice,i,j;

int T[3][3]= {

{1,0,tx},

{0,1,ty},

{0,0,1},

};
0492073107

clrscr();

cout<<"--------------------TRANSLATION MENU----------------------\n";

cout<<"1. Point Translation\n";

cout<<"2. Line Translation\n";

cout<<"3. Rectangle Translation\n";

cout<<"4. Exit\n";

cout<<"Enter the choice\n";

cin>>choice;

switch(choice)

case 1 : cout<<"Enter the coordinates(x1,y1) of the point:\n";

cin>>x1>>y1;

p1[0]=x1;

p1[1]=y1;

p1[2]=1;

cout<<"Enter the translation factors:\n";

cin>>tx>>ty;

//translation being performed

for( i=0;i<3;i++)

for( j=0;j<3;j++)

p11[i]+=T[i][j]*p1[j];

// initialize graphics and local variables


0492073107

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

//object before translation

setcolor(6);

putpixel(x1,y1,1);

setcolor(5);

//object after translation

putpixel(p11[0],p11[1],1);

getch();

closegraph();

break;

case 2 : cout<<"Enter the coordinates(x1,y1,x2,y2) of the line:\n";

cin>>x1>>y1>>x2>>y2;

p1[0]=x1;

p1[1]=y1;

p1[2]=1;

p2[0]=x2;

p2[1]=y2;

p2[2]=1;

cout<<"Enter the translation factors:\n";

cin>>tx>>ty;

//translation being performed

for( i=0;i<3;i++)

for( j=0;j<3;j++)
0492073107

p11[i]+=T[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=T[i][j]*p2[j];

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

// object before translation

line(p1[0],p1[1],p2[0],p2[1]);

setcolor(5);

// object after translation

line(p11[0],p11[1],p22[0],p22[1]);

getch();

closegraph();

break;

case 3 : cout<<"please enter the co-ordinates(x1,y1,x2,y2,x3,y3,x4,y4) of the rectangle to be


translated:";

cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;

//initial co-ordinates matrices

p1[0]=x1;

p1[1]=y1;

p1[2]=1;
0492073107

p2[0]=x2;

p2[1]=y2;

p2[2]=1;

p3[0]=x3;

p3[1]=y3;

p3[2]=1;

p4[0]=x4;

p4[1]=y4;

p4[2]=1;

cout<<"\nplease enter the translation factors:";

cin>>tx>>ty;

//translation being performed

for( i=0;i<3;i++)

for( j=0;j<3;j++)

p11[i]+=T[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=T[i][j]*p2[j];
0492073107

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p33[i]+=T[i][j]*p3[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p44[i]+=T[i][j]*p4[j];

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

//object before translation

line(p1[0],p1[1],p2[0],p2[1]);

line(p3[0],p3[1],p2[0],p2[1]);

line(p1[0],p1[1],p4[0],p4[1]);

line(p3[0],p3[1],p4[0],p4[1]);

setcolor(5);

//object after translation

line(p11[0],p11[1],p22[0],p22[1]);

line(p33[0],p33[1],p22[0],p22[1]);

line(p11[0],p11[1],p44[0],p44[1]);
0492073107

line(p33[0],p33[1],p44[0],p44[1]);

getch();

closegraph();

break;

case 4 : exit(0);

default: cout<<"\n Enter a valid choice";

getch();

closegraph();

}
0492073107

Write a program to implement Bresenham’s line drawing algorithm.

//WRITE A PROGRAM TO IMPLEMENT BRESENHAM’S LINE DRAWING ALGO

#include<stdio.h>

#include<math.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

#include<dos.h>

void main()

int i,x1,y1,x2,y2,xend;

float x,y,dx,dy,p;

int gd=DETECT,gm;

printf("Enter the first co ordinate of the line : \n");

scanf("%d %d",&x1,&y1);

printf("Enter the second co ordinate of the line : \n");

scanf("%d %d",&x2,&y2);

clrscr();

initgraph(&gd,&gm,"c:\\tc\\bgi");

setcolor(10);

rectangle(0,0,636,476);

line(320,0,320,476);

line(0,240,636,240);

setcolor(5);
0492073107

outtextxy(324,244,"(0,0)");

outtextxy(524,244,"x axis-->");

outtextxy(324,42,"y axis");

outtextxy(20,20,"Bres line drawing algorithms (|m|<1)");

outtextxy(20,40,"put 1 divide=20 pixel ");

outtextxy(x1+320,240-y1,"(x1,y1)");

outtextxy(x2+320,240-y2,"(x2,y2)");

for(i=0;i<640;i+=20)

line(i,236,i,244);

for(i=0;i<480;i+=20)

line(316,i,324,i);

dx=abs(x2-x1);

dy=abs(y2-y1);

p=2*dy-dx;

if(x1>x2)

x=x2;

y=y2;

xend=x1;

else

x=x1;

y=y1;

xend=x2;
0492073107

putpixel(x+320,240-y,2);

setcolor(4);

while(x<xend)

x=x+1;

if(p<0)

p=p+2*dy;

else

y=y+1;

p=p+2*(dy-dx);

putpixel(x+320,240-y,2);

getch();

closegraph();

}
0492073107

Write a program to implement Bresenham’s circle drawing algorithm.

//WRITE A PROGRAM TO DEMONSTRATE BRESENHAM'S CIRCLE ALGORITHM

#include<iostream.h>

#include <graphics.h>

#include<conio.h>

//function that demonstrates bresenham's circle algorithm

void bca_circle(int x,int y,int r)

int pk=3-2*r,xa=0,ya=r;//decision factor,intial x and y

//loop runs for only 1 of the 8 equal parts of a circle

while(xa<=ya)

putpixel(xa+x,ya+y,5);

putpixel(ya+x,xa+y,5);

putpixel(-xa+x,ya+y,5);

putpixel(-ya+x,xa+y,5);

putpixel(xa+x,-ya+y,5);

putpixel(-xa+x,-ya+y,5);

putpixel(ya+x,-xa+y,5);

putpixel(-ya+x,-xa+y,5);

if(pk<0)

pk+=4*xa+6;
0492073107

else

pk+=4*(xa-ya)+10;

ya--;

xa++;

void main()

// request auto detection

int gdriver = DETECT, gmode;

int x,y,r;

cout<<"Enter the centre of the circle(x&y)\n";

cin>>x>>y;

cout<<"Enter the radius of the circle r\n";

cin>>r;

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

bca_circle(x,y,r);//bresenham's circle
0492073107

getch();

closegraph();

}
0492073107

Write a program to perform Shearing of a rectangle

//WRITE A PROGRAM TO DEMONSTRATE SHEARING OF RECTANGLE

//DATE:

#include<stdio.h>

#include<process.h>

#include <graphics.h>

#include <iostream.h>

#include <conio.h>

void main()

// request auto detection

int gdriver = DETECT, gmode;

int x1,y1,x2,y2,x3,y3,x4,y4;//initial co-ordinates

float a,b;//translation factors

static float p11[3],p22[3],p33[3],p44[3];

cout<<"please enter the co-ordinates(x1,y1,x2,y2,x3,y3,x4,y4) of the rectangle to be


translated:";

cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;

//initial co-ordinates matrices

int p1[3]={x1,y1,1};

int p2[3]={x2,y2,1};

int p3[3]={x3,y3,1};
0492073107

int p4[3]={x4,y4,1};

flushall();

cout<<"\nplease enter the shearing factors:";

cin>>a>>b;

int T[3][3]= {

{1,b,0},

{a,1,0},

{0,0,1},

};

//shearing being performed

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

p11[i]+=T[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=T[i][j]*p2[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

{
0492073107

p33[i]+=T[i][j]*p3[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p44[i]+=T[i][j]*p4[j];

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

//object before shearing

line(p1[0],p1[1],p2[0],p2[1]);

line(p3[0],p3[1],p2[0],p2[1]);

line(p1[0],p1[1],p4[0],p4[1]);

line(p3[0],p3[1],p4[0],p4[1]);

setcolor(5);

//object after shearing

line(p11[0],p11[1],p22[0],p22[1]);

line(p33[0],p33[1],p22[0],p22[1]);

line(p11[0],p11[1],p44[0],p44[1]);

line(p33[0],p33[1],p44[0],p44[1]);

getch();

closegraph();

}
0492073107

Write a program to perform Rotation of a triangle about origin.

//WRITE A PROGRAM TO DEMONSTRATE ROTATION OF OBJECT(S)

#include <graphics.h>

#include <stdio.h>

#include <iostream.h>

#include <conio.h>

#include <math.h>

void main()

// request auto detection

int gdriver = DETECT, gmode;

int x1,y1,x2,y2,x3,y3;//initial co-ordinates

int A;//rotation angle

static float p11[3],p22[3],p33[3];//final co-ordinates matrices

cout<<"please enter the co-ordinates(x1,y1,x2,y2,x3,y3) of the triangle to be rotated:";

cin>>x1>>y1>>x2>>y2>>x3>>y3;

//initial co-ordinates matrices

int p1[3]={x1,y1,1};

int p2[3]={x2,y2,1};

int p3[3]={x3,y3,1};

flushall();

cout<<"\nplease enter the rotation angle(in degrees):";


0492073107

cin>>A;

//rotation matrix

float R[3][3]={{cos(A*3.14/180),-sin(A*3.14/180),0},

{sin(A*3.14/180),cos(A*3.14/180),0},

{0,0,1},

};

//rotation being performed

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

p11[i]+=R[i][j]*p1[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p22[i]+=R[i][j]*p2[j];

for(i=0;i<3;i++)

for(j=0;j<3;j++)

p33[i]+=R[i][j]*p3[j];

// initialize graphics and local variables

initgraph(&gdriver, &gmode, "c:\\tc\\bgi");


0492073107

//object before rotation

line(p1[0],p1[1],p2[0],p2[1]);

line(p2[0],p2[1],p3[0],p3[1]);

line(p1[0],p1[1],p3[0],p3[1]);

setcolor(5);

//object after rotation

line(p11[0],p11[1],p22[0],p22[1]);

line(p33[0],p33[1],p22[0],p22[1]);

line(p11[0],p11[1],p33[0],p33[1]);

getch();

closegraph();

You might also like