LINE,CIRCLE AND ELLIPSE DRAWING USING BRESENHAM S ALGORITHM

Similar documents
//2D TRANSFORMATION TRANSLATION, ROTATING AND SCALING. #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<stdlib.

IMPLEMENTATION OF DDA LINE ALGORITHM

Windowing And Clipping (14 Marks)

Overview of Transformations (18 marks) In many applications, changes in orientations, size, and shape are accomplished with

THIRUMALAI ENGINEERING COLLEGE KILAMBI, KANCHIPURAM DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING COMPUTER GRAPHICS LAB (CS 2405) REG.

LAB MANUAL OF COMPUTER GRAPHICS BY: Mrs. Manisha Saini

DDA ALGORITHM. To write a C program to draw a line using DDA Algorithm.

February 1. Lab Manual INSTITUTE OF INFORMATION TECHNOLOGY & MANAGEMENT GWALIOR COMPUTER GRAPHICS & MUTIMEDIA

SIR C.R.R COLLEGE OF ENGINEERING, ELURU.

Lab Programs. /* LAB1_2: To study the various graphics commands in C language */ /* LAB1_1: To study the various graphics commands in C language */

1. DDA Algorithm(Digital Differential Analyzer)

The pictorial representation and manipulation of data by a compute. The creation, display, and storage of pictures, with a computer

//Program1. Computer Graphics C Version Programmed By: Salha Alzahrani

DEPARTMENT OF INFORMATION TECHNOLOGY DR.NAVALAR NEDUNCHEZHIAYN COLLEGE OF ENGINEERING, THOLUDUR , CUDDALORE DIST.

CS2405 Computer Graphics Lab - Manual

'C' Programming Language

CS COMPUTER GRAPHICS LAB

Q 1. Attempt any TEN of the following:

POLYNOMIAL ADDITION. AIM:- To write a C program to represent a polynomial as a linked list and write functions for polynomial addition.

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

MET71 COMPUTER AIDED DESIGN

Part 3: 2D Transformation

Write C++/Java program to draw line using DDA and Bresenham s algorithm. Inherit pixel class and Use function overloading.

Computers Programming Course 12. Iulian Năstac

AIM:- To write a C program to create a singly linked list implementation.

C Programming Lecture V

DMI College of Engineering

Module Contact: Dr Rudy Lapeer, CMP Copyright of the University of East Anglia Version 1

Dynamic Memory Allocation

Output Primitives Lecture: 3. Lecture 3. Output Primitives. Assuming we have a raster display, a picture is completely specified by:

Module Contact: Dr Stephen Laycock, CMP Copyright of the University of East Anglia Version 1

Snake Ladder Game. ifstream infile("c:\tc\bin\rattle.txt"); infile.getline(hs,4); infile.close(); hscore = atoi(hs);

UNIT 2 2D TRANSFORMATIONS

Case Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

DEV BHOOMI INSTITUTE OF TECHNOLOGY

Lab-Report Digital Signal Processing. FIR-Filters. Model of a fixed and floating point convolver

Chapter 8: Implementation- Clipping and Rasterization

Tutorial No. 2 - Solution (Overview of C)

Introduction to Computing Lecture 07: Repetition and Loop Statements (Part II)

Arrays in C. By Mrs. Manisha Kuveskar.

1) Program to display a set of values {fij} as a rectangular mesh. #include<gl/glut.h> #define maxx 20. #define maxy 20. #define dx 15.

Computer design and manufacturing lab manual

Computer Graphics. Lab Manual

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

1. Simple if statement. 2. if else statement. 3. Nested if else statement. 4. else if ladder 1. Simple if statement

Unit 5. Decision Making and Looping. School of Science and Technology INTRODUCTION

MODULE V: POINTERS & PREPROCESSORS

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

Annexure-I. #include<stdio.h> #include<graphics.h> #include<conio.h> #include<math.h> #include<stdlib.h> #include<string.h>

Government Polytechnic Muzaffarpur.

UNIT 3 FUNCTIONS AND ARRAYS

BGI Graphics Engine Tutorial

Developed By: P.Venkateshwarlu, Alphores Womens Degree College, Karimnagar

DEPERTMENT OF CIVIL ENGINEERING RAJSHSHI UNIVERSITY OF ENGINEERING & TECHNOLOGY CE 206 NUMERICAL METHODS & COMPUTER PROGRAMMING SESSIONAL

Computer Graphics Lab. Lab Manual

DEV BHOOMI INSTITUTE OF TECHNOLOGY

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

Jaipur National University, Jaipur Dr. Rajendra Takale Prof. and Head Academics SBPIM, Pune

/* Area and circumference of a circle */ /*celsius to fahrenheit*/

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

List of Programs: Programs: 1. Polynomial addition

MGM S JAWAHARLAL NEHRU ENGINEERING COLLEGE N-6, CIDCO, AURANGABAD LAB MANUALS SUB: COMPUTER LAB-II CLASS: S.E.CIVIL

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

Laboratory Manual CAD/CAM LAB. B. Tech. Mechanical Engineering. for. Department of Mechanical Engineering

Assignment 6. Q1. Create a database of students using structures, where in each entry of the database will have the following fields:

LABORATORY MANUAL. (CSE-103F) FCPC Lab

Module Contact: Dr R J Lapeer, CMP Copyright of the University of East Anglia Version 1

