Graphics
Graphics
Exercise-2:
/*Digital Differential Analyser (DDA) Line Generation*/
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void linkfloat()
{
float a=0,*b;
b=&a;
a=*b;
}
void main()
{
int gd=DETECT,gm,x,y,i;
int x1=20,y1=50,x2=70,y2=90;
int dx,dy,steps;
float xinc,yinc;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dy=y2-y1;
dx=x2-x1;
if(abs(dy)>abs(dx))
steps=abs(dy);
else
steps=abs(dx);
xinc=(float)dx/steps;
yinc=(float)dy/steps;
printf("%f and %f",xinc,yinc);
x=x1;
y=y1;
for(i=1;i<=steps;i++){
putpixel(x,y,RED);
x=(int)(x+xinc+0.5);
y=(int)(y+yinc+0.5);
delay(100);
}
getch();
}
Exercise-3:
/*Bresenham Line Generation*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void linkfloat()
{
float a,*b;
b=&a;
a=*b;
}
void main()
{
int gdriver=DETECT,gmode;
int i,x,y,x1,y1,x2,y2,dx,dy,p,steps;
float xinc,yinc;
clrscr();
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
x1=100;
y1=100;
x2=200;
y2=170;
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
while(x<=x2)
{
putpixel(x,y,5);
x++;
if(p<0)
p=p+2*dy;
else
{
p=p+2*dy-2*dx;
y++;
}
}
getch();
closegraph();
}
Exercise-4:
/*Bresenham circle Generation*/
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main(){
int gdriver=DETECT,gmode;
int x,y,r,d,h=100,k=100;
clrscr();
initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi");
x=0;
y=30;
r=30;
d=1-r;
while(x<y){
putpixel(x+h,y+k,RED);
putpixel(-y+h,x+k,YELLOW);
putpixel(y+h,x+k,CYAN);
putpixel(-x+h,y+k,BLUE);
putpixel(-x+h,-y+k,GREEN);
putpixel(-y+h,-x+k,MAGENTA);
putpixel(y+h,-x+k,5);
putpixel(x+h,-y+k,WHITE);
if(d<0)
d=d+2*x+3;
else{
d=d+2*x-2*y+5;
y=y-1;}
x=x+1;
delay(200);
}
getch();
closegraph();
}
Exercise-5:
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;
}
}
/*outer loop is for y scanline,
and it checks for intersecting points*/
for(y=0;y<480;y++)
{
k=0;
for(i=0;i<n;i++)
{
/*condition for checking intersection*/
if(((a[i][1]<=y)&&(a[i+1][1]>y))||((a[i][1]>y)&&(a[i+1][1]<=y
)))
{
/*calculate the x-coordinate, y-a[i][1] finds the vertical
distance between the current scanline y and the y-cordinate
of the point*/
xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));
k++;
}}
/*sort the x-cordinate values*/
for(j=0;j<k-1;j++)
for(i=j+1;i<k;i++)
{
if(xi[i]>xi[i+1])
{
temp=xi[i];
xi[i]=xi[i+1];
xi[i+1]=temp;
}
}
setcolor(5);
/*use x-cordinate values in pairs*/
for(i=0;i<k;i+=2)
{
line(xi[i],y,xi[i+1]+1,y);
getch();
}
}
}
Exercise-6:
/*Line Clipping using Cohen Sutherland*/
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int
rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_cod
e[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n enter XMin, YMin =");
scanf("%d %d",&W_xmin,&W_ymin);
printf("\n enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter end point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
if(y>W_ymax) {
rcode_begin[0]=1; // Top
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1; // Bottom
flag=1;
}
if(x>W_xmax) {
rcode_begin[2]=1; // Right
flag=1;
}
if(x<W_xmin) {
rcode_begin[3]=1; //Left
flag=1;
}
//end point of Line
if(y1>W_ymax){
rcode_end[0]=1; // Top
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1; // Bottom
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1; // Right
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1; //Left
flag=1;
}
if(flag==0)
{
printf("No need of clipping as it is already in window");
getch();
exit(0);
}
else{
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
getch();
exit(0);
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[3]==1) //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;
}
if(rcode_begin[2]==1) // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;
}
if(rcode_begin[0]==1) // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;
}
if(rcode_begin[1]==1) // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;
}
// end points
if(rcode_end[3]==1) //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;
}
if(rcode_end[2]==1) // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;
}
if(rcode_end[0]==1) // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;
}
if(rcode_end[1]==1) // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;
}
}
}
getch();
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}