/*This code snippet finds the distance between a P( x,y)
and a line segement that connects 2 points P1(x1,y1) and P2(x2,y2) */
int main()
{
float x1,y1,x2,y2;
float x,y;
char ch;
do
{
clrscr();
cout<<"\n\nEnter x position of the Point:";
cin>>x;
cout<<"\nEnter y position of the Point:";
cin>>y;
cout<<"\n\nEnter x position of the frist Point:";
cin>>x1;
cout<<"\nEnter y position of the frist Point:";
cin>>y1;
cout<<"\n\nEnter x position of the second Point:";
cin>>x2;
cout<<"\nEnter y position of the second Point:";
cin>>y2;
float linedist,dist1,dist2;
linedist = sqrt( (( x1-x2 )*( x1-x2 ))+(( y1-y2 )*( y1-y2 )) );
dist1 = sqrt( (( x1-x )*( x1-x ))+(( y1-y )*( y1-y )) );
dist2 = sqrt( (( x-x2 )*( x-x2 ))+(( y-y2 )*( y-y2 )) );
float height = linedist*(dist1)/(dist1+dist2);
float distance = sqrt( (dist1*dist1) - (height*height) );
cout<<"\n\nDistance minimum of point from line is::" << distance << "\n
\ncontinue[Y/N] ??";
cin>>ch;
}
while(ch!='n' && ch!='N');
return 0;
}
Wednesday, September 30, 2009
Sample Code to find distance between a point & a line segment that connects two points in C++
Program that will return the nth node from the end of a linked list
struct node
{
int data;
struct node *next;
}mynode;
mynode * nthNode(mynode *head, int n /*pass 0 for last node*/)
{
mynode *ptr1,*ptr2;
int count;
if(!head)
{
return(NULL);
}
ptr1 = head;
ptr2 = head;
count = 0;
while(count < ptr1="ptr1-">next)==NULL)
{
//Length of the linked list less than n. Error.
return(NULL);
}
}
while((ptr1=ptr1->next)!=NULL)
{
ptr2=ptr2->next;
}
return(ptr2);
}
Given a point with co-ordinates x, y write a function to check if it lies in a box whose center co-ordinates, width and height are known.
class BOX
{
int center_x;
int center_y;
int width;
int height;
public BOX()
{
//initialize center width,height
}
boolean IsOnBox(int x,int y);
{
return ( (x >= center_x - width/2) && (x <= center_x + width/2) && (y>=center_y- height/2) && (y <= center_x-height/2) )
}
}
Reverse an unsigned integer(C)(Kindly check if the code is working properly)
uint Reverse(uint x)
{
uint y = 0;
int i=0;
for (i=0;i<32;i++) y =" (x">>= 1;
}
return y;
}
Game Program
#include
#include
#include
class Nasa
{
int x;
int y;
char dir;
void moveNorth()
{
this->y++;
}
void moveSouth()
{
this->y--;
}
void moveEast()
{
this->x++;
}
void moveWest()
{
this->x--;
}
public:
int run()
{
char ch='n';
do
{
clrscr();
char *mov;
cout<<"\n\nEnter coordinates :\n"; cout<<"X:"; cin>>this->x;
cout<<"Y:"; cin>>this->y;
cout<<"direction[N/S/E/W]:"; cin>>this->dir;
if(dir<='z' && dir>='a')
dir-=32;
cout<<"\n\nEnter movement string:\n"; cin>>mov;
strupr(mov);
for(int i=0;mov[i]!=NULL;i++)
{
switch(mov[i])
{
case 'L':
switch(dir)
{
case 'N':
dir='W';
break;
case 'S':
dir='E';
break;
case 'E':
dir='N';
break;
case 'W':
dir='S';
break;
}
break;
case 'R':
switch(dir)
{
case 'N':
dir='E';
break;
case 'S':
dir='W';
break;
case 'E':
dir='S';
break;
case 'W':
dir='N';
break;
}
break;
case 'M':
switch(this->dir)
{
case 'N':
moveNorth();
break;
case 'S':
moveSouth();
break;
case 'E':
moveEast();
break;
case 'W':
moveWest();
break;
}
}
}
cout<<"\n\n"<<<" "< <<" "< <<"\n\n continue [Y/N] :"; cin>>ch;
}
while(ch!='n' && ch!='N');
return 0;
}
};
int main()
{
Nasa n;
n.run();
}
Sunday, September 27, 2009
Purpose...
The purpose of this website to get feedback from industry experienced developers on Amit's code, if you want to provide constructive feedback, post back your comments, so he can learn from his mistakes.
On how to post on the blog, read this post http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html
On how to post on the blog, read this post http://mlawire.blogspot.com/2009/07/blogger-syntax-highlighting.html
Subscribe to:
Comments (Atom)