|
| 1 | +<!-- Copyright 2024 by Alexei Bezborodov <[email protected]> --> |
| 2 | +<!-- License: Public domain: http://unlicense.org/ --> |
| 3 | +<!-- Общественное достояние --> |
| 4 | +<!DOCTYPE html> |
| 5 | +<html> |
| 6 | + |
| 7 | +<head> |
| 8 | +<meta charset="UTF-8"> |
| 9 | +<title>Фрактал SVG</title> |
| 10 | +<script type=" text/javascript" src=" https://cdn.jsdelivr.net/npm/[email protected]/brython.min.js" ></script> |
| 11 | +<script type=" text/javascript" src=" https://cdn.jsdelivr.net/npm/[email protected]/brython_stdlib.js" ></script> |
| 12 | +</head> |
| 13 | +<body onload="brython()"> |
| 14 | + |
| 15 | +<h1 class="text-center">Фрактал SVG</h1> |
| 16 | +<p id="out_svg"></p> |
| 17 | + |
| 18 | +<script type="text/python"> |
| 19 | + |
| 20 | +from browser import document, html, window |
| 21 | + |
| 22 | +def Print2String(*args, **kwargs): |
| 23 | + newstr = "" |
| 24 | + for a in args: |
| 25 | + newstr += str(a) + '' |
| 26 | + return newstr |
| 27 | + |
| 28 | +color_black = "black" |
| 29 | +color_test_rgb = "rgb(100,255,0)" |
| 30 | + |
| 31 | +def SvgDrawRect(svg, x, y, width, height, color, line_color, line_width): |
| 32 | + svg += [Print2String("<rect x='",x,"' y='",y,"' width='", width,"' height='", height,"' style=\"fill:",color,";stroke-width:",line_width,";stroke:",line_color,"\" />")] |
| 33 | + |
| 34 | +def SvgDrawLine(svg, x1, y1, x2, y2, color, width): |
| 35 | + svg += [Print2String("<line x1='",x1,"' y1='", y1,"' x2='",x2,"' y2='",y2,"' style=\"stroke:",color,";stroke-width:",width,"\" />")] |
| 36 | + |
| 37 | +def SvgMake(svg, width, height): |
| 38 | + result = Print2String("<svg width='",width,"' height='",height,"' xmlns=\"http://www.w3.org/2000/svg\" xmlns:svg=\"http://www.w3.org/2000/svg\">") |
| 39 | + for c in svg: |
| 40 | + result += c |
| 41 | + result += "</svg>" |
| 42 | + return result |
| 43 | + |
| 44 | +def SvgDrawSquare(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width): |
| 45 | + SvgDrawLine(svg, x1, y1, x2, y2, color, width) |
| 46 | + SvgDrawLine(svg, x2, y2, x3, y3, color, width) |
| 47 | + SvgDrawLine(svg, x3, y3, x4, y4, color, width) |
| 48 | + SvgDrawLine(svg, x1, y1, x4, y4, color, width) |
| 49 | + |
| 50 | +def SvgDrawSquareFractal(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width, dep): |
| 51 | + if dep < 1: |
| 52 | + return |
| 53 | + SvgDrawSquare(svg, x1, y1, x2, y2, x3, y3, x4, y4, color, width) |
| 54 | + |
| 55 | + koef = 0.15 |
| 56 | + SvgDrawSquareFractal(svg, x1 + (x2 - x1) * koef, y1 + (y2 - y1) * koef, x2 + (x3 - x2) * koef, y2 + (y3 - y2) * koef, x3 + (x4 - x3) * koef, y3 + (y4 - y3) * koef, x4 + (x1 - x4) * koef, y4 + (y1 - y4) * koef, color, width, dep - 1) |
| 57 | + |
| 58 | + |
| 59 | +svg = [] |
| 60 | +w = 500 |
| 61 | +h = 500 |
| 62 | +SvgDrawRect(svg, 0, 0, w, h, color_black, color_black, 1) |
| 63 | + |
| 64 | +SvgDrawSquareFractal(svg, 0, 0, w, 0, w , h, 0, h, color_test_rgb, 3, 37) |
| 65 | + |
| 66 | +document["out_svg"].innerHTML = SvgMake(svg, w, h) |
| 67 | + |
| 68 | +</script> |
| 69 | + |
| 70 | +</body> |
| 71 | + |
| 72 | +</html> |
0 commit comments