source code, coding, asp.net, C#, php, ruby, sql, oracle,android,node.js,javascript,linux,unix, random stuffs etc.
Saturday, August 25, 2007
program for shearing of object in C
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
float matrix[3][3];
float Xref=200,Yref=200;
void draw_poly(float array[][2],int n)
{
int i;
for(i=0;i<n-1;i++)
line(array[i][0],array[i][1],array[i+1][0],array[i+1][1]);
line(array[i][0],array[i][1],array[0][0],array[0][1]);
}
void transpoint(float array[][2],int n)
{
float p[10][2],x,y;
int i;
for(i=0;i<n;i++)
{
x=matrix[0][0]*array[i][0]+matrix[0][1]*array[i][1]+matrix[0][2];
y=matrix[1][0]*array[i][0]+matrix[1][1]*array[i][1]+matrix[1][2];
p[i][0]=x;
p[i][1]=y;
}
draw_poly(p,n);
}
void translate(float shx,float shy)
{
matrix[0][0]=1;
matrix[0][1]=shx;
matrix[0][2]=-shx*Yref;
matrix[1][0]=shy;
matrix[1][1]=1;
matrix[1][2]=-shy*Xref;
matrix[2][0]=0;
matrix[2][1]=0;
matrix[2][2]=1;
}
void main()
{
int gd = DETECT, gm,n=4,i;
float pts[4][2]={{100.0,200.0},{200.0,200.0},{200.0,50.0},{100.0,50.0}},tx,ty;
initgraph(&gd, &gm, "c:\\tc\\bgi\\");
draw_poly(pts,n);
printf("\n\nEnter the 'shx' shy:");
scanf("%f%f",&tx,&ty);
translate(tx,ty);
transpoint(pts,n);
getch();
}
program for transtlation polygon using C
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
float matrix[3][3];
void draw_poly(float array[][2],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(array[i][0],array[i][1],array[i+1][0],array[i+1][1]);
}
line(array[i][0],array[i][1],array[0][0],array[0][1]);
}
void transpoint(float array[][2],int n)
{
float p[3][2],x,y;
int i;
for(i=0;i<n;i++)
{
x=matrix[0][0]*array[i][0] + matrix[0][1]*array[i][1] + matrix[0][2];
y=matrix[1][0]*array[i][0] + matrix[1][1]*array[i][1] + matrix[1][2];
p[i][0]=x;
p[i][1]=y;
}
draw_poly(p,n);
}
void translate(float tx,float ty)
{
matrix[0][0]=1;
matrix[0][1]=0;
matrix[0][2]=tx;
matrix[1][0]=0;
matrix[1][1]=1;
matrix[1][2]=ty;
matrix[2][0]=0; //
matrix[2][1]=0; //not used in traslation
matrix[2][2]=1; //
}
void main()
{
int gd = DETECT, gm,n,i,j;
float pts[10][2];//={{320,50},{160,150},{480,150}}
float tx,ty;
initgraph(&gd, &gm, "c:\\tc\\bgi");
printf("Enter no of edges");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter X Of edge %d ",i+1);
scanf("%f",&pts[i][0]);
printf("Enter Y of edge %d ",i+1);
scanf("%f",&pts[i][1]);
}
draw_poly(pts,n);
printf("Enter the 'tx' and 'ty' :");
scanf("%f %f",&tx,&ty);
translate(tx,ty);
transpoint(pts,n);
getch();
cleardevice();
}
BOUNDARY-FILL algorithm in C
BOUNDARY-FILL algorithm in C
/*Graphics algorithm to impliment the BOUNDARY-FILL-4 Algorithm */
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void boundfill4(int x,int y,int fill,int boundary);
void main()
{
int arr[10][2],n,x,y;
printf("Enter the number of edges : ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the X and Y position for %d edge : ",i+1);
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
printf("Enter X and Y co-ordinates of the point from where you want \n");
printf("to start BOUNDARY-FILL : ");
scanf("%d %d",&x,&y);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawpolygon(n,arr,RED);
getch();
boundfill4(x,y,BLUE,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
void boundfill4(int x,int y,int fill,int boundary) //boundary=RED
{ //fill=BLUE
int current;
current = getpixel(x,y);
if( (current != boundary) && (current != fill) )
{
delay(2);
putpixel(x,y,fill);
boundfill4(x+1,y,fill,boundary);
boundfill4(x-1,y,fill,boundary);
boundfill4(x,y+1,fill,boundary);
boundfill4(x,y-1,fill,boundary);
}
}
/*Graphics algorithm to impliment the BOUNDARY-FILL-8 Algorithm */
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void boundfill8(int x,int y,int fill,int boundary);
void main()
{
int r,x1,y1,x2,y2;
printf("Enter Left and Top co-ordinates to draw a SQUARE : ");
scanf("%d %d",&x1,&y1);
printf("Enter Right and Bottom co-ordinates to draw a SQUARE : ");
scanf("%d %d",&x2,&y2);
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(x1,y1,x2,y2);
getch();
printf("Enter X and Y co-ordinates to Fill a circle using BOUNDRY-FILL : ");
scanf("%d %d",&x1,&y1);
boundfill8(x1,y1,BLUE,RED);
getch();
closegraph();
restorecrtmode();
}
void boundfill8(int x,int y,int fill,int boundary)
{
int current;
current = getpixel(x,y);
if( (current != boundary) && (current != fill) )
{
putpixel(x,y,fill);
delay(2);
boundfill8(x-1,y-1,fill,boundary);
boundfill8(x,y-1,fill,boundary);
boundfill8(x+1,y-1,fill,boundary);
boundfill8(x-1,y,fill,boundary);
boundfill8(x+1,y,fill,boundary);
boundfill8(x-1,y+1,fill,boundary);
boundfill8(x,y+1,fill,boundary);
boundfill8(x+1,y+1,fill,boundary);
}
}
PROGRAM FOR DRAWING A CIRCLE USING MIDPOINT ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int xc,yc,dx,dy,i,k,r,p;
float xinc,yinc,x,y;
void plotpoint(int,int,int,int);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
cleardevice();
cout<<"ENTER THE VALUE OF COORDINATES OF THE CENTER OF THE CIRCLE\n";
cout<<"ENTER THE X COORDINATE"<<endl;
cin>>xc;
cout<<"ENTER THE Y COORDINATE"<<endl;
cin>>yc;
cout<<"ENTER THE RADIUS\n";
cin>>r;
cout<<"CIRCLE WITH GIVEN CENTER AND RADIUS IS "<<endl;
x=0;
y=r;
plotpoint(xc,yc,x,y);
p=1-r;
while(x<y)
{
if(p<0)
x=x+1;
else
{
x=x+1;
y=y-1;
}
if(p<0)
p=p+2*x+1;
else
p=p+2*(x-y)+1;
plotpoint(xc,yc,x,y);
}
getch();
}
void plotpoint(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,5);
putpixel(xc-x,yc+y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+y,yc+x,1);
putpixel(xc-y,yc+x,1);
putpixel(xc+y,yc-x,1);
putpixel(xc-y,yc-x,1);
}
Circle, line, Polygon - C++ program
============================
Circle
=========
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int xcent=100,ycent=100,radius=100;
void plotpixel(int,int);
void main()
{
int gd=0,gm=0;
initgraph(&gd,&gm,"c:\\tc\\bgi");
int x=0,y=radius,p=0;
plotpixel(x,y);
p=1-radius;
while(x<y)
{
if(p<0)
{
x = x+1;
p = (p+2) * (x+1);
}
else
{
x = x+1;
y=y-1;
p= (p+2) * (x-y) +1;
}
plotpixel(x, y);
}
getch();
}
void plotpixel(int x, int y)
{
putpixel(xcent+x, ycent+y, 15);
putpixel(xcent-x, ycent+y, 15);
putpixel(xcent+x, ycent-y, 15);
putpixel(xcent-x, ycent-y, 15);
putpixel(xcent+y, ycent+x, 15);
putpixel(xcent-y, ycent+x, 15);
putpixel(xcent+y, ycent-x, 15);
putpixel(xcent-y, ycent-y, 15);
}
Line
======
#include <graphics.h>
//#include <stdlib.h>
//#include <stdio.h>
#include <conio.h>
void line1(int,int,int,int);
int main(void)
{
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
setcolor(getmaxcolor());
line1(5, 20, 145, 20);
getch();
closegraph();
return 0;
}
void line1(int x1,int y1,int x2,int y2)
{
int dx=x2-x1;
if(dx<0)
dx=-dx;
int dy=y2-y1;
if(dy<0)
dy=-dy;
int i,j;
for( i=0, j=0; i<=dx j<=dy; i++,j++)
{
putpixel(x1+i,y1+j,15);
}
}
Polygon
========
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void drawpolygon(int n,int arr[][2],int color);
void main()
{
int arr[10][2],n;
printf("Enter the number of edges : ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter the X and Y position for %d edge : ",i+1);
scanf("%d %d",&arr[i][0],&arr[i][1]);
}
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
drawpolygon(n,arr,RED);
getch();
closegraph();
restorecrtmode();
}
void drawpolygon(int n,int arr[][2],int color)
{
setcolor(color);
for(int i=0;i<n;i++)
{
if( i == n-1 )
line(arr[i][0],arr[i][1],arr[0][0],arr[0][1]);
else
line(arr[i][0],arr[i][1],arr[i+1][0],arr[i+1][1]);
}
}
PROGRAM FOR DRAWING A DOTTED LINE AND DASHED LINE USING BRESENHAM'S ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
#include<dos.h>
void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;
cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);
m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;
}
else
{
x=x2;
y=y2;
}
putpixel(x,y,1);
while(x < xend)
{
x=x+1;
if((cnt/2)%3 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();
}
/*PROGRAM FOR DRAWING A DASHED LINE USING BRESENHAM'S ALGORITHM*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
#include<dos.h>
void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;
cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
// putpixel(x1,y1,1);
// putpixel(x2,y2,1);
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);
m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;
}
else
{
x=x2;
y=y2;
}
putpixel(x,y,1);
while(x < xend)
{
x=x+1;
if((cnt/5)%2 ==0)
putpixel(x,y,2);
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
//putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();
}
DDA LINE , BRESENHAM'S ALGORITHM
/*PROGRAM FOR DRAWING A LINE USING DDA ALGORITHM*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
void main()
{
int x1,x2,y1,y2,dx,dy,i,k;
float xinc,yinc,x,y;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
putpixel(x1,y1,1);
putpixel(x2,y2,1);
dx=x2-x1;
dy=y2-y1;
if(abs(dx) > abs(dy))
k=abs(dx);
else
k=abs(dy);
xinc=1.0*dx/k;
yinc=1.0*dy/k;
x=x2*1.0;
y=y2*1.0;
putpixel(round(x),round(y),1);
cleardevice();
cout<<"LINE GENERATED FROM THE GIVEN POINTS IS : ";
setbkcolor(6);
for(i=1;i<=k;i++)
{
x+=xinc;
y+=yinc;
putpixel(round(x),round(y),i);
}
getch();
}
int round(float p)
{
if (p-int(p)<0.5)
return floor(p);
else
return ceil(p);
}
/*PROGRAM FOR DRAWING A LINE USING BRESENHAM'S ALGORITHM*/
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<process.h>
#include<dos.h>
void main()
{
int x1,x2,y1,y2,dx,dy,i,k,p,xend,yend,cnt=0;
float xinc,yinc,x,y,m;
int round(float);
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
setbkcolor(5);
//cleardevice();
cout<<"ENTER THE VALUE OF X1: "<<endl;
cin>>x1;
cout<<"ENTER THE VALUE OF y1: "<<endl;
cin>>y1;
cout<<"ENTER THE VALUE OF X2: "<<endl;
cin>>x2;
cout<<"ENTER THE VALUE OF y2: "<<endl;
cin>>y2;
cleardevice();
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
if (abs(dx) > abs(dy))
xend=abs(dx);
else
xend=abs(dy);
m=dy/(float)dx;
if(x2 > x1)
{
x=x1;
y=y1;
}
else
{
x=x2;
y=y2;
}
putpixel(x,y,1);
while(x < xend)
{
x=x+1;
if (p<0)
{
p=p+2*dy;
}
else
{
if(m>=0 && m<=1)
y=y+1;
else
{
if(dx==0)
x--;
y--;
}
p=p+2*(dy-dx);
}
putpixel(x,y,1);
delay(10);
cnt++;
}
getch();
closegraph();
}