Luxor Trials

After adding the Luxor package to the Julia installation we try it out using examples from the tutorial:


                using Luxor
                Drawing(600, 500, "Drawing_01.svg")
                origin()
                
                setopacity(0.5)
                setfont("Georgia Bold", 18)
                settext("Georgia: a serif typeface designed in 1993 by Matthew Carter.", halign="center")
                
                sethue("green")
                circle(Point(0, 0), 200, :stroke)

                # A vertical arrow going through the origin.
                # The arrow goes down the image because the from point (specified first) is higher up
                # than the to point (specified second).
                sethue("gray30")
                arrow(Point(0, -200), Point(0, 200))

                finish()
                preview()
            

We put a silver border around the image and size it to fit on the page, using html / css. The result is the following:

Luxor lives up to its name by being a simple, procedural and static way of creating static images!

In the next example the function 'rule()' is used to draw a line through a point, optionally at an angle to the current x-axis.


                using Luxor
                Drawing(600, 500, "Drawing_02.svg")
                origin()
                box(BoundingBox() * 0.9, :stroke)
                for x in 10 .^ range(0, length=100, stop=3)
                    rule(Point(x, 0), pi/2,  boundingbox=BoundingBox() * 0.9)
                    rule(Point(-x, 0), pi/2,  boundingbox=BoundingBox() * 0.9)
                end
                finish()
                preview()
            

For trying things in the REPL a macro shortcuts the preamble:


                @png begin
                    lh = Point(-100,0)
                    rh = Point(100,0)
                    mp = midpoint(lh, rh)

                    line(lh, rh, :stroke)
                    sethue("blue")
                    line(mp, Point(mp.x, 100), :stroke)
                    sethue("red")
                    line(mp, Point(mp.x, -100), :stroke)
                end
            

The above code draws a simple cross centred at the origin. The blue line goes down and the red one up showing that the y axis is still positive down the screen and negative going up. The coordinate system is centred but still upside down compared to the usual math style.

The usual setup is obtained by reflecting in the x-axis using transform([1 0 0 -1 0 0])


                @png begin
                    transform([1 0 0 -1 0 0])
                    lh = Point(-100,0)
                    rh = Point(100,0)
                    mp = midpoint(lh, rh)

                    line(lh, rh, :stroke)
                    sethue("blue")
                    line(mp, Point(mp.x, 100), :stroke)
                    sethue("red")
                    line(mp, Point(mp.x, -100), :stroke)
                end
            

Now the blue line goes up and the red line goes down. Showing that the y axis is positive up the screen and negative down the screen.

There is an issue with using the math style y-axis: text will be written upside down.

When there is no requirement to have a two-colour y-axis then the code can be simplified as follows. Without the use of bounding box the code draws x and y axes filling the whole drawing. Adding bounding box our graphic will have a frame and axes that fill the frame.


                using Luxor
                Drawing(600, 600, "Drawing_03.svg")
                origin()
                transform([1 0 0 -1 0 0])
                box(BoundingBox() * 0.9, :stroke)
                rule(O, boundingbox=BoundingBox() * 0.9)        # draws an x axis
                rule(O, pi/2,  boundingbox=BoundingBox() * 0.9) # draws a  y axis
                finish()
                preview()
            

N.B. The first parameter to 'rule()' in this example is the capital letter Oh: a shortcut for Point(0, 0).