-
Notifications
You must be signed in to change notification settings - Fork 1
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
Comments
Публикувайте решенията си като коментари тук. simple_mandelbrot :: [Char]
simple_mandelbrot = ... Коментирайте взаимно решенията си. |
-- 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 |
|
И за 2те решения мога да коментирам: |
Ако се намирате на място на което не е удобно да се ползват guard-ове (например не сте в |
Напишете израз
simple_mandelbrot
, стойността на който да е фракталът на Манделброт изрисуван сChar
-овер.Стойността на израза трябва да е
String
([Char]
), и когато го покажем на конзолата сputStr simple_mandelbrot
трябва да се визуализира по начин подобен на този в картинката(не е задължително да е точно както в примера).
Може да използвате всякакъв лист от ASCII символи за визуализация, но е добре да са подбрани така, че фрактала да се вижда отчетливо.
Препоръчвам да използвате следния набор от символи -
░▒▓█
.Изразът трябва да се смята динамично, а не да е просто hardcode-нат.
Ресурси
Публикувайте решенията като коментари в това
issue
.Заградете кода на решението си по следния начин за да се highlight-не правилно кода:
Всеки публикувал валидно решение ще получи бонус точка 🌟.
Дерзайте!
The text was updated successfully, but these errors were encountered: