Program #1 Implementation of line generation using DDA algorithms #include<stdio.h> #include<graphics.h> #include<math.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,length; int i,gd,gm; clrscr(); /* Read two end points of line---------------------------------- */ printf("Enter the value of x1 :\t"); scanf("%f",&x1); printf("Enter the value of y1 :\t"); scanf("%f",&y1); printf("Enter the value of x2 :\t"); scanf("%f",&x2); printf("Enter the value of y2 :\t"); scanf("%f",&y2); /* Initialise graphics mode---------------------------------- */ detectgraph(&gd,&gm); initgraph(&gd,&gm,""); dx=abs(x2-x1); dy=abs(y2-y1);
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Program #1Implementation of line generation using DDA
/* Read two end points of line---------------------------------- */
printf("Enter the value of x1 :\t");scanf("%f",&x1);printf("Enter the value of y1 :\t");scanf("%f",&y1);printf("Enter the value of x2 :\t");scanf("%f",&x2);printf("Enter the value of y2 :\t");scanf("%f",&y2);
x = x1 + 0.5; /* Factor 0.5 is added to round the values */y = y1 + 0.5; /* Factor 0.5 is added to round the values */
i = 1; /* Initialise loop counter */while(i <= length) { putpixel(x,y,15); x = x + dx; y = y + dy; i = i + 1; delay(100); /* Delay is purposely inserted to see observe the line drawing process */ }getch();closegraph();
/* Read two end points of line---------------------------------- */printf("Enter the value of x1 :\t");scanf("%f",&x1);printf("Enter the value of y1 :\t");scanf("%f",&y1);printf("Enter the value of x2 :\t");scanf("%f",&x2);printf("Enter the value of y2 :\t");scanf("%f",&y2);
putpixel(200-y,200-x,15);putpixel(200-y,200+x,15);putpixel(200-x,200+y,15);if (d <= 0){d = d + 4*x + 6;}else{d = d + 4*(x-y) + 10;y = y - 1;}x = x + 1;delay(1000); /* Delay is purposely inserted to see
observe the line drawing process */}while(x < y);
getch();closegraph();
}
Program #5Implementation of 2D transformation: Translation, Scaling, Rotation, Mirror Reflection and Shearing
/* Read two end points of the line------------------------------------ */printf("Enter the co-ordinates for the line:\n");printf("X1: ");scanf("%f", &x1);printf("Y1: ");scanf("%f", &y1);printf("X2: ");scanf("%f", &x2);printf("Y2: ");scanf("%f", &y2);
/* Enter the thickness of the line------------------------------------*/printf("Enter the required thickness: ");scanf("%d", &thickness);cleardevice();line (x1, y1, x2, y2);if ((y2 - y1) / (x2 - x1) < 1){
for(i = 0; i < wx; i++){line(x1 - i, y1, x2 - i, y2);line(x1 + i, y1, x2 + i, y2);}
}printf("This is the line of thickness %d units.\n", thickness);getch();
}
SCALING#include <stdio.h>#include <graphics.h># include <conio.h>main(){int gdriver;int gmode,maxx,maxy,i;
int tri [58] [2];int tri1 [58] [2] ={0,8,7,8,0,8,8,6,7,8,8,12,7,8,10,5,8,12,21,12,21,12,31,27,31,27,53,36,53,36,50,30,50,30,54,32,54,32,53,29,53,29,55,30,55,30,53,26,27,13,38,25,
case 1: cleardevice(); /* Line completely invisible */
drawwindow();break;
case 2: cleardevice(); /* line partly visible */p1=resetendpt (p1,p2);p2=resetendpt(p2,p1);drawwindow();drawline(p1,p2,15);break;
}getch();closegraph();return(0);}/* Function to draw window */void drawwindow(){setcolor(RED);line(150,100,450,100);line(450,100,450,350);line(450,350,150,350);line(150,350,150,100);}/* Function to draw line between two points---------------------------------------------*/void drawline (PT p1,PT p2,int cl){setcolor(cl);line(p1.x,p1.y,p2.x,p2.y);
}/* Function to set code of the coordinates--------------------------------------------*/PT setcode(PT p){PT ptemp;if(p.y<100)ptemp.code[0]='1'; /* TOP */elseptemp.code[0]='0';if(p.y>350)ptemp.code[1]='1'; /* BOTTOM */elseptemp.code[1]='0';if (p.x>450)ptemp.code[2]='1'; /* RIGHT */elseptemp.code[2]='0';if (p.x<150) /* LEFT */ptemp.code[3]='1';elseptemp.code[3]='0';ptemp.x=p.x;ptemp.y=p.y;return(ptemp);}/* Function to determine visibility of line--------------------------------------------*/int visibility (PT p1,PT p2){int i,flag=0;for(i=0;i<4;i++){if((p1.code[i]!='0')||(p2.code[i]!='0'))flag=1;}if(flag==0)return(0);