Graphics in Julia. Strange patterns, the reflection of a triangle from a straight line and the construction of the normals of a spherical cat in a vacuum

 3r33550. 3r3-31. 3r33536. Graphics in Julia. Strange patterns, the reflection of a triangle from a straight line and the construction of the normals of a spherical cat in a vacuum Julia 3r33538. . The six-year beta is finally over, so now you can not be afraid of changes in syntax. And while everyone is arguing whether it is good or bad to start indexing from one, the excited community has been actively messed up: new libraries are coming out, old ones are being updated, serious projects are being launched, and students are actively taught this language at universities. So let's not fall behind! Brew tea stronger, because this night we will code! 3r? 3539. 3r311. 3r33538. 3r? 3534.  3r33550.
Getting ready for work 3r3451. 3r? 3534.  3r33550. 3r33536. Here is 3r33538. There is a small review in Russian, as well there is a
on the habr. familiarity with the language and installation guide 3r33535. . Again, I focus on the need for 3r3324. Windows Management Framework 3r33538. , there will be problems with loading packages. 3r? 3534.  3r33550. The package
JuliaPRO
3r33538. after the update, now only
is included. Juno
. But personally, I prefer
Jupyter
: there were no problems with it on the laptop, plus it is convenient to work in the browser and immediately create notes and formulas, in general, ideal for creating reports, slides or manuals. 3r? 3539. 3r? 3534.  3r33550. 3r33466.
Marry Jupyter and Julia ??? [/b] 3r33469. 3r33333.  3r33550.
Download the latest version of Julia official site 3r33538.
 3r33550.
Link to Jupiter included
Anacondas
given above, I used the one that was in the old 3r3353. JuliaPRO

 3r33550.
Launch Julia. It is already possible to fully use the language, but only in interpreter mode. We execute commands:
 3r33550. 3r33518. 3r? 3519. julia>]r3r3550. pkg> add IJulia
pkg> build IJulia # if there was no automatic build 3r33526.
 3r33550.
Now in
Jupyter
The creation of the file Julia ???
is available.  3r33550.
3r33546. 3r33546. 3r? 3534.  3r33550. 3r33536. There are several packages for Julia, the most successful ones are included in 3r33445. Plots
in the form of backends. 3r? 3519. Plots.jl - metalanguage plotting: that is, an interface for various graph libraries. Therefore, 3r3-3519. Plots.jl actually just interprets your commands, and then creates graphs using any kind of graph library. These background graphic libraries are called backends. The best part is that you can use many different graphic libraries with the syntax Plots.jl , and we also see that Plots.jl adds new features to each of these libraries! 3r? 3539. 3r? 3534.  3r33550. 3r33466. Install the [/b] graphics packages. 3r33469. 3r33536. To install packages, run the commands in REPL, Juno, or Jupyter: 3r3353539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. # Pkg.add ("Plots") # So added packages up to version ???
julia>]r3r3550. pkg> add Plots
pkg> add GR 3r35050. pkg> add PyPlot
pkg> add Gadfly
pkg> add PlotlyJS
pkg> add UnicodePlots 3r33526. 3r? 3534.  3r33550. 3r33536. It is not necessary to install all the packages, but you should know that each of them has its own 3r3r114. Features 3r33538. . I prefer plotlyjs () : although it does not differ in speed, but it is very interactive. There is a zoom, moving on a plane, and also the ability to save a file, and if you save the document Jupyter as html, all features will remain. So you can add to the site or make an interactive presentation. More information on pages: Plots 3r33538. , 3r3122. Gadfly 3r? 3539. 3r33546. 3r33546. 3r? 3534.  3r33550.

Endless pattern based on prime numbers

