Transcript
Page 1: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

/* LAB1_1: To study the various graphics commands in C language */

/* drawing basic shapes */

#include<graphics.h>

#include<conio.h>

void main()

{

int gd=DETECT, gm; int poly[12]={350,450, 350,410, 430,400, 350,350,

300,430, 350,450 };

initgraph(&gd, &gm, "");

circle(100,100,50);

outtextxy(75,170, "Circle"); rectangle(200,50,350,150);

outtextxy(240, 170, "Rectangle");

ellipse(500, 100,0,360, 100,50); outtextxy(480, 170, "Ellipse");

line(100,250,540,250);

outtextxy(300,260,"Line");

sector(150, 400, 30, 300, 100,50);

outtextxy(120, 460, "Sector"); drawpoly(6, poly);

outtextxy(340, 460, "Polygon");

getch(); closegraph();

}

/* LAB1_2: To study the various graphics commands in C language */ /* some graphics effects using random numbers */

#include "graphics.h"

#include "conio.h" #include "stdlib.h"

void main() {

int gd,gm;

gd=DETECT;

initgraph(&gd, &gm, "");

setcolor(3); setfillstyle(SOLID_FILL,RED);

bar(50, 50, 590, 430);

setfillstyle(1, 14);

bar(100, 100, 540, 380);

Lab Programs

Page 2: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L2 Lab Programs

while(!kbhit()) {

putpixel(random(439)+101, random(279)+101,random(16));

setcolor(random(16)); circle(320,240,random(100));

}

getch(); closegraph();

}

/* LAB2: Code for drawing line using simple DDA algorithm */

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int graphdriver, graphmode, i;

float x,y,x1,y1,x2,y2,dx,dy,delx,dely,length;

/* Input end points (coordinates) of line segment*/

printf("Enter the coordinates of end points:");

scanf("%f %f %f %f",&x1,&y1,&x2,&y2);

/* Initialize graphics mode*/

detectgraph(&graphdriver,&graphmode);

initgraph(&graphdriver,&graphmode,"C:\\TC\\BGI");

dx = x2-x1; /*calculate dx and dy */

dy = y2-y1;

if(abs(dx) >= abs(dy)) /*calculate line length estimate*/

length = dx;

else

length = dy;

delx=dx/length; /*calculate delta x and y*/

dely=dy/length;

x=x1+0.5; /* initialize initial points to round the values*/

y=y1+0.5;

i=1;

/* Obtain the next pixel and paint*/

while(i<=length)

{

Page 3: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L3

putpixel(x,y,15);

x=x+delx;

y=y+dely;

i=i+1;

delay(50);

}

getch();

closegraph(); /*close graphics mode */

}

/* LAB3: Code for drawing line using Bresenham’s line drawing algorithm */

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void main()

{

float x,y,x1,y1,x2,y2,dx,dy,err;

int graphdriver,graphmode,i;

/* Input the line coordinates*/

printf("\n\n Enter the end points of a line: ");

scanf("%f %f %f %f",&x1,&y1,&x2,&y2);

//Initialize graphics mode

detectgraph(&graphdriver,&graphmode);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

dx=abs(x2-x1); /*calculate dx and dy*/

dy=abs(y2-y1);

x=x1; y=y1; /* initialize x and y and error term*/

err=(dy/dx)-0.5;

putpixel(x,y,15);

/* Obtain next pixels and paint*/

for(i=1;i<=dx;i++)

{

while(err>=0)

{

y=y+1;

err=err-1;

}

x=x+1;

err=dy/dx+err;

putpixel(x,y,15);

Page 4: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L4 Lab Programs

} getch();

closegraph();

}

/* LAB4: Code for drawing circle using Bresenham’s circle algorithm */

#include<stdio.h>

#include<graphics.h>

#include<math.h>

void main()

