Last active
February 15, 2025 02:01
-
-
Save ket395/b0aa3243585a8e317656ee0bb4b2d008 to your computer and use it in GitHub Desktop.
Julia 2D hearts animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # rotation angle 'd' goes from 0 to 2π (one revolution) | |
| 0:2e-3:2π .|> d-> ( | |
| # screen buffer, with empty braille characters and new lines | |
| (P = fill(5<<11,64,25))[64,:] .= 10; | |
| # transformation vector: cis = rotation, sin = zoom | |
| z = 8cis(d)sin(.46d); | |
| # loop over all pixels | |
| for r=0:98, c=0:5^3 | |
| # (..)z = scale and rotate point, using complex numbers | |
| # reim = split complex number into x/y | |
| # mod = repeat pattern | |
| x,y = @.mod(2 - $reim((.016c-r/49im-1-im)z), 4) - 2; | |
| # test if point is within a heart shape | |
| √2(y+.5-√√x^2)^2 < 4-x^2 && | |
| # set pixel, using braille characters | |
| (P[c÷2+1,r÷4+1] |= Int(")*,h08H¨"[1+r&3+4&4c]) - 40) | |
| end | |
| # print buffer to screen | |
| println("\e[H\e[1;31m", join(Char.(P)))); | |
| # NOTE - Julia Syntax highlighting in Github editor(while editing) is crappy. | |
| #= | |
| fill(x) Create an array | |
| cis(x) More efficient method for exp(im*x) by using Euler's formula: \cos(x) + i \sin(x) = \exp(i x). | |
| sin(x) Compute sine of x, where x is in radians. | |
| =# |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment