【问题标题】:C# Anti Aliasing AlgorithmC# 抗锯齿算法
【发布时间】:2016-06-29 22:50:27
【问题描述】:

我想用 C# 编写自己的抗锯齿算法。我的图片只有两种颜色;黑白相间。

在 Wikipedia (https://en.wikipedia.org/wiki/Spatial_anti-aliasing) 上我发现了以下伪解码:

Define function PlotAntiAliasedPoint ( number x, number y )
    For roundedx = floor ( x ) to ceil ( x ) do
         For roundedy = floor ( y ) to ceil ( y ) do
              percent_x = 1 - abs ( x - roundedx )
              percent_y = 1 - abs ( y - roundedy )
              percent = percent_x * percent_y
              DrawPixel ( coordinates roundedx, roundedy, color percent (range 0-1) )

如何在 C# 中实现这个伪代码?

【问题讨论】:

  • 它看起来相当不言自明。您具体需要哪些帮助?
  • 我听不懂For roundedx = floor ( x ) to ceil ( x ) do这一行
  • 这只是一个普通的for 循环,就像您在 Python 或 Java 或 Javascript 或 C 或 C++,或者......实际上是大多数语言中看到的那样。 for (int roundedx = Math.Floor(x); roundedx <= Math.Ceiling(x); roundedx++) { /* rest of stuff */ }
  • 我认为维基百科上的文章缺少一些关于变量的信息。 x 和 y 是什么以及为什么它们是十进制的并不明显。乍一看,如果 (x,y) 是图像上的一个点,例如 (1024 x 768),我会认为 x 是 0 到 1024 之间的整数,而 y 是 0 到 768 之间的整数,并且舍入不会使感觉。所以我不明白为什么这个问题有负面评价。我明白为什么有些人(包括我自己)在理解这个伪代码算法时遇到问题。

标签: c# algorithm antialiasing


【解决方案1】:
private void PlotAntiAliasedPoint(decimal x, decimal y)
{
  for (var roundedx = Math.Floor(x); roundedx <= Math.Ceiling(x); roundedx++)
  {
    for (var roundedy = Math.Floor(y); roundedy <= Math.Ceiling(y); roundedy++)
    {
      var percent_x = 1 - Math.Abs(x - roundedx);
      var percent_y = 1 - Math.Abs(y - roundedy);
      var percent = percent_x*percent_y;
      //DrawPixel(coordinates roundedx, roundedy, color percent(range 0 - 1))
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多