commit 2e7c02ca8594f296cd2b45130b4206b81c76aa48 Author: Arity-T Date: Sat Nov 9 18:42:52 2024 +0300 fern diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c9b1dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.exe +*.hi +*.o +*.json +other +tmp +*.txt +*.pdf \ No newline at end of file diff --git a/lab2/.ghci b/lab2/.ghci new file mode 100644 index 0000000..4c3bab8 --- /dev/null +++ b/lab2/.ghci @@ -0,0 +1 @@ +:set -interactive-print=UnescapingPrint.uprint \ No newline at end of file diff --git a/lab2/draw_fern.ipynb b/lab2/draw_fern.ipynb new file mode 100644 index 0000000..13a99cc --- /dev/null +++ b/lab2/draw_fern.ipynb @@ -0,0 +1,63 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import matplotlib.pyplot as plt" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "with open('test.txt', 'r', encoding='utf-16') as file:\n", + " data_str = file.read()\n", + "\n", + "x, y = list(zip(*eval(data_str)))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plt.figure(figsize=(10, 10))\n", + "plt.scatter(x, y, s=0.2, color=\"green\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/lab2/fern.hs b/lab2/fern.hs new file mode 100644 index 0000000..e770f83 --- /dev/null +++ b/lab2/fern.hs @@ -0,0 +1,39 @@ +import System.Random (randomRIO) + +type Point = (Float, Float) + +transformation1 :: Point -> Point +transformation1 (_, y) = (0, 0.16 * y) + +transformation2 :: Point -> Point +transformation2 (x, y) = (0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6) + +transformation3 :: Point -> Point +transformation3 (x, y) = (0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6) + +transformation4 :: Point -> Point +transformation4 (x, y) = (-0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44) + +applyTransformation :: (Ord a, Fractional a) => Point -> a -> Point +applyTransformation point random + | random < 0.01 = transformation1 point + | random < 0.86 = transformation2 point + | random < 0.93 = transformation3 point + | otherwise = transformation4 point + +genNextPoint :: Point -> IO Point +genNextPoint point = do + random <- randomRIO (0.0, 1.0 :: Float) + return $ applyTransformation point random + +barnsleyFern :: Point -> Int -> IO [Point] +barnsleyFern _ 0 = return [] +barnsleyFern start n = do + x' <- genNextPoint start + xs <- barnsleyFern x' (n - 1) + return (start : xs) + +main :: IO () +main = do + fractal <- barnsleyFern (0, 0) 1000 + print fractal diff --git a/lab2/fern_with_mod.hs b/lab2/fern_with_mod.hs new file mode 100644 index 0000000..8ed7c6b --- /dev/null +++ b/lab2/fern_with_mod.hs @@ -0,0 +1,33 @@ +-- Не работает, потому что важно именно в случайном порядке применять трансформации + +type Point = (Float, Float) + +transformation1 :: Point -> Point +transformation1 (_, y) = (0, 0.16 * y) + +transformation2 :: Point -> Point +transformation2 (x, y) = (0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6) + +transformation3 :: Point -> Point +transformation3 (x, y) = (0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6) + +transformation4 :: Point -> Point +transformation4 (x, y) = (-0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44) + +applyTransformation :: Point -> Int -> Point +applyTransformation (x, y) n + | r == 0 = transformation1 (x, y) + | r <= 85 = transformation2 (x, y) + | r <= 92 = transformation3 (x, y) + | otherwise = transformation4 (x, y) + where + r = n `mod` 100 + +barnsleyFern :: Point -> Int -> [Point] +barnsleyFern _ 0 = [] +barnsleyFern point n = point : barnsleyFern (applyTransformation point n) (n - 1) + +main :: IO () +main = do + let fractal = barnsleyFern (0, 0) 1000 + print fractal diff --git a/lab2/task.jpg b/lab2/task.jpg new file mode 100644 index 0000000..a5a3e19 Binary files /dev/null and b/lab2/task.jpg differ