用于创建径向渐变的公式

时间:2016-01-18 18:28:14

标签: c#

我需要编写自己的Radial Gradient生成器(不使用像RadialGradientBrush这样的东西)。

目前,我的代码如下所示:

public float[,] radGrad(int width, int height, float threshold) {
     float[,] grad = new float[width, height];
     float cX = width * 0.5f;
     float cY = height * 0.5f;

     for (int y = 0; y < height; ++y) {
          for(int x = 0; x < width; ++x) {
               float pixel = (Math.Max(y, cY) - Math.Min(y, cY)) / (Math.Max(x, cX) - Math.Min(x, cX));
               pixel = fLerp(pixel, 0, 255);
               grad[x, y] = pixel;
          }
      }
      return grad;
}

它产生了这个结果: enter image description here

如何纠正此问题以使其看起来像预期的结果: enter image description here

- 编辑: 接近@ 31eee384的建议......

public float[,] radGrad(int width, int height, float threshold) {
    float[,] grad = new float[width, height];
    float cX = width * 0.5f;
    float cY = height * 0.5f;

    for (int y = 0; y < height; ++y) {
        for(int x =0; x < width; ++x) {
            float distFromCenter = distance(cX, cY, x, y);
            float pixel = (Math.Max(distFromCenter, cY)  - Math.Min(distFromCenter, cY)) / (Math.Max(distFromCenter, height) - Math.Min(distFromCenter, height));
            pixel = fLerp(pixel, 0, 255);
            grad[x, y] = pixel;
        }
    }

    return grad;
}

只需弄清楚为什么它会在边缘添加白色。 enter image description here

1 个答案:

答案 0 :(得分:0)

我相信你应该犯罪!

public float[,] radGrad(int width, int height, float threshold) {
     float[,] grad = new float[width, height];
     int cX = width * 0.5f;
     int cY = height * 0.5f;


     for (int y = 0; y < cx; ++y) {
          for(int x = 0; x < cy; ++x) {
               float pixel = (Math.Sin(x / y));
               pixel = fLerp(pixel, 0, 255);
               grad[cx + x, cy + y] = pixel;
               grad[cx + x, cy - y] = pixel;
               grad[cx - x, cy + y] = pixel;
               grad[cx - x, cy - y] = pixel;
          }
      }
      return grad;
}

远非完美,但应该让你走上正确的道路。