Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Бонус задачка - Mandelbrot #1

Closed
ichko opened this issue Oct 24, 2020 · 6 comments
Closed

Бонус задачка - Mandelbrot #1

ichko opened this issue Oct 24, 2020 · 6 comments
Assignees

Comments

@ichko
Copy link
Owner

ichko commented Oct 24, 2020

Mandelbrot

Напишете израз simple_mandelbrot, стойността на който да е фракталът на Манделброт изрисуван с Char-овер.
Стойността на израза трябва да е String ([Char]), и когато го покажем на конзолата с putStr simple_mandelbrot трябва да се визуализира по начин подобен на този в картинката
(не е задължително да е точно както в примера).

Може да използвате всякакъв лист от ASCII символи за визуализация, но е добре да са подбрани така, че фрактала да се вижда отчетливо.
Препоръчвам да използвате следния набор от символи - ░▒▓█.

Изразът трябва да се смята динамично, а не да е просто hardcode-нат.

Ресурси


Публикувайте решенията като коментари в това issue.
Заградете кода на решението си по следния начин за да се highlight-не правилно кода:

```hs
simple_mandelbrot :: [Char]
simple_mandelbrot = ...
```

Всеки публикувал валидно решение ще получи бонус точка 🌟.
Дерзайте!

@ichko ichko closed this as completed Oct 24, 2020
@ichko
Copy link
Owner Author

ichko commented Oct 24, 2020

Публикувайте решенията си като коментари тук.

simple_mandelbrot :: [Char]
simple_mandelbrot = ...

Коментирайте взаимно решенията си.
Ако получите реакция ❤️ от мен или Свилен => получавате бонус точката.
Може да добавяте и screenshot-и с резултата на решението си.

@ichko ichko changed the title test Бонус задачка - Mandelbrot Oct 24, 2020
@ichko ichko self-assigned this Oct 24, 2020
@ichko ichko pinned this issue Oct 24, 2020
@ichko ichko unpinned this issue Oct 24, 2020
@ichko ichko changed the title Бонус задачка - Mandelbrot :start2: Бонус задачка - Mandelbrot Oct 24, 2020
@ichko ichko changed the title :start2: Бонус задачка - Mandelbrot Бонус задачка - Mandelbrot Oct 24, 2020
@hvtonev
Copy link

hvtonev commented Oct 31, 2020

-- Define a data structure for complex numbers and some operations with them
data Complex = Complex Float Float deriving (Show)

complexSum :: Complex -> Complex -> Complex
complexSum (Complex a b) (Complex c d) = (Complex (a + c) (b + d))

complexMultiply :: Complex -> Complex -> Complex
complexMultiply (Complex a b) (Complex c d) = (Complex (a * c - b * d) (a * d + b * c))

complexAbs :: Complex -> Float
complexAbs (Complex a b) = sqrt (a * a + b * b)

maxIters :: Integer
maxIters = 200

-- The main function which evaluates points depenging on how close they are to the mandelbrot set
mandelbrotIt :: Complex -> Complex -> Integer -> Integer
mandelbrotIt c z n = if n >= maxIters || complexAbs z > 2 then n else mandelbrotIt c newZ (n + 1)
  where
    newZ = complexSum (complexMultiply z z) c

mandelbrot :: Complex -> Integer
mandelbrot c = mandelbrotIt c (Complex 0 0) 0

-- Information for the image to be plotted and constraints
width :: Float
width = 250

height :: Float
height = 100

reStart :: Float
reStart = -2

reEnd :: Float
reEnd = 1

imStart :: Float
imStart = -1

imEnd :: Float
imEnd = 1

print_mandelbrotIt :: Float -> Float -> [Char]
print_mandelbrotIt x y = if x >= height then "" else if y >= width then "\n" ++ (print_mandelbrotIt (x + 1) 0) else getColor ++ (print_mandelbrotIt x (y + 1))
  where
    c = (Complex (reStart + (y / width) * (reEnd - reStart)) (imStart + (x / height) * (imEnd - imStart)))
    m = mandelbrot c
    getColor
      | m == 200 = ""
      | m > 20 = ""
      | m > 10 = ""
      | otherwise = ""

-- The final function generating the string
print_mandelbrot :: [Char]
print_mandelbrot = print_mandelbrotIt 0 0

main :: IO ()
main = putStrLn print_mandelbrot

mandelbrot_plot2

@dimitroffangel
Copy link
Contributor

dimitroffangel commented Nov 1, 2020

Code repository

@ichko
Copy link
Owner Author

ichko commented Nov 2, 2020

Code repository

Actual rendered output:
image

@ichko
Copy link
Owner Author

ichko commented Nov 2, 2020

И за 2те решения мога да коментирам:
Предпочитайте гардове (|condition = expression), пред влагане на if-then-else изрази.

@googleson78
Copy link

Ако се намирате на място на което не е удобно да се ползват guard-ове (например не сте в case, или в дефиниция на тяло на функция) има MultiWayIf разширението.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants