Saturday, May 10, 2008

Comuter graphics 2D transformation


#include " stdio.h "
#include " conio.h "
#include " math.h "
#include " graphics.h "
#include " stdlib.h "
#include " ctype.h "

void intgraph();
void display_cordinate(char bg_color,char line_color);
int Create_poly(int poly[][2]);
void fill_poly(int poly[][2],int points,char line_color,char fill_color);
void Identity(float Matrix[3][3]);
void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis);

int instance=0;
int current=-1;


void main()
{
int poly[10][10][2],i,points[10],Xdis[10],Ydis[10];
float Sx,Sy,Matrix[10][3][3],temp,Cos,Sin;
char ch;
intgraph();

setfillstyle(8,RED);
points[current]=Create_poly(poly[current+1]);
display_cordinate(DARKGRAY,WHITE);
fill_poly(poly[current],points[current],BLUE,YELLOW);

Identity(Matrix[current]);
getch();
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];

Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);

Menu:
restorecrtmode();
do
{
clrscr();
instance=0;
printf("\t\t0. Instance transformation\n");
printf("\t\t1. Scaling\n");
printf("\t\t2. Translation\n");
printf("\t\t3. Rotation\n");
printf("\t\t4. Reflection [ X axis ]\n");
printf("\t\t5. Reflection [ Y axis ]\n");
printf("\t\t6. Reflection [ X=Y axis ]\n");
printf("\t\t7. Draw \n");
printf("\t\t8. New input set\n");
printf("\t\t9. EXIT \n");


printf("\n\nChoice: ");
ch=getche();
}while((ch < '0')||(ch > '9'));

switch(ch)
{
case '0':
//printf("Available instances are:");
instance=1;
goto babu;
case '1':
printf("\n\tScaling Ratio: ");
printf("\n\tSx: ");
scanf("%f",&Sx);
printf("\n\tSy: ");
scanf("%f",&Sy);
for(i=0;i<3;i++)
{
Matrix[current][i][0]=Matrix[current][i][0]*Sx;
Matrix[current][i][1]=Matrix[current][i][1]*Sy;
}
goto Menu;

case '2':
printf("\n\tTranslation : ");
printf("\n\tTx: ");
scanf("%f",&Sx);
printf("\n\tTy: ");
scanf("%f",&Sy);

Matrix[current][2][0]=Matrix[current][2][0]+Sx;
Matrix[current][2][1]=Matrix[current][2][1]+Sy;

goto Menu;

case '3':
printf("\nRotation Angle [in Degree]: ");
scanf("%f",&Sx);

Sx=(Sx*3.14159265358)/180.0;
Cos=cos(Sx);
Sin=sin(Sx);
for(i=0;i<=2;i++)
{
temp=Matrix[current][i][0]*Cos-Matrix[current][i][1]*Sin;
Matrix[current][i][1]=Matrix[current][i][0]*Sin+Matrix[current][i][1]*Cos;
Matrix[current][i][0]=temp;
}
goto Menu;

case '4':
Matrix[current][1][1]=Matrix[current][1][1]*-1;
printf("\t\tCompleted");
getch();

goto Menu;

case '5':
Matrix[current][0][0]=Matrix[current][0][0]*-1;
printf("\t\tCompleted");
getch();

goto Menu;

case '6':
Matrix[current][0][1]=1;
Matrix[current][1][1]=1;
printf("\t\tCompleted");
getch();
goto Menu;

case '7':
if(!instance)
{int j =current;
for(i=0;i<=points[j];i++)
{
temp=poly[j][i][0]*Matrix[j][0][0]+poly[j][i][1]*Matrix[j][1][0]+Matrix[j][2][0];
poly[j][i][1]=poly[j][i][0]*Matrix[j][0][1]+poly[j][i][1]*Matrix[j][1][1]+Matrix[j][2][1];
poly[j][i][0]=temp;
}

}

goto babu;

case '8':
points[current]=Create_poly(poly[current+1]);
Identity(Matrix[current]);
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];
Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);
goto Menu;

case '9':
getch();
exit(0);

}
babu:
setgraphmode(2);
display_cordinate(DARKGRAY,WHITE);

if(instance)
{
for(int u=0;u<=current;u++)
{
Translate_to_Relative(poly[u],points[u],-Xdis[u],-Ydis[u]);
fill_poly(poly[u],points[u],BLUE,YELLOW);
//getch();
//printf("1");
}
instance=0;
}
else
{ Translate_to_Relative(poly[current],points[current],-Xdis[current],-Ydis[current]);
fill_poly(poly[current],points[current],BLUE,YELLOW);
}
getch();
restorecrtmode();
do
{
clrscr();
printf("\n\tDo you Want to Switch to MENU [Y|N]:- ");
ch=toupper(getche());
}while( (ch!='Y') && (ch!='N') );
if(ch=='Y')
{
//Identity(Matrix[current]);
Xdis[current]=0-poly[current][0][0];
Ydis[current]=0-poly[current][0][1];

Translate_to_Relative(poly[current],points[current],Xdis[current],Ydis[current]);
goto Menu;
}
closegraph();
}

void intgraph()
{
int g=DETECT,d;
initgraph(&g,&d,"e:\\tc\\bgi");
}

void fill_poly(int poly1[][2],int points,char line_color,char fill_color)
{
int pol[20],i;
char str[2];
int fac=5;
for(i=0;i<=points;i++)
{
pol[i*2]=320+fac*poly1[i][0];
pol[i*2+1]=240-fac*poly1[i][1];
}
pol[i*2]=320+fac*poly1[0][0];
pol[i*2+1]=240-fac*poly1[0][1];

setcolor(line_color);
if(points>2)
setfillstyle(1,fill_color);
fillpoly(points,pol);


}

void Identity(float Matrix[3][3])
{
int i,j;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
if(i==j)
Matrix[i][j]=1;
else
Matrix[i][j]=0;
}
}
}
int Create_poly(int poly[][2])
{
//printf("%d %d\n\n",getmaxx(),getmaxy());//640 480
current++;
int total_point;
printf("No of vertices: ");
scanf("%d",&total_point);

for(int i=0;i < total_point;i++)
{
printf("Co-ordinate [%d]: ",i+1);
scanf("%d %d",&poly[i][0],&poly[i][1]);

}

return total_point;
}

void display_cordinate(char bg_color,char line_color)
{
int i;
setbkcolor(bg_color);
setcolor(line_color);
for(i=0;i<=640;i+=5)
{
line(i,0,i,480);
}

for(i=0;i<=480;i+=5)
{
line(0,i,640,i);
}

rectangle(0,0,639,479);

setcolor(RED);
line(0,240,640,240);
line(320,0 ,320,480);
}

void Translate_to_Relative(int poly[][2],int points,int Xdis,int Ydis)
{
int i;
for(i=0;i<=points;i++)
{
poly[i][0]=poly[i][0]+Xdis;
poly[i][1]=poly[i][1]-Ydis;
}
}

Thursday, April 10, 2008

JS code to disable right click and refresh button

document.onmousedown="if (event.button==2) return false";
document.oncontextmenu=new Function("return false");//disable right click

document.onkeydown = showDown;

function showDown(evt)
{
evt = (evt)? evt : ((event)? event : null);
if (evt) {
if (event.keycode == 8 && (event.srcElement.type!= "text" && event.srcElement.type!= "textarea" && event.srcElement.type!= "password")) {
// When backspace is pressed but not in form element
cancelKey(evt);
}
else if (event.keycode == 116) {
// When F5 is pressed
alert("asdfasfdasd");
cancelKey(evt);
}
else if (event.keycode == 122) {
// When F11 is pressed
cancelKey(evt);
}
else if (event.ctrlKey && (event.keycode == 78 ¦¦ event.keycode == 82)) {
// When ctrl is pressed with R or N
cancelKey(evt);
}
else if (event.altKey && event.keycode==37 ) {
// stop Alt left cursor
return false;
}
}
}

function cancelKey(evt) {
if (evt.preventDefault) {
evt.preventDefault();
return false;
}
else {
evt.keycode = 0;
evt.returnValue = false;
}
}

Javascript code to assaign comma into number/currency

This js fuction can convert currency to commified number. To assaign comma in between number this function is very helpful.



function commify(str)
{
var Num = str
var newNum = "";
var newNum2 = "";
var count = 0;

//check for decimal number
if (Num.indexOf('.') != -1){ //number ends with a decimal point
if (Num.indexOf('.') == Num.length-1){
Num += "00";
}
if (Num.indexOf('.') == Num.length-2){ //number ends with a single digit
Num += "0";
}

var a = Num.split(".");
Num = a[0]; //the part we will commify
var end = a[1] //the decimal place we will ignore and add back later
}
else {var end = "00";}

//this loop actually adds the commas
for (var k = Num.length-1; k >= 0; k--){
var oneChar = Num.charAt(k);
if (count == 3){
newNum += ",";
newNum += oneChar;
count = 1;
continue;
}
else {
newNum += oneChar;
count ++;
}
} //but now the string is reversed!

//re-reverse the string
for (var k = newNum.length-1; k >= 0; k--){
var oneChar = newNum.charAt(k);
newNum2 += oneChar;
}

// add dollar sign and decimal ending from above
newNum2 = newNum2 + "." + end;
return newNum2;
}

Javascript code for Converting english number to word

This function can convert english number to word like 59 to Fifty Nine. Also can handle fractional part. If one can use only zeros after point he must need to give only two zero like 2.00.



var th = ['','Thousand','Million', 'Billion','Trillion'];
var dg = ['Zero','One','Two','Three','Four', 'Five','Six','Seven','Eight','Nine'];
var tn = ['Ten','Eleven','Twelve','Thirteen', 'Fourteen','Fifteen','Sixteen', 'Seventeen','Eighteen','Nineteen'];
var tw = ['Twenty','Thirty','Forty','Fifty', 'Sixty','Seventy','Eighty','Ninety'];
function toWords(s)
{s = s.replace(/[\, ]/g,'');
var len=s.length;
var babu="";
if(len>2 && s.charAt(len-1)=='0'&& s.charAt(len-2)=='0' && s.charAt(len-3)=='.')
{
//alert(s);
for(var i=0;i 15)
return 'too big';
var n = s.split('');
var str = '';
var sk = 0;
for (var i=0; i < x; i++)
{if ((x-i)%3==2)
{if (n[i] == '1')
{str += tn[Number(n[i+1])] + ' '; i++; sk=1;
}
else if (n[i]!=0) {str += tw[n[i]-2] + ' ';sk=1;}}
else if (n[i]!=0) {str += dg[n[i]] +' ';
if ((x-i)%3==0) str += 'Hundred ';sk=1;}
if ((x-i)%3==1) {if (sk) str += th[(x-i-1)/3] + ' ';sk=0;}}
if (x != s.length)
{var y = s.length; str += 'point ';
for (var i=x+1; i< y; i++)
str += dg[n[i]] +' ';
}
return str.replace(/\s+/g,' ');
}

Javascript code for table height

document.getElementById("your_table_id").offsetHeight

PC Magazine Tips and Solutions

PC World: Latest Technology News

PCWorld.com - Most Popular Downloads of the Week