(a) rotating 45 0 about the origin and then translating in the direction of vector I by 4 units and (b) translating and then rotation.

Computer Graphics. Introduction: Computer Graphics

Computer Programming Unit 3

ME 172. C Programming Language Sessional Lecture 8

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI

A pointer is a variable just like other variable. The only difference from other variables is that it stores the memory address other variables.

P.E.S. INSTITUTE OF TECHNOLOGY BANGALORE SOUTH CAMPUS 1 ST INTERNAL ASSESMENT TEST (SCEME AND SOLUTIONS)

UIC. C Programming Primer. Bharathidasan University

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of MCA

More examples for Control statements

Computer Graphics Course Notes

Programming and Data Structures

Lab Manual. Computer Graphics. T.E. Computer. (Sem VI)

Decision Making and Branching


Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

MODULE 1. Introduction to Data Structures

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY CS2401 COMPUTER GRAPHICS QUESTION BANK

Fundamental of Programming (C)

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

Unit 3 Transformations and Clipping

MC9217 / Programming and Data Structures Lab MC9217 PROGRAMMING AND DATA STRUCTURES LAB L T P C

LAB MANUAL. ANTC Lab (ME-321- F) DEPARTMENT OF APPLIED SCIENCE AND HUMINITIES

COMPUTER GRAPHICS AND MULTIMEDIA

Fundamentals of Programming

CS184 : Foundations of Computer Graphics Professor David Forsyth Final Examination

Mr. Giansante. C++ Programming. Graphics

/* Polynomial Expressions Using Linked List*/ #include<stdio.h> #include<conio.h> #include <malloc.h> struct node. int num; int coeff;

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

Data Structures and Algorithms (DSA) Course 4. Iulian Năstac

Transcription:

Ex.no :1 Date: LINE,CIRCLE AND ELLIPSE DRAWING USING BRESENHAM S ALGORITHM AIM: To write a program to implement Bresenham s algorithms for line, circle and ellipse drawing. ALGORITHM: Step1: Create a function for line circle and ellipse. Step2: calculate contents Ax, Ay, 2Ay, 2Ay-2Ax and P0=2Ay-Ax to draw the line. Step3: Calculate the initial value parameter as q=q+(4*m)+10,q=q+(4*(m-n))+10 and draw circle using cplot(x,y,m,n). Step4: Calculate putpixel(xc+rx*cos(i*3.14/180),yc+ry*sin(i*3.14/180),15) to draw the ellipse drawing Step5: Display line, circle and ellipse drawing of Bresenham s algorithm. 1

PROGRAM: #include<stdio.h> #include<conio.h> #include<math.h> #include<graphics.h> void lined(int, int, int, int); void circled(int, int, int); void ellipsed(int, int, int, int); void main( ) int x, y, xe, ye, r, rx, ry, n; int gd=detect, gm; initgraph(&gd, &gm, "e:\\tc\\bgi"); do clrscr( ); printf("\n1. Line\n2. Circle\n3. Ellipse\n"); printf("enter the Choice:"); scanf("%d",&n); switch(n) case 1: printf("\nline DRAWING\nEnter the x and y axis:"); scanf("\n%d%d",&x,&y); printf("\nenter the Xend and yend:"); 2

scanf("%d%d",&xe,&ye); lined(x,y,xe,ye); break; case 2: printf("\ncircle DRAWING ); printf( \nenter the x and y axis and also Radius:"); scanf("%d%d%d",&x,&y,&r); circled(x,y,r); break; case 3: printf("\nellipse DRAWING ) printf( \nenter the x,y and rx,ry:"); scanf("%d%d%d%d",&x,&y,&rx,&ry); ellipsed(x,y,rx,ry); break; default: exit(0); break; while(n<=3); getch(); closegraph( ); void lined(int xa,int ya,int xb,int yb) 3

int dx=abs(xa-xb); int dy=abs(ya-yb); int p=2*dy-dx; int xend=2*dy; int yend=2*(dy-dx); int x,y,end; if(xa>xb) x=xb; y=yb; xend=xa; else x=xa; y=ya; yend=xb; while(x<xend) x++; if(p<0) p+=xend; else y++; p+=yend; putpixel(x,y,10); 4

getch( ); void circled(int x,int y,int r) int m=0,n=r, q; void cplot(int,int,int,int); q=3-(2*r); while(m<n) cplot(x,y,m,n); if(q<0) q=q+(4*m)+10; else q=q+(4*(m-n))+10; n--; m++; if(m==n) cplot(x,y,m,n); getch( ); void cplot(int xc,int yc,int x,int y) putpixel(xc+x,yc+y,15); putpixel(xc-x,yc+y,15); 5

putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc+y,yc+x,15); putpixel(xc-y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc-y,yc-x,15); void ellipsed(int xc,int yc,int rx,int ry) int i; for(i=0;i<=360;i++) putpixel(xc+rx*cos(i*3.14/180),yc+ry*sin(i*3.14/180),15); getch( ); 6

OUTPUT:- 1.Line 2. Circle 3. Ellipse Enter the Choice:1 LINE DRAWING Enter the x and y axis:10 15 Enter the Xend and yend: 45 60 1.Line 2. Circle 3. Ellipse Enter the Choice:2 CIRCLE DRAWING Enter the x and y axis and also Radius:45 45 45 1.Line 2. Circle 7

