椭圆纹理不符合预期

Ellipse Texture not doing what is expected

我在 monogame 中为简单的形状生成动态纹理。是的,我知道这个系统的缺点,但我只是在尝试构建我自己的物理引擎。我正在尝试生成椭圆的纹理,如 here.

所述

我有一个函数 PaintDescriptor,它接受 x 和 y 像素坐标并返回它应该是什么颜色。红色是我在调试的时候,一般是Color.Transparent.

public override Color PaintDescriptor(int x, int y)
{
      float c = (float)Width / 2;
      float d = (float)Height / 2;
      return pow((x - c) / c, 2) + pow((y - d) / d, 2) <= 1 ? BackgroundColor : Color.Red;
}

现在,如果宽度 == 高度,则此方法有效,因此,一个圆。但是,如果它们不相等,它会生成一个带有一些类似椭圆形状的纹理,但也会带有 banding/striping.

我试过看看我的宽度和高度是否被调换了,我还尝试了其他几种方法。需要注意的一件事是,在 desmos 的正常坐标系中,我有 (y + d) / d,但是由于屏幕的 y 轴被翻转,我必须翻转代码中的 y 偏移量:(y - d) / d.纹理生成和绘制的其余相关代码在这里:

public Texture2D GenerateTexture(GraphicsDevice device, Func<int, int, Color> paint)
{
    Texture2D texture = new Texture2D(device, Width, Height);

    Color[] data = new Color[Width * Height];

    for (int pixel = 0; pixel < data.Count(); pixel++)
        data[pixel] = paint(pixel / Width, pixel % Height);

    texture.SetData(data);

    return texture;
}

public void Draw(float scale = 1, float layerdepth = 0, SpriteEffects se = SpriteEffects.None)
{
    if (SBRef == null)
        throw new Exception("No reference to spritebatch object");

    SBRef.Draw(Texture, new Vector2(X, Y), null, null, null, 0, new Vector2(scale, scale), Color.White, se, layerdepth);
}

public float pow(float num, float power) //this is a redirect of math.pow to make code shorter and more readable
{
    return (float)Math.Pow(num, power);
}

为什么这不匹配desmos?为什么不是椭圆?

编辑:我忘了提及,但我遇到的一种可能的解决方案是始终画一个圆,然后将其缩放到所需的宽度和高度。这对我来说是不可接受的,因为绘图或其他人工制品可能会有些模糊,但更主要的是因为我想了解我目前无法通过此解决方案获得的任何信息。

在第 10 次睡觉并以全新的心态回来后,我找到了答案。在函数 GenerateTexture:

data[pixel] = paint(pixel / Width, pixel % Height);

应该是

data[pixel] = paint(pixel % Width, pixel / Height);