Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Here is my code, I can't really say more to it. I mean, it works fine, but is it really the most effiecient way? It looks so dirt. D:

C#
// -- Distance.
/// <summary>
/// Returns the Pixel distance between two points.
/// </summary>
/// <param name="Entity"></param>
/// <returns></returns>
public int GetDistanceFromEntity(Entity Entity)
{
    /* Return distance. */
    int OurEntityX = this.PositionX.ToString().StartsWith("-") ? -this.PositionX : this.PositionX;
    int OurEntityY = this.PositionY.ToString().StartsWith("-") ? -this.PositionY : this.PositionY;

    int OtherEntityX = Entity.PositionX.ToString().StartsWith("-") ? -Entity.PositionX : Entity.PositionX;
    int OtherEntityY = Entity.PositionY.ToString().StartsWith("-") ? -Entity.PositionY : Entity.PositionY;

    return (int)Math.Sqrt(Math.Pow(OtherEntityX - OurEntityX, 2) + Math.Pow(OtherEntityY - OurEntityY, 2));
}
Posted

This code cannot possibly return the correct distance between two arbitrary points.
If this.PositionX == -2 and Entity.PositionX == +2, the difference between them is 4, but because you are effectively taking the absolute value of each before the subtraction, the difference you get will be 0 (zero!)
For this calculation, just use the .PositionX and .PositionY values AS IS.
The math will all work out!

See the other solutions for answers to the original question...
 
Share this answer
 
v2
Comments
Andreas Gieriet 20-Feb-13 15:46pm    
My 5!
See also my solution #2.
Cheers
Andi
Sergey Alexandrovich Kryukov 20-Feb-13 19:16pm    
Sure, a 5. (Hard to explain anything to anyone with such "background"; you've done some decent attempt... :-)
—SA
I assume your values are int.
There is a function called Math.Abs(...).

[EDIT]
Your function is broken. Coordinates may be in general positive as well as negative (a point may lay in any of the four quadrants[^]). If you take the absolute value of the coordinates before you calculate the coodinate distance results in wrong results. Take the values as they are.
C#
public int GetDistanceFromEntity(Entity Entity)
{
   int deltaX = Entity.PositionX-PositionX;
   int deltaY = Entity.PositionY-PositionY;
   return (int)Math.Sqrt(deltaX*deltaX + deltaY*deltaY);
}
You can test it with points in all quadrants and in both directions:
C#
var q1=new Entity(1, 2);
var q2=new Entity(-3, 2);
var q3=new Entity(-3, -1);
var q4=new Entity(4, -1);
var p0=new Entity(0, 0);

Assert.AreEqual(0, q1.GetDistanceFromEntity(q1), "identiy should have distance 0");
Assert.AreEqual(0, q2.GetDistanceFromEntity(q2), "identiy should have distance 0");
Assert.AreEqual(0, q3.GetDistanceFromEntity(q3), "identiy should have distance 0");
Assert.AreEqual(0, q4.GetDistanceFromEntity(q4), "identiy should have distance 0");
Assert.AreEqual(0, p0.GetDistanceFromEntity(p0), "identiy should have distance 0");

Assert.AreEqual(q2.GetDistanceFromEntity(q1), q1.GetDistanceFromEntity(q2), "distance should be invariant");
Assert.AreEqual(q3.GetDistanceFromEntity(q1), q1.GetDistanceFromEntity(q3), "distance should be invariant");
Assert.AreEqual(q4.GetDistanceFromEntity(q1), q1.GetDistanceFromEntity(q4), "distance should be invariant");
Assert.AreEqual(p0.GetDistanceFromEntity(q1), q1.GetDistanceFromEntity(p0), "distance should be invariant");

Assert.AreEqual(4, q1.GetDistanceFromEntity(q2), "distance q1-q2 mismatch");
Assert.AreEqual(5, q1.GetDistanceFromEntity(q3), "distance q1-q3 mismatch");
...

[/EDIT]

Cheers
Andi
 
Share this answer
 
v2
Comments
Yvar Birx 20-Feb-13 15:32pm    
Cheers!
Sergey Alexandrovich Kryukov 20-Feb-13 19:15pm    
Agree, a 5.
—SA
Andreas Gieriet 20-Feb-13 19:16pm    
Thanks for your 5, Sergey!
Andi
I think what you are looking for is the Math.Abs Method[^] to always get the positive number.
 
Share this answer
 
Comments
Yvar Birx 20-Feb-13 15:33pm    
Cheers too!
No!!! Stop it immediately! My eyes are hurt. I probably never saw anything worse. Never ever do anything similar. This is the activity opposite to programming, a total abuse.

This is such a weird destructive trend to work with strings representing data instead of data itself. I cannot even explain how wrong it is; it should not happen from the very first steps.

Second thing is: in most cases, you don't even need to tell negatives from positives; arithmetic expressions should correctly work with both. But if you really have to, you should write if (value < 0) {/* ... */}

—SA
 
Share this answer
 
Comments
Andreas Gieriet 20-Feb-13 15:47pm    
My 5!
Agree, the code is very odd.
But the real problem is calculating the distance, and for that, he does not need to know the sign of the values at all.
See my solution #2.
Cheers
Andi
Sergey Alexandrovich Kryukov 20-Feb-13 19:16pm    
Thank you, Andi.
—SA

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