【问题标题】:1/4 of Newton's fractal is drawn only仅绘制 1/4 的牛顿分形
【发布时间】:2018-10-16 02:07:14
【问题描述】:

我在使用 F# 绘制 f(x) = x^3 - 1 的牛顿分形时遇到问题 问题是我的程序似乎只绘制了分形的右下 1/4,没有别的。由于实际绘制的区域是正确的,我认为问题可能出在表单上的位图表示

这是我得到的图片的链接 https://imgur.com/a/YPSYIk9

到目前为止我想出的代码:

open System
open System.Drawing
open System.Windows.Forms
open System.Numerics

let pi = 3.14159265359
let MaxCount = 50
let multCol = 15
let Tol = 0.5
let r1 = Complex(1.0, 0.0)
let r2 = Complex(-0.5, sin(0.66*pi))
let r3 = Complex(-0.5, -sin(0.66 * pi))

let createImage () =
    let image = new Bitmap (800, 800,System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
    let graphics = Graphics.FromImage(image)
    let mutable maxMod = 0.0
    for x = 0 to image.Width - 1 do
        for y = 0 to image.Height - 1 do
            let mutable z = Complex(float x , float y)
            let mutable count = 0
            while (count < MaxCount && Complex.Abs(z - r1) >= Tol && Complex.Abs(z - r2) >= Tol && Complex.Abs(z - r3) >= Tol) do
                if(Complex.Abs(z) > 0.0) then
                    z <- z - (z*z*z - Complex(1.0,0.0))/(Complex(3.0,0.0) * z * z)
                if(Complex.Abs(z) > maxMod) then
                    maxMod <- Complex.Abs(z)
                count <- count + 1
            let temp1 = Complex.Abs(z - r1)
            let temp2 = Complex.Abs(z - r2)
            let temp3 = Complex.Abs(z - r3)
            if(Complex.Abs(z - r1) <= Tol) then
                let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Red)
                graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
            if(Complex.Abs(z - r2) <= Tol) then
                let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Blue)
                graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
            if(Complex.Abs(z - r3) <= Tol) then
                let Brush = new System.Drawing.SolidBrush(System.Drawing.Color.Green)
                graphics.FillRectangle(Brush, new Rectangle(x,y,1,1))
    image.Save("redacted.png")

createImage()

谁能给我提示一下这个问题实际上可能会发生什么?

【问题讨论】:

    标签: c# f# drawing fractals


    【解决方案1】:

    实际上您看到的是顶部而不是右下角,但它是上下颠倒的。你无法分辨,因为分形的右半部分关于 x 轴对称。

    问题是您是根据标准笛卡尔平面图绘制的,原点的右上角为 (+, +),并且表单的原点位于左上角,坐标向右和向下增加。

    要更正它,您需要将数学坐标转换为表单上的坐标,如下所示:

    new Rectangle(x,y,1,1)
    

    变成

    new Rectangle(x - xmin,ymin - y,1,1)
    

    其中xmin 和ymin 是您正在绘制的区域的最小范围。并注意 x 坐标和 y 坐标的计算之间的差异,因为 y 维度也需要翻转。

    【讨论】:

    • 据我了解,表格的左上角有 (0,0),对吗?我是否需要自己进行坐标转换的计算,或者是否有可以为我做的函数?
    • 你是对的。 (0, 0) 位于左上角。您还需要自己进行计算以移动坐标,但我已经更新了我的答案以显示可能需要的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-17
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多