3. Ellipse Enter the Choice:3 ELLIPSE DRAWING Enter the x,y and rx,ry:45 45 45 45 RESULT: Thus the program implementation of Bresenham s algorithm for line, circle and ellipse drawing is done successfully. 8

Ex.no :2 Date: 2D-TRANSFORMATION AIM: To create a program to implement the 2D transformation of translation, Rotation, Scaling, Reflection and Shearing. ALGORITHM: Step 1: Start the Program. Step 2: Include the graphics header file Step 3: Initialize graphics using initgraph(). Step 4: Initialize the variable Step 5: Enter the choice for transformation. Step 6: If choice=1 Translation i.e. changing the co-ordinates is performed, x =x+tx y =y+ty Step 7: If choice=2 Rotation i.e. Rotate the object is performed x =x*cosθ-y*sinθ y =x*sinθ+y*cosθ Step 8: If choice=3 Scaling i.e.changing the size of the object is performed x =x*sx y =y*sy Step 9:If choice=4 Reflection i.e to produce the mirror image of the objects. Step 10: If choice=5 Shearing i.e Slanting the objects Step 11: Stop the Program. 9

PROGRAM: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<alloc.h> #include<stdlib.h> int n,i,p[20],q[20]; int convertx(int x); int converty(int y); void axis(void); void init(void); void share(void); void translate(void); void scaling(void); void rotate(void); void reflect(void); void main( ) int ch, gd=detect,gm; initgraph(&gd,&gm,"e:\\tc\\bgi"); axis( ); printf("\b"); cleardevice( ); printf("enter THE NUMBER OF VERTICES:"); scanf("%d",&n); 10

printf("enterthecoordinate(reenterthefirstvertexattheend )"); for(i=0;i<2*n+2;i++) scanf("%d",&p[i]); clrscr( ); init( ); getch( ); do clrscr( ); cleardevice( ); printf("\n\t\t2d-transformations\n1.translation\n 2. ROTATION\n 3.SCALING\n4.REFLECTION\n5.SHARING\n"); printf("\n\n ENTER UR CHOICE:\n"); scanf("%d",&ch); clrscr( ); cleardevice( ); switch(ch) case 1:axis( ); init( ); translate( ); break; case 2:axis( ); init( ); rotate( ); break; case 3:axis( ); 11

init( ); scaling( ); break; case 4:reflect( ); break; case 5:share( ); break; while(ch!=6); closegraph( ); void axis(void) cleardevice( ); setlinestyle(solid_line,1,1); line(320,0,320,getmaxy( )); line(0,240,getmaxx( ),240); outtextxy(325,245,"0"); line(316,40,324,40); outtextxy(325,41,"400"); line(316,440,324,440); outtextxy(325,441,"-400"); line(20,236,20,244); outtextxy(19,250,"-600"); line(620,236,620,244); outtextxy(608,250,"600"); 12

void init( ) cleardevice( ); axis( ); setcolor(yellow); setlinestyle(solid_line,1,1); for(i=0;i<2*n+2;i+=2) q[i]=convertx(p[i]); for(i=1;i<2*n+2;i+=2) q[i]=converty(p[i]); drawpoly(n+1,q); setcolor(white); int convertx(int x) x=x/2; return(x+=320); int converty(int y) y=y/2; return(y=240-y); void share( ) int sh[20],shr[20]; float shx,shy,c; printf("(1)share RELATIVE TO X-AXIS \n"); 13

printf("(2)share RELATIVE TO Y-AXIS \n"); printf("\n ENTER UR CHOICE:"); scanf("%f",&c); if(c==1) init( ); printf("\n ENTER THE SHARE FACTOR(shx):"); scanf("%f",&shx); for(i=0;i<2*n+2;i+=2) sh[i]=p[i]+(shx*(p[i+1])); for(i=1;i<2*n+2;i+=2) sh[i]=p[i]; else init( ); printf("\n ENTER THE SHARE FACTOR(shy):"); scanf("%f",&shy); for(i=1;i<2*n+2;i+=2) sh[i]=(shy*p[i-1])+p[i]; for(i=0;i<2*n+2;i+=2) sh[i]=p[i]; setlinestyle(dotted_line,1,1); for(i=0;i<2*n+2;i+=2) shr[i]=convertx(sh[i]); for(i=1;i<2*n+2;i+=2) 14

shr[i]=converty(sh[i]); drawpoly(n+1,shr); getch( ); void translate( ) int tr[20],tr1[20],tx,ty; printf("\n ENTER THR X-TRANSLATION VECTOR:"); scanf("%d",&tx); printf("\n ENTER THR Y-TRANSLATION VECTOR:"); scanf("%d",&ty); for(i=0;i<2*n+2;i+=2) tr[i]=p[i]+tx; for(i=1;i<2*n+2;i+=2) tr[i]=p[i]+ty; setlinestyle(dotted_line,1,1); for(i=0;i<2*n+2;i+=2) tr1[i]=convertx(tr[i]); for(i=1;i<2*n+2;i+=2) tr1[i]=converty(tr[i]); drawpoly(n+1,tr1); getch( ); void scaling( ) int sc[20],sc1[20]; float sx,sy; 15

printf("\n ENTER THE X-AXIS SCALING FACTOR:"); scanf("%f",&sx); printf("\n ENTER THE Y-AXIS SCALING FACTOR:"); scanf("%f",&sy); for(i=0;i<2*n+2;i+=2) sc[i]=p[i]*sx; for(i=1;i<2*n+2;i+=2) sc[i]=p[i]*sy; setlinestyle(dotted_line,1,1); for(i=0;i<2*n+2;i+=2) sc1[i]=convertx(sc[i]); for(i=1;i<2*n+2;i+=2) sc1[i]=converty(sc[i]); drawpoly(n+1,sc1); getch( ); void rotate( ) int ro[20],rot[20],xr,yr,v; float ang; printf("\n ENTER THE ANGLE TO ROTATE:"); scanf("%f",&ang); ang=((ang*3.14)/180)*(-1); printf("\n ENTER THE ROTATION VERTEX ("); for(i=1;i<=n;i++) printf("%d)",i); 16

scanf("%d",&v); xr=p[(2*v)-2]; yr=p[((2*v)-2)+1]; for(i=0;i<(2*n+1);i+=2) ro[i]=xr+(p[i]-xr)*cos(ang)-(p[i+1]-yr)*sin(ang); ro[i+1]=yr+(p[i]-xr)*sin(ang)+(p[i+1]-yr)*cos(ang); setlinestyle(dotted_line,1,1); for(i=0;i<2*n+2;i+=2) rot[i]=convertx(ro[i]); for(i=1;i<2*n+2;i+=2) rot[i]=converty(ro[i]); drawpoly(n+1,rot); getch( ); void reflect( ) int j,c,rt[20],rft[20]; printf("\n1.x-axis REFLECTION\n2.y-AXIS REFLECTION"); printf("\n ENTER THE CHOICE:"); scanf("%d",&c); clrscr( ); cleardevice( ); if(c==1) for(i=1;i<2*n+2;i+=2) rt[i]=p[i]*(-1); 17

for(i=0;i<2*n+2;i+=2) rt[i]=p[i]; else for(i=0;i<2*n+2;i+=2) rt[i]=p[i]*(-1); for(i=1;i<2*n+2;i+=2) rt[i]=p[i]; init( ); setlinestyle(dotted_line,1,1); for(i=0;i<2*n+2;i+=2) rft[i]=convertx(rt[i]); for(i=1;i<2*n+2;i+=2) rft[i]=converty(rt[i]); drawpoly(n+1,rft); getch( ); OUTPUT: ENTER THE NUNBER OF VERTICES: 1 ENTER THE COORDINATE (reenter the first vertex at the end) : 100 100 400 400 18

2D-TRANSFORMATIONS 1)TRANSLATION 2)ROTATION 3)SCALING 4)REFLECTION 5)SHEARING ENTER YOUR CHOICE: 1 ENTER THE X-TRANSLATION VECTOR:80 ENTER THE Y-TRANSLATION VECTOR:50 ENTER YOUR CHOICE: 2 ENTER THE ANGLE TO ROTATE:90 ENTER THE ROTATION VERTEX (1) 1 19

ENTER YOUR CHOICE: 3 ENTER THE X-AXIS SCALING FACTOR:100 ENTER THE Y-AXIS SCALING FACTOR:100 ENTER YOUR CHOICE: 4 1. X-AXIS REFLECTION 2. Y-AXIS REFLECTION ENTER YOUR CHOICE:1 20

ENTER YOUR CHOICE: 5 1.SHEAR RELATIVE TO X-AXIS 2.SHEAR RELATIVE TO Y-AXIS ENTER YOUR CHOICE:2 ENTER THE SHEAR FACTOR (shy):100 RESULT: Ex.no :3 Date: ations is executed successfully. 3D Transformation Thu s the above program for Two Dimension al Transform 21

AIM: To create a program to implement the 3D transformation of translation, Rotation, Scaling of objects.. ALGORITHM: Step 1: Start the program. Step 2: Declare the necessary variables, with member functions. Step 3: Create the main function, in that function to initialize graph using do-while statement. Step 4: Using Switch statement for translation, scaling and rotation exit. Step 5: Using get function for getting the values. Step 6: Similarly, get the draw function for draw the lines. Step 7: Stop the program. 22

PROGRAM: #include<graphics.h> #include<process.h> #include<stdlib.h> #include<stdio.h> #include<conio.h> #include<math.h> #include<dos.h> #define X 250 #define Y 100 #define Z 100 int ori[24]; void wtor(int pts3[24]); void cube(int pts1[16]); void main( ) int gd=detect,gm,s,ch; void tran( ); void rot( ); void scale( ); initgraph(&gd,&gm,"e:\\tc\\bgi"); printf("\n\t\t 3D-TRANSFORMATION"); printf("\n\t\t~~~~~~~~~~~~~~~~~~~~\n"); printf("\n ENTER THE SIDE OF THE CUBE:"); scanf("%d",&s); 23

ori[0]=x; ori[1]=y; ori[2]=z; ori[3]=x+s; ori[4]=y; ori[5]=z; ori[6]=x+s; ori[7]=y; ori[8]=z+s; ori[9]=x; ori[10]=y; ori[11]=z+s; ori[12]=x; ori[13]=y-s; ori[14]=z; ori[15]=x+s; ori[16]=y-s; ori[17]=z; ori[18]=x+s; ori[19]=y-s; ori[20]=z+s; ori[21]=x; ori[22]=y-s; ori[23]=z+s; wtor(ori); getch( ); do clearviewport( ); gotoxy(35,5); printf("\n\t\t 3D-TRANSFORMATIONS \n"); printf("\n\t\t 1.TRANSLATION \n"); printf("\n\t\t 2.ROTATION \n"); printf("\n\t\t 3.SCALING\n"); printf("\n\t\t 4.EXIT\n"); printf("\n\n ENTER UR CHOICE:\n"); scanf("%d",&ch); switch(ch) case 1: wtor(ori); 24

tran( ); getch( ); break; case 2: wtor(ori); rot( ); getch( ); break; case 3: wtor(ori); scale( ); getch( ); break; case 4: break; default: printf("\n INVALID ENTRY!!!!!"); getch( ); break; while(ch!=4); closegraph( ); void cube(int pts1[16]) 25

setcolor(15); setlinestyle(dotted_line,1,1); line(pts1[0],pts1[1],pts1[2],pts1[3]); delay(200); line(pts1[2],pts1[3],pts1[4],pts1[5]); delay(200); line(pts1[2],pts1[3],pts1[10],pts1[11]); delay(200); setlinestyle(solid_line,1,1); line(pts1[4],pts1[5],pts1[6],pts1[7]); delay(200); line(pts1[6],pts1[7],pts1[0],pts1[1]); delay(200); line(pts1[0],pts1[1],pts1[8],pts1[9]); delay(200); line(pts1[4],pts1[5],pts1[12],pts1[13]); delay(200); line(pts1[6],pts1[7],pts1[14],pts1[15]); delay(200); line(pts1[8],pts1[9],pts1[10],pts1[11]); delay(200); line(pts1[10],pts1[11],pts1[12],pts1[13]); delay(200); line(pts1[12],pts1[13],pts1[14],pts1[15]); 26

delay(200); line(pts1[8],pts1[9],pts1[14],pts1[15]); delay(200); setcolor(15); void wtor(int pts3[24]) int pts2[16],i,j; float a1=3.14*60/180; float a2=3.14*60/180; for(i=0,j=0;i<24,j<16;i+=3,j+=2) pts2[j]=pts3[i]+pts3[i+2]*1/tan(a1)*cos(a2); pts2[j+1]=pts3[i+1]+pts3[i+2]*1/tan(a1)*sin(a2); cube(pts2); void tran( ) int tr3[24]; int i,tx,ty,tz; printf("\n ENTER THE TRANSLATION VECTORS:"); scanf("%d%d%d",&tx,&ty,&tz); for(i=0;i<24;i+=3) 27

tr3[i]=ori[i]+tx; tr3[i+1]=ori[i+1]+ty; tr3[i+2]=ori[i+2]+tz; wtor(tr3); void rot( ) int r3[24]; float deg,rad; int i,axis; printf("\n ENTER THE ANGLE:"); scanf("%f",&deg); rad=3.14*deg/180; printf("\n ENTER THE AXIS OF ROTATION:"); printf("x=1/y=2/z=3):\n"); scanf("%d",&axis); for(i=0;i<24;i+=3) switch(axis) case 1: r3[i]=ori[i]; r3[i+1]=ori[i+1]*cos(rad)-ori[i+2]*sin(rad); r3[i+2]=ori[i+1]*sin(rad)+ori[i+2]*cos(rad); 28

break; case 2: r3[i]=ori[i+2]*sin(rad)+ori[i]*cos(rad); r3[i+3]=ori[i+1]; r3[i+2]=ori[i+2]*cos(rad)-ori[i+2]*sin(rad); break; case 3: r3[i]=ori[i]*cos(rad)-ori[i+1]*sin(rad); r3[i+1]=ori[i]*sin(rad)+ori[i+1]*cos(rad); break; default: printf("\n INVALID CHOICE!!!!"); getch( ); break; wtor(r3); void scale( ) float sx,sy,sz; int i,s3[24]; printf("\n ENTER THE SCALING FACTOR:"); scanf("%f%f%f",&sx,&sy,&sz); for(i=0;i<24;i+=3) 29

s3[i]=ori[i]*sx; s3[i+1]=ori[i+1]*sy; s3[i+2]=ori[i+2]*sz; wtor(s3); OUTPUT: ENTER THE SIDE OF THE CUBE:50 1. TRANSLATION 2. ROTATION 3. SCALING 4. EXIT ENTER THE CHOICE: 1 ENTER THE TRANSLATION VECTORS: 50 50 50 30

31

1. TRANSLATION 2. ROTATION 3. SCALING 4. EXIT ENTER THE CHOICE: 2 ENTER THE ANGLE:45 ENTER THE AXIS OF ROTATION:(X=1/Y=2/Z=3):1 ENTER THE CHOICE: 3 ENTER THE SCALING FACTOR: 1 3 1 RESULT: Thus the above program for Three Dimensional Transformation of Translation, Scaling and Rotation is executed successfully. 32

Ex.no :4 Date: Cohen Sutherland 2D clipping and windowing AIM: To write a program to implement Cohen-Sutherland 2D clipping and windowing.. ALGORITHM: Step1: Create a function to get the line co-ordinates and window co-ordinate values. Step2: Check all the co-ordinates of the lines are within the window co-ordinate or not. Step3: Clip the lines which are all outside of the window co-ordinates. Step4: After the clipping draw all the lines along with the window. 33

PROGRAM: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<stdlib.h> #define top 200 #define left 200 #define right 400 #define bottom 400 typedef struct float x,y; wcpt2; int accept(int c1[4],int c2[4]) int k,t; t=1; for(k=0;k<=3;k++) if(c1[k] c2[k]) t=0; return(t); int ptinside(int c[4]) int p; 34

if(c[0] c[2] c[3]) p=0; else p=1; return(p); void main( ) wcpt2 p1,p2,tmp; int code1[4],code2[4],t[4],done=0,draw=0,i,j,k,p,y; int gd=detect,gm; float m; initgraph(&gd,&gm,"e:\\tc\\bgi"); rectangle(left,top,right,bottom); printf("2d-clipping USINGF COHEN SUTHERLAND ALGORITHM \n"); printf("**************************************************\n\n"); printf("enter THE VERTICES OF THE LINE TO BE CLIPPED \n"); scanf("%f%f%f%f",&p1.x,&p1.y,&p2.x,&p2.y); settextstyle(1,0,2); getch(); clrscr( ); cleardevice( ); outtextxy(60,100,"before CLIPPING"); rectangle(left,top,right,bottom); line(p1.x,p1.y,p2.x,p2.y); 35

getch( ); clearviewport( ); rectangle(left,top,right,bottom); while(!done) if(p1.x<left) code1[0]=1; else code1[0]=0; if(p1.x>right) code1[1]=1; else code1[1]=0; if(p1.y>bottom) code1[2]=1; else code1[2]=0; if(p1.y<top) code1[3]=1; else code1[3]=0; if(p2.x<left) code2[0]=1; else code2[0]=0; 36

if(p2.x>right) code2[1]=1; else code2[1]=0; if(p2.y>bottom) code2[2]=1; else code2[2]=0; if(p2.y<top) code2[3]=1; else code2[3]=0; if(accept(code1,code2)) done=1; draw=1; else y=0; for(k=0;k<=3;k++) if(code1[k]&&code2[k]) y=1; if(y==1) done=1; 37

else if(ptinside(code1)) tmp=p1; p1=p2; p2=tmp; for(i=0;i<3;i++) t[i]=code1[i]; for(i=0;i<=3;i++) code2[i]=t[i]; m=(p2.y-p1.y)/(p2.x-p1.x); if(code1[0]) p1.y=p1.y+(left-p1.x)*m; p1.x=left; else if(code1[1]) p1.y=p1.y+(right-p1.x)*m; p1.x=right; else 38

if(code1[2]) p1.y=p1.y+(bottom-p1.y)/m; p1.x=bottom; else if(code1[3]) p1.x=p1.x+(top-p1.y)/m; p1.y=top; if(draw) settextstyle(1,0,2); outtextxy(20,60,"after CLIPPING"); line(abs(p1.x),abs(p1.y),abs(p2.x),abs(p2.y)); getch( ); closegraph( ); 39

40

OUTPUT: 2D-CLIPPING USING COHEN SUTHERLAND ALGORITHM ************************************************** ENTER THE VERTICES OF THE LINE TO BE CLIPPED: 150 150 300 350 BEFORE CLIPPING: AFTER CLIPPING: RESULT: 41

Thus the above program for Cohen Sutherland 2D clipping and windowing is executed successfully. 5. IMPLEMENTATION OF TWO DIMENSIONAL COMPOSITE TRANSFORMATIONS DESCRIPTION: A transformation is any operation on a point in space (x, y) that maps the point's coordinates into a new set of coordinates (x1, y1).the Two Dimensional Composite transformation represent a sequence of transformations as a single matrix which has the order of operations as Translation, Rotation, Scaling, Shearing, Reflection. CODING: #include <graphics.h> /* include the necessary header files*/ #include <stdlib.h> #include <stdio.h> #include <conio.h> #include <math.h> int xa,xb,xc,ya,yb,yc,y1a,y1b,y1c,x1a,x1b,x1c,x2a,x2b,x2c,y2a,y2b,y2c; int x3a,x3b,x3c,y3a,y3b,y3c,x4a,x4b,x4c,y4a,y4b,y4c,x5a,x5b,x5c,y5a,y5b,y5c; int tx,shx,t,ch,shy; float ang,theta,sx,sy; int main(void) int gdriver = DETECT, gmode, errorcode; initgraph(&gdriver, &gmode,"c:\\tc\\bgi"); /* request for auto detection*/ printf("\n\t\t\t 2D Composite Transformations"); printf("\n\n Enter all coordinates values :"); scanf("%d %d %d %d %d %d",&xa,&ya,&xb,&yb,&xc,&yc); printf("\n\n The original Image"); /* get the coordinates for the original image*/ line(xa,ya,xb,yb); /* draw the original image*/ line(xb,yb,xc,yc); line(xc,yc,xa,ya); 42

printf("\n\n Enter the value tranlsation factor :"); /* get the translation factor*/ scanf("%d",&tx); 43

