69 lines
1.6 KiB
Text
69 lines
1.6 KiB
Text
require "bitmap"
|
|
|
|
local w = 600
|
|
local h = 600
|
|
local bmp = bitmap.of(w, h, color.white, "Simple_turtle_graphics")
|
|
local t = turtle.of(bmp)
|
|
|
|
local function drawhouse(size)
|
|
-- Save initial turtle position and direction.
|
|
local saveX = t.x
|
|
local saveY = t.y
|
|
local saveD = t.dir
|
|
|
|
t.pen.width = 2
|
|
|
|
-- Draw house.
|
|
t:rect(w // 4, h // 2, size, size)
|
|
|
|
-- Draw roof.
|
|
t:right(30)
|
|
t:walk(size)
|
|
t:right(120)
|
|
t:walk(size)
|
|
|
|
-- Draw door.
|
|
local doorWidth = size // 4
|
|
local doorHeight = size // 2
|
|
t:rect(w // 4 + doorWidth // 2, h // 2 + doorHeight, doorWidth, doorHeight)
|
|
|
|
-- Draw window
|
|
local windWidth = size // 3
|
|
local windHeight = size // 4
|
|
t:rect(w // 4 + size // 2, h // 2 + size // 2, windWidth, windHeight)
|
|
|
|
-- Restore initial turtle position and direction.
|
|
t.x = saveX
|
|
t.y = saveY
|
|
t.dir = saveD
|
|
end
|
|
|
|
-- 'nums' assumed to be all non-negative.
|
|
local function barChart(nums, size)
|
|
-- Save intial turtle position and direction.
|
|
local saveX = t.x
|
|
local saveY = t.y
|
|
local saveD = t.dir
|
|
|
|
-- Find maximum.
|
|
local max = nums:max()
|
|
|
|
-- Scale to fit within a square with sides 'size' and draw chart.
|
|
local barWidth = size // #nums
|
|
local startX = w // 2 + 20
|
|
local startY = h // 2
|
|
for i = 1, #nums do
|
|
local barHeight = math.round(nums[i] * size / max)
|
|
t:rect(startX, startY - barHeight, barWidth, barHeight)
|
|
startX += barWidth
|
|
end
|
|
|
|
-- restore intial turtle position and direction
|
|
t.x = saveX
|
|
t.y = saveY
|
|
t.dir = saveD
|
|
end
|
|
|
|
drawhouse(w // 4)
|
|
barChart({15, 10, 50, 35, 20}, w // 3)
|
|
bmp:view()
|