{

int x,y,radius,del, graphdriver,graphmode,i;

/* Input the line coordinates*/

printf("\n\n Enter the radius of circle: ");

scanf("%d ",&radius);

/*Initialize graphics mode*/

detectgraph(&graphdriver,&graphmode);

initgraph(&graphdriver,&graphmode,"c:\\tc\\bgi");

del=3-2*radius; /* calculate base decision factor*/

x=0; y=radius; /* set base values of coordinates*/

/* draw the x-y axis at the center of the screen */

line(getmaxx()/2,0,getmaxx()/2,getmaxy());

line(0,getmaxy()/2,getmaxx(),getmaxy()/2);

/* Obtain next pixels and paint*/

do

{

putpixel(getmaxx()/2+x,getmaxy()/2+y,5);

putpixel(getmaxx()/2+y,getmaxy()/2+x,5);

putpixel(getmaxx()/2+y,getmaxy()/2-x,5);

putpixel(getmaxx()/2+x,getmaxy()/2-y,5);

putpixel(getmaxx()/2-x,getmaxy()/2-y,5);

putpixel(getmaxx()/2-y,getmaxy()/2-x,5);

putpixel(getmaxx()/2-y,getmaxy()/2+x,5);

putpixel(getmaxx()/2-x,getmaxy()/2+y,5);

if(del<0)

del=del+4*x+6;

else

{

del=del+4*(x-y)+10;

y=y-1;

}

Page 5: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L5

x=x+1;

}while(x<y);

getch();

closegraph();

}

/* LAB5: C program for to display different types of lines */ #include <graphics.h>

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <conio.h>

/* store line styles */

char *lname[] = {

"SOLID_LINE",

"DOTTED_LINE",

"CENTER_LINE",

"DASHED_LINE",

"USERBIT_LINE"

};

int main(void)

{

int gdriver = DETECT, gmode, errorcode;

int style, midx, midy, userpat;

char stylestr[40];

/* initialize graphics and local variables */

initgraph(&gdriver, &gmode, "..//bgi");

midx = getmaxx() / 2;

midy = getmaxy() / 2;

/* a user defined line pattern */

/* binary: "0000000000000001" */

userpat = 1;

for (style=SOLID_LINE; style<=USERBIT_LINE; style++)

{

/* select the line style */

setlinestyle(style, userpat, 1);

Page 6: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L6 Lab Programs

/* convert style into a string */

strcpy(stylestr, lname[style]);

/* draw the line */

line(0, 0, midx-10, midy);

/* output the line style message */

outtextxy(midx, midy, stylestr);

/* wait for a key */

getch();

cleardevice();

}

closegraph();

return 0;

}

/* LAB6: Code for performing 2D transformations - scaling, rotation, reflection and translation - on the user-defined polygonal figure */

#include <stdlib.h>

#include <graphics.h>

#include <stdio.h>

#include <math.h>

#include <conio.h>

#define PI 3.14

float cord[10][3], ans[10][3]; /* storage for coordinate vertices*/

int n; /* number for defining coordinate vertices*/

float tMat[3][3]={1,0,0,

0,1,0,

0,0,1 };

/*obtain the defining vertices of the figure for transformation*/

void getPoints()

{

int i;

printf("\nEnter the no of defining vertices: ");

scanf("%d",&n);

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

{

printf("\nEnter coordinate (%d)of the defining figure: ",i+1);

scanf("%f %f",&cord[i][0],&cord[i][1]);

cord[i][2]=1;

}

Page 7: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L7

}

/* draw the polygon with coordinates*/

void drawPolygon(float ans[][3])

{

int i;

line(getmaxx()/2, 0, getmaxx()/2, getmaxy());

line(0,getmaxy()/2, getmaxx(), getmaxy()/2);

for(i=0;i<n-1;i++)

line(getmaxx()/2+ans[i][0],getmaxy()/2-ans[i][1],getmaxx()/2+ans[i+1][0],

getmaxy()/2-ans[i+1][1]);

line(getmaxx()/2+ans[0][0],getmaxy()/2-

ans[0][1],getmaxx()/2+ans[i][0],getmaxy()/2-ans[i][1]);

}

/* Routine for mattrix multiplication*/

void mul(float mat1[][3],int n,float mat2[][3],float ans[][3])

{

int i,j,k;

float sum;

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

{

sum=0;

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

{

sum=0;

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

sum+=mat1[i][k]*mat2[k][j];

ans[i][j]=sum;

}

}

}

