如何编写函数将画布上的点映射到真实平面上的点
我正在用 Python 在 pygame 屏幕上编写一个简单的 Mandelbrot 可视化工具。对于 600 x 600 屏幕上的每个像素,我正在绘制该像素(x,y)作为复数是否在 Mandelbrot 集中。
问题是我从 (0, 0) 开始并迭代到 (600, 600),其中大部分无论如何都在集合之外。因此,我输入了一个比例因子来放大,但我仍然只绘制右上象限。我想要某种方法来制作它,这样我的情节总是以 0+0i 为中心。
我想做的是找到某种方法将 600px^2 画布从 x 轴上的 [-2, 2] 映射到真实平面,到 y 轴上的 [2, -2] 。例如,这意味着复数 0+0i 将映射到屏幕上的 (300, 300)。这样,我的情节就会始终处于中心位置。
I'm writing a simple Mandelbrot visualiser in Python onto a pygame Screen. For each pixel on the 600 by 600 Screen, I am plotting whether or not this pixel, (x, y) as a complex number, is in the Mandelbrot set or not.
The problem being that I start at (0, 0) and iterate through to (600, 600), most of which is outside the set anyways. So I chuck in a scaling factor to zoom in, but I'm still only plotting the upper right quadrant. I would like some way of making it so my plot was always centered around 0+0i.
What I would like to do is find some kind of way to map the 600px^2 canvas to the real plane from [-2, 2] on the x-axis to [2, -2] on the y-axis. This would mean for instance, that the complex number 0+0i would map to (300, 300) on the screen. This way, my plot would always be centered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要一个数据窗口。您知道它的宽度为 600 像素,高度为 600 像素。像素坐标为 (0, 0) - (600, 600)。您可以这样做:
pixelX和pixelY是您想要转换为较小范围的像素坐标。 PixelWidth 和 height 是窗口的宽度和高度。 min/maxCoordX/Y 是 (-2,-2) 到 (2,2) 值。
You want a window for your data. You know it is 600 pixels wide and 600 pixels tall. The pixel coordinates are (0, 0) - (600, 600). You can do this:
pixelX and pixelY are the pixel coordinates that you want to convert to the smaller range. pixelWidth and height are the width and height of your window. min/maxCoordX/Y are the (-2,-2) to (2,2) values.
事实上,我会采取更灵活的方法,并编写代码以允许相当任意的映射,但要按照您的要求进行操作,您可以尝试:
然后对 y 进行相同的操作。这将每个像素视为屏幕上一对中心的点,但将像素的角映射到(+-2,+-2)。这是将整个视口映射到复平面中整个正方形的最“正确”方法,但这样做的一个缺点是您永远不会绘制一些重要的数字,例如 0、实线或虚线。
In truth I would take a more flexible approach, and write your code to allow fairly arbitrary mappings, but to do literally what you ask, you can try:
and then the same of
y
. This treats each pixel as being a point at the centre of the-on-screen pair, but maps the corners of the pixels to (+-2, +-2). This is the most "correct" way to map whole of your viewport to the whole of the square in the complex plane, but one downside of this is that you will never plot some important numbers, such as the 0, the real line or the imaginary line.Mapper的构造函数采用原来的左上角和右下角,以及新的左上角和右下角。 Mapper.map() 将一个点从原始空间转换到新空间。
Mapper's constructor takes the original upper left and lower right corners, and the new upper left and lower right corners. Mapper.map() converts a point from the original space to the new one.