为什么我们的C#图形代码不再起作用? [英] Why isn't our c# graphics code working any more?

查看:95
本文介绍了为什么我们的C#图形代码不再起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是情况:

我们有一些通用图形代码可用于我们的一个项目.在对代码进行了一些清理之后,似乎有些东西不再起作用了(图形输出看起来完全错误).

We have some generic graphics code that we use for one of our projects. After doing some clean-up of the code, it seems like something isn't working anymore (The graphics output looks completely wrong).

我对提供正确输出的代码的最后一个版本进行了比较,看来我们更改了其中一个功能,如下所示:

I ran a diff against the last version of the code that gave the correct output, and it looks like we changed one of our functions as follows:

static public Rectangle FitRectangleOld(Rectangle rect, Size targetSize)
{
    if (rect.Width <= 0 || rect.Height <= 0)
    {
        rect.Width = targetSize.Width;
        rect.Height = targetSize.Height;
    }
    else if (targetSize.Width * rect.Height > 
        rect.Width * targetSize.Height)
    {
        rect.Width = rect.Width * targetSize.Height / rect.Height;
        rect.Height = targetSize.Height;
    }
    else
    {
        rect.Height = rect.Height * targetSize.Width / rect.Width;
        rect.Width = targetSize.Width;
    }

    return rect;
}

static public Rectangle FitRectangle(Rectangle rect, Size targetSize)
{
    if (rect.Width <= 0 || rect.Height <= 0)
    {
        rect.Width = targetSize.Width;
        rect.Height = targetSize.Height;
    }
    else if (targetSize.Width * rect.Height > 
             rect.Width * targetSize.Height)
    {
        rect.Width *= targetSize.Height / rect.Height;
        rect.Height = targetSize.Height;
    }
    else
    {
        rect.Height *= targetSize.Width / rect.Width;
        rect.Width = targetSize.Width;
    }

    return rect;
}

我们所有的单元测试都通过了,除了某些语法上的捷径外,代码中的任何内容都没有改变.但是就像我说的那样,输出是错误的.我们可能会回到原来的代码,但是我很好奇是否有人知道这里发生了什么.

All of our unit tests are all passing, and nothing in the code has changed except for some syntactic shortcuts. But like I said, the output is wrong. We'll probably just revert back to the old code, but I'm curious if anyone has any idea what's going on here.

谢谢.

推荐答案

听起来像没有足够的单元测试:]

Sounds like you don't have sufficient unit tests :]

很遗憾,您的声明

除了某些语法上的快捷方式外,代码中的所有内容都没有改变"

"Nothing in the code has changed except for some syntactic shortcuts"

是错误的,我猜这就是您的问题所在. (这肯定是您的问题之一!)

is wrong, and I'm guessing that's where your problem is. (It's certainly one of your problems!)

是的

a *= b;

等效于

a = a * b;

但是

a *= b / c;

a = a * b / c;

代替

a *= b / c;    // equivalent to a = a * (b / c)
a = a * b / c; // equivalent to a = (a * b) / c

(请参见 c#运算符优先级 msdn)

我猜想当您的目标高度不是原始矩形高度的精确倍数(或宽度的相同)时,您会遇到麻烦.

I'm guessing you're running into trouble when your target height is not an exact multiple of the original rectangle height (or the same for the width).

然后您将遇到以下情况:

Then you'd end up with the following sort of situation:

让我们假设rect.Size =(8,20),targetSize =(15,25)

Let's assume rect.Size = (8, 20), targetSize = (15, 25)

使用您的原始方法,您将得出以下计算结果:

Using your original method, you'd arrive at the following calculation:

rect.Width     = rect.Width * targetSize.Height / rect.Height;
//             = 8          * 25                / 20
//             = 200 / 20 (multiplication happens first)
//             = 10
// rect.Width  = 10

使用新代码,您将拥有

rect.Width    *= targetSize.Height / rect.Height;
//            *= 25 / 20
//            *= 1 (it's integer division!)
// rect.Width  = rect.Width * 1
//             = 8
// rect.Width  = 8

这是不一样的. (如果目标尺寸小于您的原始尺寸会更糟;在这种情况下,整数除法将导致尺寸之一为0!)

which isn't the same. (It get's worse if the target size is less than your original size; in this case the integer division will result in one of the dimensions being 0!)

如果"[您的]单元测试全部通过",那么您肯定需要一些其他测试,尤其是那些处理非整数倍数的测试.

If "[your] unit tests are all passing" then you definitely need some additional tests, specifically ones that deal with non-integer multiples.

还要注意您的计算

else if(targetSize.Width * rect.Height > 
        rect.Width * targetSize.Height)

不可靠;对于非常大的矩形,它可能会溢出并给您不正确的结果.您最好将其转换为较大的类型(即long)作为乘法的一部分. (同样,应该对此进行一些单元测试)

isn't reliable; for very large rectangles, it has the potential to overflow and give you incorrect results. You'd be better off casting to a larger type (i.e. a long) as part of the multiplication. (Again, there should be some unit tests to this effect)

希望有帮助!

这篇关于为什么我们的C#图形代码不再起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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