/* Perform translation with factors tx and ty*/

void translate(int tx,int ty,int draw)

{

int i;

tMat[2][0]=tx;

tMat[2][1]=ty;

mul(cord,n,tMat,ans);

if(draw) drawPolygon(ans);

}

/*Perform scaling with factors sx and sy*/

void scale(float sx,float sy)

{

Page 8: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L8 Lab Programs

tMat[0][0]=sx;

tMat[1][1]=sy;

mul(cord,n,tMat,ans);

drawPolygon(ans);

}

/*Perform reflection about x-axis/ y-axis*/

void reflect(char axis)

{

if(axis=='x')

tMat[1][1]=-1;

else

tMat[0][0]=-1;

mul(cord,n,tMat,ans);

drawPolygon(ans);

}

/* Perform rotation with reference coord by an angle ‘angle’*/

void rotate(float angle)

{

int i;

angle*=PI/180;

tMat[0][0]=cos(angle);

tMat[0][1]=sin(angle);

tMat[1][0]=-sin(angle);

tMat[1][1]=cos(angle);

mul(cord,n,tMat,ans);

drawPolygon(ans);

}

/* main routine integrating transformations scaling, translation, reflection and rotation

*/

void main()

{

/* define storage variables*/

float sx,sy,tx,ty,r;

int ctr,i, choice, gdriver = DETECT, gmode;

char axis;

fflush(stdin);

/* detect an initialize graphics mode*/

detectgraph(&gdriver,&gmode);

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

Page 9: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L9

printf("Choose from the following 2D transformations\n");

printf("1- Translation\n");

printf("2- Scaling\n");

printf("3- Rotation \n");

printf("4- Reflection about x-axis\n");

printf("5- Reflection about y-axis\n");

printf("0- Exit\n");

printf("Enter your choice: ");

scanf("%d",&choice);

switch(choice)

{

case 1 :

getPoints(); /* obtain coordinates for defining figure*/

printf("\nEnter the translating co-ordinates: Tx Ty");

scanf("%f %f",&tx,&ty);

cleardevice();

drawPolygon(cord); /* draw the polygon*/

translate(tx,ty,1);

getch();

break;

case 2 : getPoints(); /* obtain coordinates for defining figure*/

printf("\nEnter the Scaling factors: Sx Sy");

scanf("%f %f",&sx,&sy);

cleardevice();

drawPolygon(cord); /* draw the polygon*/

scale(sx,sy);

getch();

break;

case 3: getPoints(); /* obtain coordinates for defining figure*/

printf("\nEnter the Rotating angle:");

scanf("%f",&r);

cleardevice();

drawPolygon(cord); /* draw the figure*/

rotate(r);

getch();

break;

case 4: getPoints();

cleardevice();

drawPolygon(cord); /* draw the figure */

axis='x';

reflect(axis);

getch();

break;

case 5: getPoints();

cleardevice();

drawPolygon(cord); /* draw the figure*/

Page 10: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L10 Lab Programs

axis='y';

reflect(axis);

getch();

break;

case 0 : exit(0);

}

}

/* LAB7_1: Code for implementing line clipping using the Cohen–Sutherland algorithm */

#include<stdio.h>

#include<graphics.h>

/*define outcodes*/

typedef unsigned int outcode;

enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };

/* routine for clipping against the defining window*/

void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )

float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;

{

int graphdriver,graphmode;

outcode code0,code1,codeout;

int accept = 0, done=0;

/* calculate the clipping code */

code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

do

{

if(!(code0 | code1))

{

accept =1 ; done =1;

}

else

if(code0 & code1) done = 1;

else

{

float x,y;

codeout = code0 ? code0 : code1;

if(codeout & TOP)

{

x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);

y = ywmax;

Page 11: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L11

}

else

if( codeout & BOTTOM)

{

x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);

y = ywmin;

}

else

if ( codeout & RIGHT)

{

y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);

x = xwmax;

}

else

{

y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);

x = xwmin;

}

if( codeout == code0)

{

x0 = x; y0 = y;

code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);

}

else

{

x1 = x; y1 = y;

code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);

}

}

} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}

/* routine to calculate code of point*/

int calcode (x,y,xwmin,ywmin,xwmax,ywmax)

float x,y,xwmin,ywmin,xwmax,ywmax;

{

int code =0;

if(y> ywmax)

code |=TOP;

else if( y<ywmin)

code |= BOTTOM;

Page 12: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L12 Lab Programs

else if(x > xwmax)

code |= RIGHT;

else if ( x< xwmin)

code |= LEFT;

return(code);

}

/* main routine*/

void main()

{

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;

int graphdriver,graphmode;

/* Initialize graphics mode and driver*/

detectgraph(&graphdriver,&graphmode);

initgraph(&graphdriver,&graphmode,"C:\\TC\\BGI");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");

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

printf("\n\n\tX2 Y2 : ");

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

printf("\n\tEnter the co_ordinates of window :\n ");

printf("\n\txwmin , ywmin : ");

scanf("%f %f",&xwmin,&ywmin);

printf("\n\txwmax , ywmax : ");

scanf("%f %f",&xwmax,&ywmax);

getch();

cleardevice();

line(x1,y1,x2,y2);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );

getch();

closegraph();

}

Page 13: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L13

/* LAB7_2: Code for implementing the Liang–Barsky line clipping algorithm */

#include<graphics.h>

#include<stdio.h>

#include<dos.h>

#include<conio.h>

#include<stdlib.h>

void main()

{

int gd, gm,x1,y1,x2,y2,x11,y11,x22,y22;

int wxleft,wytop,wxright,wybottom ;

float u1 = 0.0 , u2 = 1.0 ;

int p1,q1,p2,q2,p3,q3,p4,q4 ;

float r1,r2,r3,r4 ;

detectgraph(&gd,&gm);

clrscr();

printf("Enter the windows left , top boundry coordinates:\n");

scanf("%d%d",&wxleft,&wytop);

printf("Enter the windows right ,bottom boundry coordinates:\n");

scanf("%d%d",&wxright,&wybottom);

printf("Enter line x1 , y1 co-ordinate\n");

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

printf("Enter line x2 , y2 co-ordinate\n");

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

p1 = -(x2 - x1);

q1 = x1 - wxleft;

p2 = (x2 - x1);

q2 = wxright - x1;

p3 = -(y2 - y1);

q3 = y1 - wytop;

p4 = (y2 - y1);

q4 = wybottom - y1;

if(((p1==0.0)&&(q1<0.0))||((p2==0.0)&&(q2<0.0))||((p3==0.0)&&(q3<0.0))||((p4 ==

0.0) && (q4<0.0)))

{

printf("Line is rejected\n");

Page 14: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L14 Lab Programs

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

setcolor(2);

rectangle(wxleft,wybottom,wxright,wytop);

setcolor(1);

line(x1,y1,x2,y2);

getch();

setcolor(0);

line(x1,y1,x2,y2);

getch();

}

else

{

if(p1!= 0.0)

{

r1 =(float) q1 /p1 ;

if( p1 < 0 )

u1 = max(r1,u1 );

else

u2 = min(r1,u2 );

}

if(p2!= 0.0)

{

r2 = (float)q2/p2 ;

if( p2 < 0 )

u1 = max(r2,u1 );

else

u2 = min(r2,u2 );

}

if(p3!= 0.0)

{

r3 = (float)q3/p3 ;

if(p3<0)

u1 = max(r3,u1);

else

u2 = min(r3,u2);

}

if(p4!= 0.0)

{

r4 = (float)q4/p4 ;

if(p4<0)

u1 = max(r4,u1);

else

u2 = min(r4,u2);

}

if(u1 >u2)

printf("line rejected\n");

else

Page 15: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L15

{

x11 = x1 + u1 * ( x2 - x1 ) ;

y11 = y1 + u1 * ( y2 - y1 ) ;

x22 = x1 + u2 * ( x2 - x1 );

y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");

printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);

printf("Windows coordiante are \n");

printf("wxleft= %d, wytop= %d,wxright= %d, wybottom= %d

",wxleft,wytop,wxright,wybottom);

printf("\nNew coordinates are \n");

printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);

getch();

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

setcolor(2);

rectangle(wxleft,wybottom,wxright,wytop);

setcolor(1);

line(x1,y1,x2,y2);

getch();

setcolor(0);

line(x1,y1,x2,y2);

setcolor(3);

line(x11,y11,x22,y22);

getch();

}

}

}

/* LAB8: Code for implementing the Sutherland polygon clipping algorithm */

#include<graphics.h>

#include<stdio.h>

#include<conio.h>

#include<dos.h>

union REGS i,o;

struct pt

{

int x,y;

};

Page 16: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L16 Lab Programs

float slope[20];

int button=0,xc,yc,n=0,k,dy,dx,x,y,xl,xr,yt,yb,m,temp,a[20][2],xi[20];

struct point

{

float x,y;

};

enum bound {left,right,bottom,top};

int inside(struct point p, enum bound b)

{

int c=1;

switch(b)

{

case left : if(p.x<xl)

c=0;

break;

case right : if(p.x>xr)

c=0;

break;

case bottom : if(p.y>yb)

c=0;

break;

case top : if(p.y<yt)

c=0;

break;

}

return(c);

}

struct point intersect (struct point p1,struct point p2,enum bound b)

{

struct point t;

float m=0;

if(p2.x!=p1.x) m=(p2.y - p1.y)/(p2.x-p1.x);

switch(b)

{

case left : t.x =xl;

t.y = p2.y+(xl-p2.x)*m;

break;

case right: t.x=xr;

t.y = p2.y + (xr-p2.x)*m;

break;

case bottom: t.y = yb;

if(p1.x==p2.x) t.x=p2.x;

Page 17: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L17

else t.x = p2.x +(yb-p2.y)/m;

break;

case top : t.y = yt;

if(p1.x==p2.x) t.x=p2.x;

else

t.x = p2.x +(yt-p2.y)/m;

break;

}

return t;

}

/*initialize mouse*/

initmouse()

{

i.x.ax=0;

int86(0x33,&i,&o);

return(o.x.ax);

}

/* show mouse pointer */

showmouseptr()

{

i.x.ax=1;

int86(0x33,&i,&o);

return(0);

}

/* hide mouse pointer */

hidemouseptr()

{

i.x.ax=2;

int86(0x33,&i,&o);

return(0);

}

/* obtain mouse position on click */

getmousepos(int *button,int *x,int *y)

{

i.x.ax=3;

int86(0x33,&i,&o);

*button=o.x.bx;

*x=o.x.cx;

*y=o.x.dx;

return;

}

void main()

{

enum bound b;

int cou,i,gd=DETECT,gm;

Page 18: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L18 Lab Programs

struct point p[30],pout[30],z;

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

cleardevice();

printf("Enter the clipping window vertices-xl, yt, xr, yb:");

scanf("%d %d %d %d",&xl,&yt,&xr,&yb);

rectangle(xl,yt,xr,yb);

printf("\nDefine polygon to clip (click right button to complete:");

showmouseptr();

while(button!=2) /*Check for right button click*/

{

getmousepos(&button,&xc,&yc);

if(button==1)

{

p[n].x=xc;

p[n].y=yc;

n++;

hidemouseptr();

circle(xc,yc,2);

if(n>1)

line(p[n-2].x,p[n-2].y,xc,yc);

showmouseptr();

delay(20);

}

}

p[n]=p[0];

hidemouseptr();

line(p[n-1].x,p[n-1].y,p[n].x,p[n].y);

showmouseptr();

getmousepos(&button,&xc,&yc);

button=0;

for(b=left;b<=top;b++)

{

cou =-1;

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

if((inside(p[i],b)==0) && (inside(p[i+1],b)==1))

{

z=intersect(p[i],p[i+1],b);

pout[++cou] =z;

pout[++cou]=p[i+1];

}

else

if((inside(p[i],b)==1)&&(inside(p[i+1],b)==1)) pout[++cou]=p[i+1];

else

if((inside(p[i],b)==1)&&(inside(p[i+1],b)==0))

{

z = intersect(p[i],p[i+1],b);

Page 19: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L19

pout[++cou]=z;

}

pout[++cou]=pout[0];

n=cou;

for(i=0;i<=n;i++) p[i]=pout[i];

}

getch();

cleardevice();

rectangle(xl,yt,xr,yb);

for(i=0;i<n;i++) line(p[i].x,p[i].y,p[i+1].x,p[i+1].y);

getch();

}

