Why will the image (Mandelbrot) be skewed and wrapped?

So, I just wrote a small fragment to create a Mandelbrot fractal and imagine my surprise when it got all ugly and skewed (as you can see below). I would appreciate that this could happen. This is a learning experience, and I'm not looking for someone to do this for me, but I kind of brush aside the dead end. Trouble Code:

module Mandelbrot where import Complex import Image main = writeFile "mb.ppm" $ imageMB 1000 mandelbrotPixel xy = mb (x:+y) (0:+0) 0 mb cx iter | magnitude x > 2 = iter | iter >= 255 = 255 | otherwise = mb c (c+q^2) (iter+1) where q = x -- Mandelbrot -- q = (abs.realPart $ x) :+ (abs.imagPart $ x) --Burning Ship argandPlane x0 x1 y0 y1 width height = [ (x,y) | y <- [y1, y1 - dy .. y0], --traverse from x <- [x0, x0 + dx .. x1] ] --top-left to bottom-right where dx = (x1 - x0) / width dy = (y1 - y0) / height drawPicture :: (a -> b -> c) -> (c -> Colour) -> [(a, b)] -> Image drawPicture function colourFunction = map (colourFunction . uncurry function) imageMB s = createPPM ss $ drawPicture mandelbrotPixel (replicate 3) $ argandPlane (-1.8) (-1.7) (0.02) 0.055 s' s' where s' = fromIntegral s 

And the image code (which I'm pretty sure about):

 module Image where type Colour = [Int] type Image = [Colour] createPPM :: Int -> Int -> Image -> String createPPM whi = concat ["P3 ", show w, " ", show h, " 255\n", unlines.map (unwords.map show) $ i] 

Ugly mandelskew thing

+7
image haskell mandelbrot
source share
2 answers

Well, the image is distorted because the dimensions are wrong, but this is obvious. You specify the size of the image, and then spit out a list of pixels, but with an incorrect number of pixels per line.

In particular, note that the image wraps around almost exactly once. In other words, skew per line * height of the image = width of the image . Since the image is square, it means that you generate an extra pixel in the row - an old old mistake.

The obvious place to do this is when you generate the coordinates for repetition. Try a small set and see what it gives us:

 > length $ argandPlane (-2.5) (-2) 1.5 2 10 10 121 > 10 ^ 2 100 > 11 ^ 2 121 

So. I suspect the error is due to the fact that you are calculating the increment as the real distance, divided by the size of the pixel, which generates the correct number of intervals, but an extra point. Consider the interval from 0.0 to 1.0. Using your calculation with a width of 4, we get:

 > let x0 = 0.0 > let x1 = 1.0 > let width = 4.0 > let dx = (x1 - x0) / width > dx 0.25 > let xs = [x0, x0 + dx .. x1] > xs [0.0, 0.25, 0.5, 0.75, 1.0] > length xs 5 

So, to get the correct number of points, simply reduce the size by 1 when creating the coordinates.

+15
source share

This is a learning experience, and I'm not looking for someone to do this for me, but I'm a little retired to debug it

I know camcann already solved your problem, but it kind of “gave you fish,” while “teaching you how to fish” might be more helpful.

So, I will share what, in my opinion, can be a useful way to solve the problem.

So your image of the mandelbrot is distorted. Possible reasons:

  • You have a mistake in the mandelbrot formula.
  • You have an error while presenting / saving your image.

You can do an experiment to find out if any of the above explanations is relevant or not. Such an experiment can be, for example, drawing trivial images, say, horizontal and vertical lines.

After this experience, you will see that your vertical lines are not so vertical. Returning to possible possible reasons, it is clear that you have an error in presenting / saving your image, and this explains everything. You may have a mistake in your mandelbrot formula, but you probably aren't doing this, and that doesn't apply to the problem right now.

Now you should think about what error with saving the image will lead to the diagonal display of vertical lines. If the idea does not appear, you can make your simple example smaller and smaller, until the PPM result is small enough so that you can study it manually. Then you will surely catch the mistake.

+4
source share

All Articles