3r? 3534.  3r33550. 3r33536. Implemented idea articles on Habré . In a few words: what if we take the coordinate of a point and use some operation between, say, 3r3443 between the abscissa and ordinate. XOR or bitwise AND and then check the number for simplicity or for belonging to Fibonacci numbers, and with a positive answer paint a dot in one color and a negative one in another? Check: 3r33939. 3r? 3534.  3r33550. 3r33466. For surgery% [/b] 3r33469. 3r33518. 3r? 3519. using Plots
plotlyjs () 3r33550. 3r33550. function eratosphen (n, lst) # sieve of Eratosthenes 3r-3550. ar =[i for i=1:n]3r33550. ar[1]= 0 3r350. for i = 1: n
if ar[i]! = 0 3r350. push! (lst, ar[i]) 3r35050. for j = i: i: n
ar[j]= 0 3r350. end
end
end
end
3r33550. ertsfn =[]3r33550. eratosphen (100? ertsfn) 3r33550. # print (ertsfn)
# print (size (ertsfn)) # -> 168
N = 80 3r350. M = 80 3r350. 3r33550. W1 =[in( x % y, ertsfn) for x = 1:N, y = 1:M]; 3r33550. W2 =[x % y for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x% y is prime?") 3r33550. p2 = spy (W? title = "x% y") 3r-3550. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r3181. 3r? 3539. 3r33546. 3r33546. 3r? 3534.  3r33550. 3r33466. For operation + [/b] 3r33469. 3r33518. 3r? 3519. W1 =[in( x + y, ertsfn) for x = 1:N, y = 1:M]; 3r33550. W2 =[x + y for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x + y is prime?") 3r33550. p2 = spy (W? title = "x + y") 3r-3550. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r3202. 3r? 3539. 3r33546. 3r33546. 3r? 3534.  3r33550. 3r33466. For operation | 3r33434. 3r33469. 3r33518. 3r? 3519. W1 =[in( x | y, ertsfn) for x = 1:N, y = 1:M]; 3r33550. W2 =[x | y for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x | y is prime?") 3r33550. p2 = spy (W? title = "x | y") 3r-3550. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. For operation & [/b] 3r33469. 3r33518. 3r? 3519. W1 =[in( x & y, ertsfn) for x = 1:N, y = 1:M]; 3r33550. W2 =[x & y for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x & y is prime?") 3r33550. p2 = spy (W? title = "x & y") 3r-3550. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. For operation xor [/b] 3r33469. 3r33518. 3r? 3519. W1 =[in( xor(x, y), ertsfn) for x = 1:N, y = 1:M]; 3r33550. W2 =[xor(x, y) for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x xor y is prime?") 3r33550. p2 = spy (W? title = "x xor y") 3r-3550. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. And now everything is the same, but for Fibonnachi numbers [/b] 3r33469. 3r33536. It is possible, as usual, to make a series with something like: 3r33539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. function fib (n) 3r35050. a = 0 3r350. b = 1 3r???. for i = 1: n
a, b = b, a + b
end
return a
end
fbncc = fib. ([i for i=1:10]) 3r33525. 3r33526. 3r? 3534.  3r33550. 3r33536. But let's use the matrix representation (3r33294. More 3r3-33538.): 3r33434.  3r33550. [1 1; 1 0]^ (n-1) # anonymous function raising the matrix to the power n-1 3r33550. mfbnc =[matr_fib( i )[1,1]for i = 1: 17]; # Element 1.1 of this matrix is ​​the nth element of the Fibonacci set 3r33550. N = 100 3r?550. M = N 3r33550. 3r33550. W1 =[in( x % y, mfbnc) for x = 1:N, y = 1:M]; 3r33550. W2 =[in( x | y, mfbnc) for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x% y ∈ fibonacci?") 3r35050. p2 = spy (W? title = "x | y ∈ fibonacci?") 3r35050. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r33333. 3r? 3539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. W1 =[in( xor(x, y), mfbnc) for x = 1:N, y = 1:M]; 3r33550. W2 =[in( x & y, mfbnc) for x = 1:N, y = 1:M]; 3r33550. p1 = spy (W? title = "x xor y ∈ fibonacci?") 3r35050. p2 = spy (W? title = "x & y ∈ fibonacci?") 3r35050. plot (p? p? layout = (2), legend = false) 3r-3525. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r33333. 3r? 3539. 3r33546. 3r33546. 3r? 3534.  3r33550.

Reflection of a figure relative to a straight line

3r? 3534.  3r33550. 3r33536. This type of display is determined by the matrix: 3r33539. 3r? 3534.  3r33550. 3r33536. 3r? 3534.  3r33550. 3r33536. 3r33357. 3r? 3539. 3r? 3534.  3r33550. 3r33536. These matrices allow for transformations for different polygons, the main thing is to write down the coordinates under each other and not forget about the column of ones at the end. Thus[T]: 3r? 3534.  3r33550. 3r33333.  3r33550. shifts the straight line to the origin along with the polygon to be transformed  3r33550. rotates it to coincide with the axis X  3r33550. reflects all points of the polygon with respect to X  3r33550. This is followed by a reverse rotation and shift  3r33550. 3r? 3534.  3r33550. 3r33536. In more detail the subject is opened in the book Rogers, D., Adams, J. Mathematical foundations of computer graphics 3-33538. 3r? 3539. 3r? 3534.  3r33550. 3r33536. And now code it! 3r? 3539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. using Plots
plotlyjs () 3r33550. f = x -> 0.4x + 2 # function defining a straight line 3r33550. # X and igaki transpose into columns 3r3550. X =[2 4 2 2]'
Y =[4 6 6 4]'
xs =[-2; 7]# segment belonging to the straight
ys = f (xs) 3r33550. inptmtrx =[X Y ones( size(X, 1), 1 ) ]# we assign a column of units 3r33550. 3r33550. m = 0 3r350. n = -f (0) # offset in Y
displacement =[1 0 0; 0 1 0; m n 1]3r33550. 3r33550. a = (ys[2]-ys[1]) /(xs[2]-xs[1]) # tangent of the angle of the straight 3r-3550. θ = -atan (a)
rotation =[cos(θ) sin(θ) 0; -sin(θ) cos(θ) 0; 0 0 1]3r33550. reflection =[1 0 0; 0 -1 0; 0 0 1]3r33550. 3r33550. T = displacement * rotation * reflection * rotation ^ (- 1) * displacement ^ (- 1) # full transformation matrix 3r350. outptmtrx = triangle * T
plot (X, Y) 3r350. plot! (xs, ys)
plot! (outptmtrx[:,1], outptmtrx[:,2]) 3r33535. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r33434. 3r? 3534.  3r33550. 3r33434. Interesting fact: if you get rid of the Greek characters and replace the first line with 3r33939. 3r? 3534.  3r33550. 3r33518. 3r? 3519. function y = f (x, t) 3r33550. y = 0.4 * x + 2
endfunction, 3r33525. 3r33526. 3r? 3534.  3r33550. 3r33536. brackets []3r3442. framing array indices on () 3r3442. , and rafts on 3r33519. plot (X, Y, xs, ys, trianglenew (:, 1), trianglenew (:, 2)) 3r3353525. , then this code runs in 3r3343445. Scilab . 3r? 3539. 3r? 3534.  3r33550. 3r33450. Work with three-dimensional graphics 3r? 3534.  3r33550. 3r33536. Some of the listed packages support the construction of three-dimensional graphs. But separately I would like to mention a rather powerful visualization tool
Makie working coupled with packages GLFW 3r33538. and GLAbstraction implementing the capabilities of OpenGL in julia. More information about Makie . Its testing will hide under 3r33939. 3r? 3534.  3r33550. 3r33466. spoiler [/b] 3r33469. 3r33518. 3r? 3519. using Makie
3r33550. N = 51 3r350. x = linspace (-? ? N) 3r33550. y = x 3r350. z = (-x. * exp. (- x. ^ 2 .- (y '). ^ 2)). * 4 3r3-3550. 3r33550. scene = wireframe (x, y, z)
xm, ym, zm = minimum (scene.limits[])
scene = surface! (scene, x, y, z)
contour! (scene, x, y, z, levels = 1? linewidth = ? transformation = (: xy, zm))
scene 3r33526. 3r? 3534.  3r33550. 3r33536. 3r33526. 3r? 3534.  3r33550. 3r33536. 3r3499. 3r? 3539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. using fileIO 3r350. 3r33550. scene = Scene (resolution = (50? 500)) 3r350. catmesh = FileIO.load (Makie.assetpath ("cat.obj"), GLNormalUVMesh)
mesh (catmesh, color = Makie.loadasset ("diffusemap.tga")) 3r33526. 3r? 3534.  3r33550. 3r33536. 3r???. 3r? 3539. 3r? 3534.  3r33550. 3r33518. 3r? 3519. x = Makie.loadasset ("cat.obj")
mesh (x, color =: black)
pos = map (x.vertices, x.normals) do p, n
p => p. + (normalize (n). * ???f0) 3r35050. end
linesegments! (pos, color =: blue) 3r33526. 3r? 3534.  3r33550. 3r33536. 3r? 3530. 3r? 3539. 3r33546. 3r33546. 3r? 3534.  3r33550. 3r33536. On this with all the graphics. Interactivity, animation, 3d, big data or fast construction of the simplest graphs - constantly evolving packages will satisfy almost every taste and need, moreover, everything is quite easy to master. Feel free to swing
practical tasks 3r33538. and continues to comprehend Julia! 3r? 3539. 3r33546. 3r33550. 3r33550. 3r? 3543. ! function (e) {function t (t, n) {if (! (n in e)) {for (var r, a = e.document, i = a.scripts, o = i.length; o-- ;) if (-1! == i[o].src.indexOf (t)) {r = i[o]; break} if (! r) {r = a.createElement ("script"), r.type = "text /jаvascript", r.async =! ? r.defer =! ? r.src = t, r.charset = "UTF-8"; var d = function () {var e = a.getElementsByTagName ("script")[0]; e.parentNode.insertBefore (r, e)}; "[object Opera]" == e.opera? a.addEventListener? a.addEventListener ("DOMContentLoaded", d,! 1): e.attachEvent ("onload", d ): d ()}}} t ("//mediator.mail.ru/script/2820404/"""_mediator") () (); 3r3544. 3r33550. 3r33546. 3r33550. 3r33550. 3r33550. 3r33550.
+ 0 -

Add comment