Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
 
        // -- 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 20-Feb-13 9:02am
Yvar Birx2.4K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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...
  Permalink  
v2
Comments
Andreas Gieriet at 20-Feb-13 15:46pm
   
My 5!
See also my solution #2.
Cheers
Andi
Sergey Alexandrovich Kryukov at 20-Feb-13 19:16pm
   
Sure, a 5. (Hard to explain anything to anyone with such "background"; you've done some decent attempt... :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
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:
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
  Permalink  
v2
Comments
Yvar Birx at 20-Feb-13 15:32pm
   
Cheers!
Sergey Alexandrovich Kryukov at 20-Feb-13 19:15pm
   
Agree, a 5.
—SA
Andreas Gieriet at 20-Feb-13 19:16pm
   
Thanks for your 5, Sergey!
Andi
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think what you are looking for is the Math.Abs Method[^] to always get the positive number.
  Permalink  
Comments
Yvar Birx at 20-Feb-13 15:33pm
   
Cheers too!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
Comments
Andreas Gieriet at 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 at 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)

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 170
2 CHill60 160
3 RyanDev 135
4 Richard MacCutchan 115


Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid