这是检测负数的最佳方法吗? [英] Is this the best way to detect negative numbers?

查看:95
本文介绍了这是检测负数的最佳方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,我真的不能多说了。我的意思是,它工作正常,但它真的是最有效的方式吗?它看起来很脏。 D:



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));
}

推荐答案

此代码无法返回两个任意点之间的正确距离。

如果 this.PositionX == -2 Entity.PositionX == +2 ,之间的区别它们是4,但因为你 有效地 在减法之前取每个的绝对值,你得到的差异将是0 (零!)

对于此计算,只需使用 .PositionX .PositionY 按原样

数学全部解决!



请参阅其他解决方案以获得原始问题的答案...
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...


我假设您的值为 int

有一个名为的函数Math.Abs​​(...)





你的功能坏了。坐标可能一般是积极的也可以是消极的(这一点可能存在于四个象限中的任何一个[< a href =http://en.wikipedia.org/wiki/Quadrant_(plane_geometry)target =_ blanktitle =New Window> ^ ])。如果您在之前取坐标的绝对值,则计算坐标距离会导致错误的结果。按原样取值。

I assume your values are int.
There is a function called Math.Abs(...).


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]



干杯

Andi


[/EDIT]

Cheers
Andi


我认为你要找的是 Math.Abs​​方法 [ ^ ]总是得到正数。
I think what you are looking for is the Math.Abs Method[^] to always get the positive number.


这篇关于这是检测负数的最佳方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