Click here to Skip to main content
15,888,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My aim to get direction based on two cordinates x1 and x2

here

center=x1;
pointOncircle=x2;


C#
if (center.X < pointOnCircle.X)
             {
                 UpdateText("right");

             }
             else if (center.X >pointOnCircle.X)
             {

                 UpdateText("left");

             }
             else if (center.Y < pointOnCircle.Y)
             {
                 UpdateText("down");

             }
             else {

                 UpdateText("up");


             }
Posted

1 solution

Don't do that: work out the angle of a line between the two points instead.
C#
double angle(Point p1, Point p2)
    {
    double xDiff = p2.X - p1.X;
    double yDiff = p2.Y - p1.Y;
    return Math.Atan2(yDiff, xDiff) * (180 / Math.PI);
    }
Then all you have to do is look at the value:
between 45 and -45 is Right, and so forth.
 
Share this answer
 
Comments
vishnulalr 23-Jun-13 10:05am    
thanks for you reply it help me a lot but how can i get for up down ? i have got another snipet is that good for me it almost works


float xCentroidsDifference = center.X - pointOnCircle.X;
float yCentroidsDifference = center.Y - pointOnCircle.Y;

float threshold = 50;


UpdateText5("center");
if (Math.Abs(xCentroidsDifference) > Math.Abs(yCentroidsDifference))
{
if (xCentroidsDifference > threshold)
UpdateText5( "right");
if (xCentroidsDifference < -threshold)
UpdateText5("left");
}
if (Math.Abs(xCentroidsDifference) < Math.Abs(yCentroidsDifference))
{
if (yCentroidsDifference > threshold)
UpdateText5("up");
if (yCentroidsDifference < -threshold)
UpdateText5("down");
}
OriginalGriff 23-Jun-13 10:13am    
How do you think it works for up and down?
If Right is 45 to -45, then up is 46 to 45 + 90, and so forth...
Try looking at the numbers you get for (0, 0) and (0, 100) for example - that's directly up on most graphs...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900