Friday, April 5, 2013

Sutherland-Hodgeman Polygon Clipping Program in C


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;

void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin  && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}

void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax  && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}

void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax  && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}

void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin  && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}

void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin:");
scanf("%f%f",&xmin,&ymin);
printf("Coordinates of rectangular clip window :\nxmax,ymax:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides       :");
scanf("%d",&n);
printf("Enter the coordinates :");
for (i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
printf("hi");
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}

Wednesday, March 20, 2013

Cohen-Sutherland's Line clipping in C


//line clipping
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<dos.h>
#define MAX 20
void cohen_sutherlands(double,double,double,double,double,double,double,double);
enum{TOP=0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};
enum{FALSE,TRUE};
typedef unsigned int outcode;
outcode compute_outcode(int x,int y,int xmin,int ymin,int xmax,int ymax)
{
 outcode oc=0;
 if(y>ymax)
 oc|=TOP;
 else if(y<ymin)
 oc|=BOTTOM;
 if(x>xmax)
 oc|=RIGHT;
 else if(x<xmin)
 oc|=LEFT;
 return oc;
}

void cohen_sutherlands(double x1,double y1,double x2,double y2,double xmin,double ymin,double xmax,double ymax)
{
 int accept;
 int done;
 outcode outcode1,outcode2;
 accept=FALSE;
 done=FALSE;
 outcode1=compute_outcode(x1,y1,xmin,ymin,xmax,ymax);
 outcode2=compute_outcode(x2,y2,xmin,ymin,xmax,ymax);
 do
 {
  if(outcode1==0&&outcode2==0)
  {
   accept=TRUE;
   done=TRUE;
  }
  else if(outcode1&outcode2)
  {
   done=TRUE;
  }
  else
  {
   double x,y;
   int outcode_ex=outcode1?outcode1:outcode2;
   if(outcode_ex&TOP)
   {
    x=x1+(x2-x1)*(ymax-y1)/(y2-y1);
    y=ymax;
   }
   if(outcode_ex&BOTTOM)
   {
    x=x1+(x2-x1)*(ymin-y1)/(y2-y1);
    y=ymin;
   }
   else if(outcode_ex&RIGHT)
   {
    y=y1+(y2-y1)*(xmax-x1)/(x2-x1);
    x=xmax;
   }
   else if(outcode_ex&LEFT)
   {
    y=y1+(y2-y1)*(xmin-x1)/(x2-x1);
    x=xmin;
   }
   if(outcode_ex==outcode1)
   {
    x1=x;
    y1=y;
    outcode1=compute_outcode(x1,y1,xmin,ymin,xmax,ymax);
   }
   else
   {
    x2=x;
    y2=y;
    outcode2=compute_outcode(x2,y2,xmin,ymin,xmax,ymax);
   }
  }
 }
 while(done==FALSE);
 if(accept==TRUE)
 line(x1,y1,x2,y2);
}
//fms
void main()
{
 int n,i,j;
 int ln[MAX][4];
 int clip[4];
 int gd=DETECT,gm;
 printf("\nenter the no. of lines to be clipped");
 scanf("%d",&n);
 printf("\nEnter the x & y coordinates of line end points");
 for(i=0;i<n;i++)
 for(j=0;j<4;j++)
 scanf("%d",&ln[i][j]);
 printf("\nEnter the x & y coordinates of left top & right bottom");
 for(i=0;i<4;j++)
 scanf("%d",&clip[i]);
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 rectangle(clip[0],clip[1],clip[2],clip[3]);
 for(i=0;i<n;i++)
 line(ln[i][0],ln[i][1],ln[i][2],ln[i][3]);
 getch();
 cleardevice();
 rectangle(clip[0],clip[1],clip[2],clip[3]);
 for(i=0;i<n;i++)
 {
  cohen_sutherlands(ln[i][0],ln[i][1],ln[i][2],ln[i][3],clip[0],clip[1],clip[2],clip[3]);
  getch();
 }
 closegraph();
}

Saturday, March 2, 2013

Traffic System using GRAPHICS in C


//Traffic System
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void traf(int,int,int);
void car(int);

void main()
{
 int gd=DETECT,gm,i;
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 for(i=0;i<650;i++)
 {
  if(i!=150)
  {
   traf(0,0,2);
   setcolor(6);
   car(i);
   delay(15);
   cleardevice();
  }
  else
  {
   traf(4,0,0);
   setcolor(6);
   car(i);
   delay(3500);
   traf(0,0,2);
  }
 }
 getch();
}

void traf(int r,int y,int g)
{
 cleardevice();
 setcolor(6);
 rectangle(490,50,530,110);
 setfillstyle(1,r);
 fillellipse(510,60,10,10);
 setfillstyle(1,y);
 fillellipse(510,80,10,10);
 setfillstyle(1,g);
 fillellipse(510,100,10,10);
 setcolor(4);
 line(505,110,505,200);
 line(515,110,515,200);
 setcolor(15);
 line(0,200,getmaxx(),200);
 line(0,416,getmaxx(),416);
}

void car(int i)
{
 ellipse(125+i,320,0,180,50,25);
 line(41+i,320,75+i,320);
 line(175+i,320,208+i,320);
 arc(75+i,328,165,216,36);
 line(45+i,350,205+i,350);
 arc(175+i,328,320,373,36);
 circle(80+i,360,10);
 circle(160+i,360,10);
}

Monday, February 11, 2013

JavaScript program to display todays date


<!--fms-->
<html>
<head>
<script>
function displayDate()
{
document.getElementById("demo").innerHTML=Date();
}
</script>
</head>
<body>

<h1>JavaScript to display date</h1>
<p id="demo">This is a paragraph.</p>

<button type="button" onclick="displayDate()">Display Date</button>

</body>
</html>

jsp to display todays day of the week


<!--fms-->
<html>
<body>

<p id="demo">Click the button to display todays day of the week.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var d = new Date();
var weekday=new Array(7);
weekday[0]="Today is Sunday";
weekday[1]="Today is Monday";
weekday[2]="Today is Tuesday";
weekday[3]="Today is Wednesday";
weekday[4]="Today is Thursday";
weekday[5]="Today is Friday";
weekday[6]="Today is Saturday";

var x = document.getElementById("demo");
x.innerHTML=weekday[d.getDay()];
}
</script>

</body>
</html>

Sunday, February 10, 2013

jsp program to display clock


<!jsp program to display clock!>
<!DOCTYPE html>
<html>
<head>
<script>
function startTime()
{
  var today=new Date();
  var h=today.getHours();
  var m=today.getMinutes();
  var s=today.getSeconds();
  // add a zero in front of numbers<10
  m=checkTime(m);
  s=checkTime(s);
  document.getElementById('txt').innerHTML=h+":"+m+":"+s;
  t=setTimeout(function(){startTime()},500);
}

function checkTime(i)
{
   if (i<10)
   {
      i="0" + i;
   }
   return i;
}
</script>
</head>

<body onload="startTime()">
<div id="txt"></div>
</body>
</html>