CHANDIGARH UNIVERSITY Institute of Distance and Online Learning SLM Development Committee Prof. (Dr.) H.B. Raghvendra Vice- Chancellor, Chandigarh University, Gharuan, Punjab:Chairperson Prof. (Dr.) S.S. Sehgal Registrar Prof. (Dr.) B. Priestly Shan Dean of Academic Affairs Dr. Nitya Prakash Director – IDOL Dr. Gurpreet Singh Associate Director –IDOL Advisors& Members of CIQA –IDOL Prof. (Dr.) Bharat Bhushan, Director – IGNOU Prof. (Dr.) Majulika Srivastava, Director – CIQA, IGNOU Editorial Committee Prof. (Dr) Nilesh Arora Dr. Ashita Chadha University School of Business University Institute of Liberal Arts Dr. Inderpreet Kaur Prof. Manish University Institute of Teacher Training & University Institute of Tourism & Hotel Management Research Dr. Manisha Malhotra Dr. Nitin Pathak University Institute of Computing University School of Business © No part of this publication should be reproduced, stored in a retrieval system, or transmitted in any formor by any means, electronic, mechanical, photocopying, recording and/or otherwise without the prior written permission of the authors and the publisher. SLM SPECIALLY PREPARED FOR CU IDOL STUDENTS
COMPUTER GRAPHICS PRACTICAL Course Code: BCA147 Credits: 1 Course Description This course on principles of computer graphics will consider both 2D and 3D graphics. The focus will be on raster scan graphics including line and circle drawing, polygon filling, anti- aliasing algorithms, clipping, hidden line and hidden surface algorithms. Course Objectives This course is designed to provide a comprehensive introduction to computer graphics techniques. The course will make the students able to carry out computer graphics and to evaluate software and hardware for that use. The course is a foundation for a master´s thesis within computer graphics. Provide an understanding of mapping from a world coordinates to device coordinates, clipping, and projections. EXPERIMENT 1-WAP ON DDA LINE DRAWING ALGORITHM DDA stands for Digital Differential Analyzer. It is an incremental method of scan conversion of line. In this method calculation is performed at each step but by using results of previous steps. Suppose at step i, the pixels is (xi,yi) The line of equation for step i yi=mxi+b......................equation 1
Next value will be yi+1=mxi+1+b.................equation 2 m= yi+1-yi=∆y.......................equation 3 yi+1-xi=∆x......................equation 4 yi+1=yi+∆y ∆y=m∆x yi+1=yi+m∆x ∆x=∆y/m xi+1=xi+∆x xi+1=xi+∆y/m Case1: When |M|<1 then (assume that x1<x2) x= x1,y=y1 set ∆x=1 yi+1=y1+m, x=x+1 Until x = x2</x Case2: When |M|<1 then (assume that y1<y2) x= x1,y=y1 set ∆y=1 xi+1= , y=y+1 Until y → y2</y Program #include<graphics.h> #include<conio.h> #include<stdio.h> void main() { intgd = DETECT ,gm, i; float x, y,dx,dy,steps; int x0, x1, y0, y1; initgraph(&gd, &gm, \"C:\\\\TC\\\\BGI\"); setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300; 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(); } Output:
EXPERIMENT 2-WAP ON BRESENHAM’S LINE DRAWING ALGORITHM Algorithm: Step1: Start Algorithm Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy Step3: Enter value of x1,y1,x2,y2 Where x1,y1are coordinates of starting point And x2,y2 are coordinates of Ending point Step4: Calculate dx = x2-x1 Calculate dy = y2-y1 Calculate i1=2*dy Calculate i2=2*(dy-dx) Calculate d=i1-dx Step5: Consider (x, y) as starting point and xendas maximum possible value of x. If dx < 0 Then x = x2 y = y2 xend=x1 If dx > 0 Then x = x1 y = y1 xend=x2 Step6: Generate point at (x,y)coordinates. Step7: Check if whole line is generated.
If x > = xend Stop. Step8: Calculate co-ordinates of the next pixel If d < 0 Then d = d + i1 If d ≥ 0 Then d = d + i2 Increment y = y + 1 Step9: Increment x = x + 1 Step10: Draw a point of latest (x, y) coordinates Step11: Go to step 7 Step12: End of Algorithm Program #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 gdriver=DETECT, gmode, error, x0, y0, x1, y1; initgraph(&gdriver, &gmode, \"c:\\\\turboc3\\\\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; } Output:
EXPERIMENT 3-WAP ON MIDPOINT CIRCLE DRAWING Program #include <graphics.h> #include <stdlib.h> #include <math.h> #include <stdio.h> #include <conio.h> #include <iostream.h> class bresen { float x, y,a, b, r, p; public: void get (); void cal (); }; void main () { bresen b; b.get (); b.cal (); getch (); } Void bresen :: get () { cout<<\"ENTER CENTER AND RADIUS\"; cout<< \"ENTER (a, b)\"; cin>>a>>b;
cout<<\"ENTER r\"; cin>>r; } void bresen ::cal () { /* request auto detection */ int gdriver = DETECT,gmode, errorcode; int midx, midy, i; /* initialize graphics and local variables */ initgraph (&gdriver, &gmode, \" \"); /* read result of initialization */ errorcode = graphresult (); if (errorcode ! = grOK) /*an error occurred */ { printf(\"Graphics error: %s \\n\", grapherrormsg (errorcode); printf (\"Press any key to halt:\"); getch (); exit (1); /* terminate with an error code */ } x=0; y=r; putpixel (a, b+r, RED); putpixel (a, b-r, RED); putpixel (a-r, b, RED); putpixel (a+r, b, RED); p=5/4)-r; while (x<=y) { If (p<0) p+= (4*x)+6; else { p+=(2*(x-y))+5;
y--; } x++; putpixel (a+x, b+y, RED); putpixel (a-x, b+y, RED); putpixel (a+x, b-y, RED); putpixel (a+x, b-y, RED); putpixel (a+x, b+y, RED); putpixel (a+x, b-y, RED); putpixel (a-x, b+y, RED); putpixel (a-x, b-y, RED); } } Output:
EXPERIMENT 4-WAP ON BRESENHAM’S CIRCLE DRAWING. Algorithm: Step1: Start Algorithm Step2: Declare p, q, x, y, r, d variables p, q are coordinates of the center of the circle r is the radius of the circle Step3: Enter the value of r Step4: Calculate d = 3 - 2r Step5: Initialize x=0 &nbsy= r Step6: Check if the whole circle is scan converted If x > = y Stop Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p, q). Current active pixel is (x, y). putpixel (x+p, y+q) putpixel (y+p, x+q) putpixel (-y+p, x+q) putpixel (-x+p, y+q) putpixel (-x+p, -y+q) putpixel (-y+p, -x+q) putpixel (y+p, -x+q)
putpixel (x+p, -y-q) Step8: Find location of next pixels to be scanned If d < 0 then d = d + 4x + 6 increment x = x + 1 If d ≥ 0 then d = d + 4 (x - y) + 10 increment x = x + 1 decrement y = y - 1 Step9: Go to step 6 Step10: Stop Algorithm Program #include <graphics.h> #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <math.h> void EightWaySymmetricPlot(int xc,int yc,int x,int y) { putpixel(x+xc,y+yc,RED); putpixel(x+xc,-y+yc,YELLOW); putpixel(-x+xc,-y+yc,GREEN); putpixel(-x+xc,y+yc,YELLOW); putpixel(y+xc,x+yc,12); putpixel(y+xc,-x+yc,14); putpixel(-y+xc,-x+yc,15); putpixel(-y+xc,x+yc,6); }
void BresenhamCircle(int xc,int yc,int r) { int x=0,y=r,d=3-(2*r); EightWaySymmetricPlot(xc,yc,x,y); while(x<=y) { if(d<=0) { d=d+(4*x)+6; } else { d=d+(4*x)-(4*y)+10; y=y-1; } x=x+1; EightWaySymmetricPlot(xc,yc,x,y); } } int main(void) { /* request auto detection */ int xc,yc,r,gdriver = DETECT, gmode, errorcode; /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, \"C:\\\\TURBOC3\\\\BGI\"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ {
printf(\"Graphics error: %s\\n\", grapherrormsg(errorcode)); printf(\"Press any key to halt:\"); getch(); exit(1); /* terminate with an error code */ } printf(\"Enter the values of xc and yc :\"); scanf(\"%d%d\",&xc,&yc); printf(\"Enter the value of radius :\"); scanf(\"%d\",&r); BresenhamCircle(xc,yc,r); getch(); closegraph(); return 0; } Output:
EXPERIMENT 5-WAP ON MID-POINT ELLIPSE DRAWING Algorithm: int x=0, y=b; [starting point] int fx=0, fy=2a2 b [initial partial derivatives] int p = b2-a2 b+a2/4 while (fx<=\"\" 1=\"\" {=\"\" set=\"\" pixel=\"\" (x,=\"\" y)=\"\" x++;=\"\" fx=\"fx\" +=\"\" 2b2; if (p<0) p = p + fx +b2; else { y--; fy=fy-2a2 p = p + fx +b2-fy; } } Setpixel (x, y); p=b2(x+0.5)2+ a2 (y-1)2- a2 b2 while (y>0) { y--; fy=fy-2a2; if (p>=0) p=p-fy+a2
else { x++; fx=fx+2b2 p=p+fx-fy+a2; } Setpixel (x,y); } Program: #include <graphics.h> #include <stdlib.h> #include <math.h> #include <stdio.h> #include <conio.h> #include <iostream.h> class bresen { float x,y,a, b,r,p,h,k,p1,p2; public: void get (); void cal (); }; void main () { bresen b; b.get (); b.cal ();
getch (); } void bresen :: get () { cout<<\"\\n ENTER CENTER OF ELLIPSE\"; cout<<\"\\n ENTER (h, k) \"; cin>>h>>k; cout<<\"\\n ENTER LENGTH OF MAJOR AND MINOR AXIS\"; cin>>a>>b; } void bresen ::cal () { /* request auto detection */ int gdriver = DETECT,gmode, errorcode; int midx, midy, i; /* initialize graphics and local variables */ initgraph (&gdriver, &gmode, \" \"); /* read result of initialization */ errorcode = graphresult (); if (errorcode ! = grOK) /*an error occurred */ { printf(\"Graphics error: %s \\n\", grapherrormsg (errorcode); printf (\"Press any key to halt:\"); getch (); exit (1); /* terminate with an error code */ } x=0; y=b;
// REGION 1 p1 =(b * b)-(a * a * b) + (a * a)/4); { putpixel (x+h, y+k, RED); putpixel (-x+h, -y+k, RED); putpixel (x+h, -y+k, RED); putpixel (-x+h, y+k, RED); if (p1 < 0) p1 += ((2 *b * b) *(x+1))-((2 * a * a)*(y-1)) + (b * b); else { p1+= ((2 *b * b) *(x+1))-((2 * a * a)*(y-1))-(b * b); y--; } x++; } //REGION 2 p2 =((b * b)* (x + 0.5))+((a * a)*(y-1) * (y-1))-(a * a *b * b); while (y>=0) { If (p2>0) p2=p2-((2 * a * a)* (y-1))+(a *a); else { p2=p2-((2 * a * a)* (y-1))+((2 * b * b)*(x+1))+(a * a); x++; } y--;
putpixel (x+h, y+k, RED); putpixel (-x+h, -y+k, RED); putpixel (x+h, -y+k, RED); putpixel (-x+h, y+k, RED); } getch(); } Output:
EXPERIMENT 6-WAP ON SCAN FILL AND BOUNDARY FILL ALGORITHM Scan fill Algorithm: 1. We will process the polygon edge after edge, and store in the edge Table. 2. Storing is done by storing the edge in the same scanline edge tuple as the lowermost point's y-coordinate value of the edge. 3. After addition of any edge in an edge tuple, the tuple is sorted using insertion sort, according to its xofymin value. 4. After the whole polygon is added to the edge table, the figure is now filled. 5. Filling is started from the first scanline at the bottom, and continued till the top. 6. Now the active edge table is taken and the following things are repeated for each scanline: i. Copy all edge buckets of the designated scanline to the active edge tuple ii. Perform an insertion sort according to the xofymin values iii. Remove all edge buckets whose ymax is equal or greater than the scanline iv. Fillup pairs of edges in active tuple, if any vertex is got, follow these instructions: o If both lines intersecting at the vertex are onthe same side of the scanline, consider it as two points. o If lines intersecting at the vertex are at opposite sides of the scanline, consider it as only one point. v. Update the xofymin by adding slopeinverse for each bucket.
Program #include<stdio.h> #include<conio.h> #include<math.h> #include<dos.h> #include<graphics.h> void main() { int n,i,j,k,gd,gm,dy,dx; int x,y,temp; int a[20][2],xi[20]; float slope[20]; clrscr(); printf(“\\n\\Enter no.of edges of polygon”); scanf(“%d”,&n); printf(“\\n\\Enter co-ordinates of polygon”); for(i=0;i<n;i++)
{ printf(“\\tX%dY%d:”,i,i); scanf(“%d%d”,&a[i][0],&a[i][1]); } a[n][0]=a[0][0]; a[n][1]=a[0][1]; detectgraph(&gd,&gm); initgraph(&gd,&gm,”c:\\\\tc\\\\bgi”); for(i=0;i<n;i++) { line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]); } getch(); for(i=0;i<n;i++) { dy=a[i+1][1]-a[i][1]; dx=a[i+1][0]-a[i][0]; if(dy==0)
slope[i]=1.0; if(dx==0) slope[i]=0.0; if((dy!=0)&&(dx!=0)) { slope[i]=(float)dx/dy; } } for(y=0;y<480;y++) { k=0;for(i=0;i<n;i++) { if((a[i][1]<=y)&&(a[i+1][1]>y)){||((a[i][1]>y)&&(a[i+1][1]<=y))xi[k]=(int)(a[i][0]+ slope[i]*(y-a[i] [1])); k++; } }
for(j=0;j<k-1;j++) for(i=0;i<k;i++) { if(xi[i]>xi[i+1]) { temp=xi[i]; xi[i]=xi[i+1]; xi[i+1]=temp; } } setcolor(35); for(i=0;i<k;i+=2) { line(xi[i],y,xi[i+1]+1,y); getch(); } } }
Boundary Fill Algorithm 1. Create a function named as boundaryfill with 4 parameters (x,y,f_color,b_color). void boundaryfill(int x,inty,intf_color,intb_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 2. Call it recursively until the boundary pixels are reached. 3. Stop.
Program #include<stdio.h> #include<graphics.h> #include<dos.h> void boundaryfill(int x,inty,intf_color,intb_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:\\\\turboc3\\\\bgi\"); circle(x,y,radius); boundaryfill(x,y,4,15); delay(5000);
closegraph(); return 0; } Output
EXPERIMENT 7-WAP ON BOUNDARY FILL (8 CONNECTED POINT) #include<iostream.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> #include<dos.h> class Circle { intXCoord; intYCoord; int Radius; voidPlotPoint(int,int,int); public: Circle() { XCoord=0; YCoord=0; Radius=0; } Circle(int x, int y, int z) { XCoord=x; YCoord=y; Radius=z; } voidCircleMidpoint();
voidInitGraphs(); voidBoundryFill(int,int,int,int); voidBoundryFillRound(); }; void Circle :: InitGraphs() { intgMode, gDriver=DETECT; interrorcode; initgraph(&gDriver,&gMode,\"c:\\\\tc\\\\bgi\"); errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { cout<<\"Graphics error: \"<<grapherrormsg(errorcode); cout<<\"Press any key to halt:\"; getch(); exit(1); /* return with error code */ } setbkcolor(3); } void Circle :: PlotPoint(intx,inty,int z) { putpixel(XCoord+x,YCoord+y,z); putpixel(XCoord-x,YCoord+y,z); putpixel(XCoord+x,YCoord-y,z); putpixel(XCoord-x,YCoord-y,z); putpixel(XCoord+y,YCoord+x,z); putpixel(XCoord-y,YCoord+x,z); putpixel(XCoord+y,YCoord-x,z); putpixel(XCoord-y,YCoord-x,z); }
void Circle :: CircleMidpoint() { intx,y; int p; x=0; y=Radius; PlotPoint(x,y,15); p=1-Radius; while(x<y) { if(p<0) x+=1; else { x+=1; y-=1; } if(p<0) p=p+2*x+1; else { p=p + 2 *(x-y) + 1; } PlotPoint(x,y,15); } } void Circle :: BoundryFillRound() { intx,y;
int p; for(intiCntr=1;iCntr<Radius;iCntr++) { x=0; y=iCntr; PlotPoint(x,y,5); p=1-iCntr; while(x<y) { if(p<0) x+=1; else { x+=1; y-=1; } if(p<0) p=p+2*x+1; else { p=p + 2 *(x-y) + 1; } PlotPoint(x,y,5); } // sleep(1); } } void Circle :: BoundryFill(int x, inty,intfill,int bound) { int cur;
cur=getpixel(x,y); if(cur!=bound && cur!= fill) { putpixel(x,y,fill); Circle :: BoundryFill(x+1, y,fill,bound); Circle :: BoundryFill(x-1, y,fill,bound); Circle :: BoundryFill(x, y-1,fill,bound); Circle :: BoundryFill(x, y+1,fill,bound); Circle :: BoundryFill(x+1, y+1,fill,bound); Circle :: BoundryFill(x+1, y+1,fill,bound); Circle :: BoundryFill(x-1, y-1,fill,bound); Circle :: BoundryFill(x-1, y+1,fill,bound); } } void main() { Circle cObj(250,250,42); cObj.InitGraphs(); cObj.CircleMidpoint(); cObj.BoundryFillRound(); //cObj.BoundryFill(250,250,11,15); getch(); closegraph();
Output:
EXPERIMENT 8-WAP ON ROTATE ABOUT ORIGIN Program #include<stdio.h> #include<graphics.h> #include<math.h> int main() { intgd=0,gm,x1,y1,x2,y2; double s,c, angle; initgraph(&gd, &gm, \"C:\\\\TC\\\\BGI\"); setcolor(RED); printf(\"Enter coordinates of line: \"); scanf(\"%d%d%d%d\",&x1,&y1,&x2,&y2); cleardevice(); setbkcolor(WHITE); line(x1,y1,x2,y2); getch(); setbkcolor(BLACK); printf(\"Enter rotation angle: \"); scanf(\"%lf\", &angle); setbkcolor(WHITE); c = cos(angle *3.14/180); s = sin(angle *3.14/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); cleardevice(); line(x1, y1 ,x2, y2); getch();
closegraph(); return 0; } Output: Before rotation
After rotation
EXPERIMENT 9-WAP ON ROTATE ABOUT REFERENCE POINT. Solution # include <iostream.h> # include <conio.h> # include <graphics.h> # include <math.h> charIncFlag; intPolygonPoints[4][2] = {{10,100},{110,100},{110,200},{10,200}}; intRefX = 10; intRefY = 100; voidPolyLine() { intiCnt; cleardevice(); line(0,240,640,240); line(320,0,320,480); for (iCnt=0; iCnt<4; iCnt++) { line(PolygonPoints[iCnt][0],PolygonPoints[iCnt][1], PolygonPoints[(iCnt+1)%4][0],PolygonPoints[(iCnt+1)%4][1]); } } void Translate(int Direction) {
intiCnt; for (iCnt=0; iCnt<4; iCnt++) { PolygonPoints[iCnt][0] += Direction*RefX; PolygonPoints[iCnt][1] -= Direction*RefY; } } void Rotate() { float Angle; intiCnt; intTx,Ty; Translate(-1); cout<<endl; Angle = 30.0*(22.0/7.0)/180.0; for (iCnt=0; iCnt<4; iCnt++) { Tx = PolygonPoints[iCnt][0]; Ty = PolygonPoints[iCnt][1]; PolygonPoints[iCnt][0] = (Tx - 320)*cos(Angle) - (Ty - 240)*sin(Angle) + 320; PolygonPoints[iCnt][1] = (Tx - 320)*sin(Angle) + (Ty - 240)*cos(Angle) + 240; } Translate(1); } void main() { intgDriver = DETECT, gMode; intiCnt; initgraph(&gDriver, &gMode, \"C:\\\\TC\\\\BGI\");
for (iCnt=0; iCnt<4; iCnt++) { PolygonPoints[iCnt][0] += 320; PolygonPoints[iCnt][1] = 240 - PolygonPoints[iCnt][1]; } PolyLine(); getch(); Rotate(); PolyLine(); getch(); } Output:
EXPERIMENT 10-WAP ON SCALING ABOUT ORIGIN. Algorithm: 1. Make a 2x2 scaling matrix S as: Sx 0 0 Sy 2. For each point of the polygon. (i) Make a 2x1 matrix P, where P[0][0] equals to x coordinate of the point and P[1][0] equals to y coordinate of the point. (ii) Multiply scaling matrix S with point matrix P to get the new coordinate. 3. Draw the polygon using new coordinates. Program // C program to demonstrate scaling of abjects #include<stdio.h> #include<graphics.h> // Matrix Multiplication to find new Coordinates. // s[][] is scaling matrix. p[][] is to store // points that needs to be scaled. // p[0][0] is x coordinate of point. // p[1][0] is y coordinate of given point. void findNewCoordinate(int s[][2], int p[][1]) {
int temp[2][1] = { 0 }; for (int i = 0; i < 2; i++) for (int j = 0; j < 1; j++) for (int k = 0; k < 2; k++) temp[i][j] += (s[i][k] * p[k][j]); p[0][0] = temp[0][0]; p[1][0] = temp[1][0]; } // Scaling the Polygon void scale(int x[], int y[], int sx, int sy) { // Triangle before Scaling line(x[0], y[0], x[1], y[1]); line(x[1], y[1], x[2], y[2]); line(x[2], y[2], x[0], y[0]); // Initializing the Scaling Matrix. int s[2][2] = { sx, 0, 0, sy }; int p[2][1]; // Scaling the triangle for (int i = 0; i < 3; i++) { p[0][0] = x[i]; p[1][0] = y[i]; findNewCoordinate(s, p); x[i] = p[0][0]; y[i] = p[1][0];
} // Triangle after Scaling line(x[0], y[0], x[1], y[1]); line(x[1], y[1], x[2], y[2]); line(x[2], y[2], x[0], y[0]); } // Driven Program int main() { int x[] = { 100, 200, 300 }; int y[] = { 200, 100, 200 }; int sx = 2, sy = 2; int gd, gm; detectgraph(&gd, &gm); initgraph(&gd, &gm,\" \"); scale(x, y, sx,sy); getch(); return 0; }
Output:
EXPERIMENT 11- WAP ON TRANSLATION. A translation process moves every point a constant distance in a specified direction. It can be described as a rigid motion. A translation can also be interpreted as the addition of a constant vector to every point, or as shifting the origin of the coordinate system. Suppose, If point (X, Y) is to be translated by amount Dx and Dy to a new location (X’, Y’) then new coordinates can be obtained by adding Dx to X and Dy to Y as: X' = Dx + X Y' = Dy + Y or P' = T + P where P' = (X', Y'), T = (Dx, Dy ), P = (X, Y) Here, P(X, Y) is the original point. T(Dx, Dy) is the translation factor, i.e. the amount by which the point will be translated. P'(X’, Y’) is the coordinates of point P after translation. Examples: Input : P[] = {5, 6}, T = {1, 1} Output : P'[] = {6, 7}
Input : P[] = {8, 6}, T = {-1, -1} Output : P'[] = {7, 5} Whenever we perform translation of any object we simply translate its each and every point. Some of basic objects along with their translation can be drawn as: 1. Point Translation P(X, Y) : Here we only translate the x and y coordinates of given point as per given translation factor dx and dy respectively. Below is the C++ program to translate a point: // C++ program for translation // of a single coordinate #include<bits/stdc++.h> #include<graphics.h> using namespace std; // function to translate point void translatePoint ( int P[], int T[]) { /* init graph and putpixel are used for representing coordinates through graphical functions */ int gd = DETECT, gm, errorcode; initgraph (&gd, &gm, \"c:\\\\tc\\\\bgi\"); cout<<\"Original Coordinates :\"<<P[0]<<\",\"<<P[1]; putpixel (P[0], P[1], 1);
// calculating translated coordinates P[0] = P[0] + T[0]; P[1] = P[1] + T[1]; cout<<\"\\nTranslated Coordinates :\"<< P[0]<<\",\"<< P[1]; // Draw new coordinatses putpixel (P[0], P[1], 3); closegraph(); } // driver program int main() { int P[2] = {5, 8}; // coordinates of point int T[] = {2, 1}; // translation factor translatePoint (P, T); return 0; } Output: Original Coordinates : 5, 8 Translated Coordinates : 7, 9 2. Line Translation: The idea to translate a line is to translate both of the end points of the line by the given translation factor(dx, dy) and then draw a new line with inbuilt graphics function. Below is the C++ implementation of above idea: // cpp program for translation // of a single line #include<bits/stdc++.h> #include<graphics.h>
using namespace std; // function to translate line void translateLine ( int P[][2], int T[]) { /* init graph and line() are used for representing line through graphical functions */ int gd = DETECT, gm, errorcode; initgraph (&gd, &gm, \"c:\\\\tc\\\\bgi\"); // drawing original line using graphics functions setcolor (2); line(P[0][0], P[0][1], P[1][0], P[1][1]); // calculating translated coordinates P[0][0] = P[0][0] + T[0]; P[0][1] = P[0][1] + T[1]; P[1][0] = P[1][0] + T[0]; P[1][1] = P[1][1] + T[1]; // drawing translated line using graphics functions setcolor(3); line(P[0][0], P[0][1], P[1][0], P[1][1]); closegraph(); } // driver program int main() { int P[2][2] = {5, 8, 12, 18}; // coordinates of point
int T[] = {2, 1}; // translation factor translateLine (P, T); return 0; } Output: 3. Rectangle Translation : Here we translate the x and y coordinates of both given points A(top left ) and B(bottom right) as per given translation factor dx and dy respectively and then draw a rectangle with inbuilt graphics function // C++ program for translation // of a rectangle #include<bits/stdc++.h> #include<graphics.h> using namespace std; // function to translate rectangle void translateRectangle ( int P[][2], int T[]) { /* init graph and rectangle() are used for representing rectangle through graphical functions */ int gd = DETECT, gm, errorcode;
Search