绘制真实坐标

发布于 2024-08-21 12:32:05 字数 82 浏览 5 评论 0原文

我已经实现了一个绘图类,目前只能处理整数值。我想获得有关处理浮点数的技术/机制的建议。使用的库是GDI。

谢谢,

阿迪

I've implemented a plotting class that is currently capable of handling integer values only. I would like to get advice about techniques/mechanisms in order to handle floating numbers. Library used is GDI.

Thanks,

Adi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

谈下烟灰 2024-08-28 12:32:06

在某些时候,它们需要转换为整数才能绘制实际像素。

然而,一般来说,您不想只想将每个 float 转换为 int,然后进行绘制 - 几乎肯定会弄得一团糟。相反,您需要/想要缩放浮点数,然后将缩放后的值舍入为整数。在大多数情况下,您需要使缩放因子可变,以便用户可以根据需要进行放大和缩小。

另一种可能性是让硬件处理大部分工作——您可以使用 OpenGL(例如)来渲染点,将它们保留为内部浮点,并让驱动程序/硬件处理缩放和转换为整数等问题。这有相当高的前期成本(学习足够的 OpenGL 来让它做任何有用的事情),但也可以获得相当可观的回报,例如快速、基于硬件的渲染,并使处理某些事情相对容易例如缩放以及(如果您需要的话)能够像 2D 一样轻松地显示 3D 点。

编辑:(主要是对评论的回应):最终归结为:屏幕的分辨率低于浮点数的分辨率。例如,真正高分辨率的屏幕可能水平显示 2048 个像素,即 11 位分辨率。即使单精度浮点数也有大约 24 位的精度。不管你怎么做,将 24 位分辨率降低到 12 位分辨率都会损失一些东西——通常是很多。

这就是为什么您几乎必须使缩放因子可变 - 这样用户就可以选择是缩小并以降低的分辨率查看整个图片,还是放大以高分辨率查看一小部分。

由于提到了子像素分辨率:它确实有帮助,但只有一点点。它不会解析映射到单个像素的一千个不同项目。

At some point, they need to be converted to integers to draw actual pixels.

Generally speaking, however, you do not want to just cast each float to int, and draw -- you'll almost certainly get a mess. Instead, you need/want to scale the floats, then round the scaled value to an integer. In most cases, you'll want to make the scaling factor variable so the user can zoom in and out as needed.

Another possibility is to let the hardware handle most of the work -- you could use OpenGL (for one example) to render your points, leaving them as floating point internally, and letting the driver/hardware handle issues like scaling and conversion to integers. This has a rather steep cost up-front (learning enough OpenGL to get it to do anything useful), but can have a fairly substantial payoff as well, such as fast, hardware-based rendering, and making it relatively easy to handle some things like scaling and (if you ever need it) being able to display 3D points as easily as 2D.

Edit:(mostly response to comment): Ultimately it comes down to this: the resolution of a screen is lower than the resolution of a floating point number. For example, a really high resolution screen might display 2048 pixels horizontally -- that's 11 bits of resolution. Even a single precision floating point number has around 24 bits of precision. No matter how you do it, reducing 24-bit resolution to 12-bit resolution is going to lose something -- usually a lot.

That's why you pretty nearly have to make your scaling factor variable -- so the user can choose whether to zoom out and see the whole picture with reduced resolution, or zoom in to see a small part at high resolution.

Since sub-pixel resolution was mentioned: it does help, but only a little. It's not going to resolve a thousand different items that map to a single pixel.

嘿看小鸭子会跑 2024-08-28 12:32:06

这些float值代表什么?我假设它们是一些坐标。您需要知道两件事:

  • 源分辨率(即绘制这些坐标的 dpi)
  • 您需要解决的范围

之后,这成为将点缩放到合适的整数坐标(基于根据您的屏幕分辨率)。

编辑:一个简单的公式是:

X(dst) = X(src) * DPI(dst) / DPI(src)

What do these float values represent? I will assume they are some co-ordinates. You will need to know two things:

  • The source resolution (i.e. the dpi at which these co-ordinates are drawn)
  • The range that you need to address

After that, this becomes a problem of scaling the points to suitable integer co-ordinates (based on your screen-resolution).

Edit: A simple formula will be:

X(dst) = X(src) * DPI(dst) / DPI(src)

萌逼全场 2024-08-28 12:32:06

您必须将它们转换为整数,然后将它们传递给 MoveTo()LineTo() 等函数。

You'll have to convert them to integers and then pass them to functions like MoveTo() and LineTo().

躲猫猫 2024-08-28 12:32:06

规模。例如,将所有整数值乘以 10。将浮点值乘以 10.0,然后截断或舍入(您的选择)。现在正常绘制。

这将使您的绘图更加精确。当您查看图片时,请记住比例因子。

否则,在绘图之前将浮点数转换为 int。

Scale. For example, multiply all the integral values by 10. Multiply the floating point values by 10.0 and then truncate or round (your choice). Now plot as normal.

This will give you extra precision in your graphing. Just remember the scale factor when you look at the picture.

Otherwise convert the floats to int before plotting.

一身仙ぐ女味 2024-08-28 12:32:06

您可以尝试使用 GDI+ 代替 GDI,它具有使用浮点坐标的功能。

You can try to use GDI+ instead GDI, it has functions that are using float coordinates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文