/* LAB9: MATLAB commands with images */

1. Storing an Image

2. Negative of an Image

3. RGB Components of an Image

4. Gamma Scaling of an Image

5. Convert Image to Grayscale

7. Brightening an Image

*/

TestImage = imread(‘C:\pictures\flowers.tiff’); % Storing an Image

imshow(TestImage); % view stored image

% convert image to its negative

ImgNeg = double(TestImage); % Converts the image matrix to double

ImgNegScale = 1.0/max(ImgNeg(:)); % compute the max value in array and take its

inverse

ImgNeg = 1 - ImgNeg*ImgNegScale; % Multiply the double image

% matrix by the factor of

ImgNegScale and subtract the total from 1

figure; % Draw the figure

imshow(ImgNeg); % Show the new image

% RGB Components of an Image

ImageRed = TestImage; % Create a new matrix equal to the matrix of your original

image.

ImageRed (:, :, 2:3) = 0; % nullifies the second and third columns of the

colormap matrix which are part

%of the original matrix.

imshow(ImageRed); % show the ImageRed that you just created.

% Display blue image

Page 20: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

L20 Lab Programs

ImageBlue = TestImage; % Create a new matrix equal to the matrix of your

original image.

ImageBlue(:, :, 1:2) = 0; % Note the difference in column numbers.

imshow(ImageBlue); % Display the ImageBlue you just created.

% Gamma Scaling of an Image

% format is J = imadjust(I, [low high], [bottom top], gamma);

NewImage = imadjust(TestImage, [.2, .7], []); % original image has gamma at default

figure;

imshow(NewImage);

NewImage = imadjust(TestImage, [.2, .7], [], .2);

figure;

imshow(NewImage); % gamma has been changed

%Converting an Image to Grayscale

grayimage = rgb2gray(TestImage);

figure;

imshow(grayimage);

%Brightening an Image

brighten(beta); % Set beta to any value between -1.0 and 1.0

/* LAB10_1: Program to convert image to negative */

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

FILE *fp;

int gd=DETECT, gm, xsize, ysize, i, j, c;

struct palettetype pal;

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

fp = fopen("taj.tif", "rb");

if(fp==NULL)

{

printf("*** Program to convert image to negative ***\n\n");

printf("\nSorry, the file does not exist.");

}

else

{

Page 21: Lab Programs - Wiley India · L4 X Lab Programs } getch(); closegraph(); } /* LAB4: Code for drawing circle using Bresenham’s circle algorithm */ #include #include

Lab Programs L21

floodfill(0, 0, RED);

setcolor(BLUE);

fseek(fp, 30, SEEK_SET);

xsize = getw(fp);

fseek(fp, 42, SEEK_SET);

ysize = getw(fp);

fseek(fp, 864, SEEK_SET);

/* grab a copy of the palette */

getpalette(&pal);

/* create gray scale */

for(i=0; i<pal.size; i++)

setrgbpalette(pal.colors[i], i*4, i*4, i*4);

outtextxy(20, 10, "*** Program to convert image to negative ***");

for(i=0; i<ysize; i++) //Displaying negated image

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

{

c = fgetc(fp);

c = 256-c-1;

putpixel(j+35, i+50, c/16);

}

}

getch();

fclose(fp);

closegraph();

}

/* LAB10_2: MATLAB code to computer Negative of Image */

%Matlab Code:

clear all; close all; % clear workspace

img = imread('rice.jpg'); % read image file

img2 = 1 - im2double(img); % compute negative

/% draw the figures

figure; subplot(1,2,1); imshow(img); title('Original Image');

subplot(1,2,2); imshow(img2); title('Image after Negative Transform');


Recommended