哪种代码更干净、更快或更优雅/更可取?是否已经有一个函数可以做到这一点?
我写这两个都是为了校正矩形。例如,如果一个矩形具有以下属性:
X:32,Y:32,宽度:-32,高度:-32
这将使矩形X:0,Y:0,宽度:32,高度:32
if (r.Width < 0)
{
r.X -= Math.Abs(r.Width);
r.Width = Math.Abs(r.Width);
}
if (r.Height < 0)
{
r.Y -= Math.Abs(r.Height);
r.Height = Math.Abs(r.Height);
}
这里是# 2
r.X -= Math.Abs(Math.Min(0, r.Width));
r.Width = Math.Abs(r.Width);
r.Y -= Math.Abs(Math.Min(0, r.Height));
r.Height = Math.Abs(r.Height);
它们都工作得很好。我的问题是哪个?我觉得第一个可能更快、更具可读性,但第二个代码行更少。另外,我觉得这可能已经被发明了。 .NET 或 XNA 框架中是否已经有一种方法可以执行此操作?
谢谢!!
编辑:有人发布了我非常喜欢的第二种方式的缩短版本,并将其组合成这样:
r = new Rectangle(r.X + Math.Min(0, r.Width), r.Y + Math.Min(0, r.Height), Math.Abs(r.Width), Math.Abs(r.Height));
老实说,我更喜欢更少的行而不是更多的行,我不认为这太复杂。如果有的话,我可以添加注释或将其封装到具有描述性名称的方法中。
I wrote both of these for correcting rectangles. For example, if a rectangle has these properties:
X:32, Y:32, Width:-32, Height:-32
This will make the rectangle X:0, Y:0, Width:32, Height:32
if (r.Width < 0)
{
r.X -= Math.Abs(r.Width);
r.Width = Math.Abs(r.Width);
}
if (r.Height < 0)
{
r.Y -= Math.Abs(r.Height);
r.Height = Math.Abs(r.Height);
}
Here is #2
r.X -= Math.Abs(Math.Min(0, r.Width));
r.Width = Math.Abs(r.Width);
r.Y -= Math.Abs(Math.Min(0, r.Height));
r.Height = Math.Abs(r.Height);
They both work just fine. My question is which? I feel like the first may be faster and more readable but the second has less lines of code. Also, I feel like this is something that may have been invented already. Is there a method that does this already in the .NET or XNA Framework?
Thanks!!
Edit: Someone posted a shortened version of the second way which I really like and combined it into this:
r = new Rectangle(r.X + Math.Min(0, r.Width), r.Y + Math.Min(0, r.Height), Math.Abs(r.Width), Math.Abs(r.Height));
Honestly I prefer less lines to more and I don't think this is too complex. If anything I can place a comment or encapsulate it in to a method with a descriptive name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您知道 r.Width 和 r.Height < 0 为什么你需要腹肌?删除它,它会更快、更简单。
恕我直言,你写的第二种方法实在是丑陋。你不用看它就能立即意识到它在做什么。记住吻。
编辑:我知道您喜欢尽可能少的代码行,因此我重新设计了上述内容:
同样简单 - 全部都在一行代码中!它的行数甚至比您的替代方案还要少......
行数通常不是衡量编程美感的好方法 - 但当然这是您的代码,因此您必须选择给您带来最大乐趣的代码。
If you know r.Width and r.Height are < 0 why do you need to Abs on them? Remove that and it will be even faster, and simpler.
IMHO the second way you've written of doing this is just plain ugly. You don't look at it and immediately realise what it's doing. Remember KISS.
EDIT: I understand you like your code in the fewest lines possible so I re-engineered the above:
Just as simple - and all in one line of code! It's even less characters on the line than your alternative....
Line count is generally not a good measure of programming beauty - but of course it's your code so you've got to go with what brings you the most joy.
第一个更具可读性,并且它也应该稍微快一些,因为如果没有什么可做的,它的作用会更少。
检查值是否为负数后,您知道它们是负数,因此您不需要任何
Math.Abs
:只是为了完整性,第二个也可以写成少
Math .Abs
调用,因为您知道当Math.Min
不为零时产生的符号:The first one is more readable, and it should also be slightly faster as it does less if there is nothing to do.
After checking if the values are negative, you know that they are negative, so you don't need any
Math.Abs
:Just for completeness, the second one can also be written will less
Math.Abs
calls as you know the sign of what comes out ofMath.Min
when it's not zero: