Simple turtle graphics: Difference between revisions

(Clarified non-negative numbers in bar-chart.)
Line 13:
 
::* Both functions should return the turtle to the location it was at and facing in the same direction as it was immediately before the function was executed.
 
=={{header|Julia}}==
{{trans|Wren}}
<lang julia>using Luxor, Colors
 
function house(🐢, x, y, siz)
# house wall
Reposition(🐢, x, y)
Rectangle(🐢, siz, siz)
# roof
HueShift(🐢)
Reposition(🐢, x - siz / 2, y - siz / 2)
Turn(🐢, -60)
Forward(🐢, siz)
Turn(🐢, 120)
Forward(🐢, siz)
# turtle_demo
doorheight, doorwidth = siz / 2, siz / 4
Pencolor(🐢, 0, 0, 0)
HueShift(🐢)
Reposition(🐢, x, y + doorheight / 2)
Rectangle(🐢, doorwidth, doorheight)
# window
windowheight, windowwidth = siz /3, siz / 4
HueShift(🐢)
Reposition(🐢, x + siz / 4, y - siz / 4)
Rectangle(🐢, windowwidth, windowheight)
Reposition(🐢, x - siz / 4, y - siz / 4)
Rectangle(🐢, windowwidth, windowheight)
end
 
function barchart(🐢, data, x, y, siz)
maxdata = maximum(data)
# scale to fit within a square with sides `siz` and draw bars of chart
barwidth = siz / length(data)
Pencolor(🐢, 1.0, 0.0, 0.5)
Reposition(🐢, x, y)
for n in data # draw each bar in chart
barheight = n * siz / maxdata
HueShift(🐢)
Reposition(🐢, x, y - barheight / 2)
Rectangle(🐢, barwidth, barheight)
x += barwidth
end
end
 
function testturtle(width = 400, height = 600)
dra = Drawing(600, 400, "turtle_demo.png")
origin()
background("midnightblue")
🐢 = Turtle()
Pencolor(🐢, "cyan")
Penwidth(🐢, 1.5)
house(🐢, -width / 3, height / 7, width / 2)
barchart(🐢, [15, 10, 50, 35, 20], width / 8, height / 8, width / 2)
finish()
end
 
testturtle()
</lang>
 
 
=={{header|Quackery}}==
4,105

edits