printf("\n\n After Translation "); x1a=xa+tx; x1b=xb+tx; x1c=xc+tx; y1a=ya; y1b=yb; y1c=yc; line(x1a,y1a,x1b,y1b); /* image after translation*/ line(x1b,y1b,x1c,y1c); line(x1c,y1c,x1a,y1a); delay(1); printf("\n\n Next Operation is Rotation"); printf("\n\n Enter the rotation angle :"); /* get the angle of rotation*/ scanf("%f",&ang); theta=((ang*3.14)/180); /* convert the angle*/ x2a=x1a*cos(theta)-y1a*sin(theta); y2a=x1a*sin(theta)+y1a*cos(theta); x2b=x1b*cos(theta)-y1b*sin(theta); y2b=x1b*sin(theta)+y1b*cos(theta); x2c=x1c*cos(theta)-y1c*sin(theta); y2c=x1c*sin(theta)+y1c*cos(theta); printf("\n\n After Rotation "); /* the rotated object*/ line(x2a,y2a,x2b,y2b); line(x2b,y2b,x2c,y2c); line(x2c,y2c,x2a,y2a); delay(1); printf("\n\n Next Operation is Scaling"); /* get the scale factor*/ printf("\n\n Enter the Scale factor :"); scanf("%f %f",&sx,&sy); x3a=x2a+sx; /* modify the objects coordinates based on the scale factor*/ y3a=y2a+sy; x3b=x2b+sx; 44

y3b=y2b+sy; x3c=x2c+sx; y3c=y2c+sy; printf("\n\n After Scaling "); line(x3a,y3a,x3b,y3b); line(x3b,y3b,x3c,y3c); line(x3c,y3c,x3a,y3a); delay(1); printf("\n\n Next Operation is Shearing"); printf("\n\n Enter 1 for x-axis \n 2 for y-axis: "); /* get the choice of shearing in the x or y axis*/ scanf("%d",&ch); if(ch==1) /* get the shear value*/ printf("\n\n Enter the x-shear (^.^) Value: "); scanf("%d",&shx); else printf("\n\n Enter the y-shear (^.^) Value: "); scanf("%d",&shy); if(ch==1) x3a=x3a+shx*y3a; y4a=y3a; x3b=x3a+shx*y3a; y4b=y3b; x3c=x3a+shx*y3a; y4c=y3c; else 45

46

x4a=x3a; y3a=y3a+shy*x3a; x4b=x3b; y3b=y3b+shy*x3b; x4c=x3c; y3c=y3c+shy*x3c; printf("\n\n After Shearing "); /* draw the final object after shearing*/ line(x3a,y3a,x3b,y3b); line(x3b,y3b,x3c,y3c); line(x3c,y3c,x3a,y3a); delay(1); printf("\n\n Next Operation is Reflection"); t=abs(y3ay3c); /* calculate the value for reflection*/ x5a=x3a; x5b=x3b; x5c=x3c; y5a=y3a+10+(2*t); y5b=y3b+10; y5c=y3c+10; printf("\n\n After Reflection "); /* the final object after all the transformations*/ line(x5a,y5a,x5b,y5b); line(x5b,y5b,x5c,y5c); line(x5c,y5c,x5a,y5a); getch(); closegraph(); return 0; 47

48

OUTPUT: 2D Composite Transformations Enter all coordinates values 213 236 253 321 256 214 The original Image Enter the value translation vector 32 After Translation Next Operation is Rotation Enter the rotation angle 20 After Rotation Next Operation is Scaling Enter the scale factor 10 5 49

50

After Scaling Next Operation is Shearing Enter 0 for x axis and 1 for y axis 0 Enter the x-shear value 1 After Shearing Enter 0 for x axis and 1 for y axis 1 Enter the y-shear value 1 Next Operation is Reflection After Reflection 51

RESULT: Thus the Two dimensional Composite transformations were successfully executed and the output is transformed, drawn and verified. 52

6. IMPLEMENTATION OF LINE, CIRCLE AND ELLIPSE ATTRIBUTES DESCRIPTION: Output primitives have geometric and non-geometric attributes. Geometric attributes, such as the character height, affect the size and shape of a primitive, whereas non-geometric attributes are qualities such as colour, line style, etc. The output primitives Such as Line, Circle and Ellipse are associated with set of attributes such as Line (color and Line Style), Cicrle (Color) and Ellipse (Color and Patterns). CODING: #include<graphics.h> /* include the necessary header files*/ #include<stdlib.h> #include<stdio.h> #include<conio.h> int main(void) /* select a driver and mode that supports */ /* multiple drawing colors.*/ int gdriver=ega,detect,gmode=egahi,errorcode; int color,maxcolor,x,y,s,ch,ch1,ch2,i; int midx,midy; int radius=100; int xradius=100,yradius=50; char msg[80]; char *lname[]="solid Line", "Dotted Line", "Center Line", "Dashed Line", "Usebit Line"; /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); /* read result of initialization */ errorcode=graphresult(); if (errorcode!=grok) /* an error occurred */ printf("graphics error: %s\n", grapherrormsg(errorcode)); 53

54

printf("press any key to halt:"); getch(); exit(1); /* terminate with an error code */ do printf("\n1.line\n2.circle\n3.ellipse\n"); /* get the user choice*/ printf("\nenter Your choice\n"); scanf("%d",&ch); switch(ch) case 1: printf("attribute: 1.Color 2.Style:\n"); scanf("%d",&ch1); switch(ch1) case 1: maxcolor=getmaxcolor(); /* use predefined methods to change the color*/ x=getmaxx()/2; y=getmaxy()/2; for(color=1;color<=maxcolor;color++) cleardevice(); setcolor(color); line(100,100,100,300); sprintf(msg,"color:%d",color); outtextxy(x,y,msg); getch(); closegraph(); break; case 2: 55

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

for(s=0;s<5;s++) setlinestyle(s,1,1); /* pre defined method for linestyle*/ line(20,20+s*50,120,120+s*50); outtextxy(125,120+s*50,lname[s]); getch(); closegraph(); break; break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; maxcolor=getmaxcolor(); /* draw circles of different colors*/ for(color=1;color<=maxcolor;color++) cleardevice(); setcolor(color); /* draw the circle */ circle(midx,midy,radius); /* clean up */ getch(); closegraph(); break; case 3: printf("\n1.pattern 2.colour\n"); /* get choice for color or style of eclipse*/ printf("\nenter your choice:\n"); scanf("%d",&ch2); 57

switch(ch2) 58

default: case 1: /* initialize graphics and local variables */ initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; /* loop through the fill patterns */ for(i=empty_fill;i<user_fill;i++) /* set fill pattern */ setfillstyle(i,getmaxcolor()); /* draw a filled ellipse */ fillellipse(midx, midy, xradius, yradius); getch(); closegraph(); break; case 2: initgraph(&gdriver,&gmode,"c:\\tc\\bgi"); maxcolor=getmaxcolor(); for(color=1;color<=maxcolor;color++) cleardevice(); setcolor(color); ellipse(100,200,0,360,xradius,yradius); getch(); /* clean up */ closegraph(); break; 59

60

while(ch==3); return 0; //exit(0); break; OUTPUT: Line Color Line Style 61

62

Circle Color Ellipse Pattern Ellipse Color 63

64

RESULT: verified. Thus the attributes were successfully applied to Line, circle and ellipse and the output is drawn and 65

7. IMPLEMENTATION OF SUTHERLAND HODGEMAN POLYGON CLIPPING ALGORITHM DESCRIPTION: The Sutherland Hodgeman Algorithm is used for clipping polygons. It works by extending each line of the convex clip polygon in turn and selecting only vertices from the subject polygon that is on the visible side. This algorithm performs a clipping of a polygon against each window edge in turn. It accepts an ordered sequence of vertices v1, v2, v3... vn and puts out a set of vertices defining the clipped polygon. CODING: #include<stdio.h> /* include the necessary header files*/ #include<graphics.h> #include<conio.h> #include<math.h> #include<process.h> #define TRUE 1 #define FALSE 0 typedef unsigned int outcode; outcode CompOutCode(float x,float y); /* create an user defined function for the output*/ enum TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 ; float xmin,xmax,ymin,ymax; void clip(float x0,float y0,float x1,float y1) /* define the clipping function*/ outcode outcode0,outcode1,outcodeout; int accept=false,done=false; outcode0=compoutcode(x0,y0); /* call the user defined function*/ 66

67

outcode1=compoutcode(x1,y1); do /* assign the values for the condition variables*/ if(!(outcode0 outcode1)) accept=true; done=true; else if(outcode0&outcode1) done=true; else float x,y; outcodeout=outcode0?outcode0:outcode1; /* use the tertiary operator to assign values*/ if(outcodeout&top) /* reassign the value of x and y */ x=x0+(x1-x0)*(ymax-y0)/(y1-y0); y=ymax; else if(outcodeout&bottom) x=x0+(x1-x0)*(ymin-y0)/(y1-y0); y=ymin; else if(outcodeout&right) y=y0+(y1-y0)*(xmax-x0)/(x1-x0); x=xmax; 68

69

else y=y0+(y1-y0)*(xmin-x0)/(x1-x0); x=xmin; if(outcodeout==outcode0) x0=x; y0=y; outcode0=compoutcode(x0,y0); else x1=x; y1=y; outcode1=compoutcode(x1,y1); while(done==false); if(accept) line(x0,y0,x1,y1); outtextxy(150,20,"polygon AFTER CLIPPING"); rectangle(xmin,ymin,xmax,ymax); /* draw the clipping window*/ outcode CompOutCode(float x,float y) /* define the output function*/ outcode code=0; if(y>ymax) code =TOP; else if(y<ymin) 70

code =BOTTOM; if(x>xmax) code =RIGHT; else if(x<xmin) code =LEFT; return code; void main() float x1,y1,x2,y2; /* request auto detection */ int gdriver=detect,gmode,n,poly[14],i; clrscr( ); printf("enter the no of sides of polygon:"); /* get the sides of the polygon*/ scanf("%d",&n); printf("\nenter the coordinates of polygon\n"); for(i=0;i<2*n;i++) scanf("%d",&poly[i]); poly[2*n]=poly[0]; poly[2*n+1]=poly[1]; printf("enter the rectangular coordinates of clipping window\n"); scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax); /* get the coordinates of the clipping window*/ /* initialize graphics and local variables */ initgraph(&gdriver, &gmode, "c:\\tc\\bgi"); outtextxy(150,20,"polygon BEFORE CLIPPING"); drawpoly(n+1,poly); rectangle(xmin,ymin,xmax,ymax); getch(); cleardevice(); 71

for(i=0;i<n;i++) getch(); clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]); /* call the clipping function*/ restorecrtmode(); OUTPUT: Enter the Sides of the Polygon: 5 Enter the co-ordinates of the Polygon 80 50 200 100 350 350 80 200 40 80 Enter the rectangular co-ordinates 150 150 300 300 RESULT: Thus the Sutherland Hodgeman polygon clipping algorithm was successfully executed and the output is drawn and verified. 72

73

74