Microsoft Small Basic

Program Listing: TZK617
' Cargo Crates
' Copyright (c) 2012 Nonki Takahashi
'
' Calculate the level that a boat will sit in the water as you add cargo to it.
' Assume the boat has a length of 50 [m] and width of 5 [m] and has vertical sides,
' and its unloaded mass is 100 [t].
' If cargo crates are 2 x 2 x 5 [m] and weigh 5 [ton] each, how many crates can
' it hold to not exceed a total draft of 4 [m].
' Do a graphical display of the boat and crates to scale as it is loaded.

GraphicsWindow.Width = 600
GraphicsWindow.Height = 400
GraphicsWindow.BackgroundColor = "LightBlue"
SetCargoColor()
DrawPort()
AddBoat()
area = 5 * 50 ' area of boat base [m^2]
weight = 100 ' total weight of boat [t] = [m^3 of water]
draft = weight / area ' [m]
xBoat = 50
yBoat = 300 + (draft * 10)
MoveBoat()
AddWater()
AddData()
While draft < 4
AddCargo()
weight = 100 + nCargo * 5 ' [t]
draft = weight / area ' [m]
yBoat = 300 + (draft * 10)
MoveBoat()
Shapes.SetText(oNumCargo, nCargo)
Shapes.SetText(oWeight, weight + " [t]")
Shapes.SetText(oDraft, draft + " [m]")
If nCargo <= 20 Or 160 < nCargo Then
Program.Delay(200)
EndIf
EndWhile

Sub SetCargoColor
nColor = 5
colorCargo[1] = "Silver"
colorCargo[2] = "FireBrick"
colorCargo[3] = "SteelBlue"
colorCargo[4] = "DarkGreen"
colorCargo[5] = "GoldenRod"
EndSub

Sub DrawPort
GraphicsWindow.BrushColor = "DimGray"
GraphicsWindow.FillRectangle(0, 280, 600, 120)
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawLine(0, 280, 600, 280)
EndSub

Sub AddBoat
GraphicsWindow.BrushColor = "Black"
oUpper = Shapes.AddRectangle(500,20)
GraphicsWindow.BrushColor = "DarkRed"
oLower = Shapes.AddRectangle(500,40)
GraphicsWindow.PenColor = "White"
GraphicsWindow.BrushColor = "White"
oScale40 = Shapes.AddRectangle(10,2)
oNum40 = Shapes.AddText("40")
oScale20 = Shapes.AddRectangle(10,2)
oNum20 = Shapes.AddText("20")
EndSub

Sub MoveBoat
' param xBoat
' param yBoat
Stack.PushValue("local", i)
Shapes.Move(oUpper, xBoat, yBoat - 60)
Shapes.Move(oLower, xBoat, yBoat - 40)
Shapes.Move(oScale40, xBoat + 2, yBoat - 40)
Shapes.Move(oNum40, xBoat + 15, yBoat - 40 - 10)
Shapes.Move(oScale20, xBoat + 2, yBoat - 20)
Shapes.Move(oNum20, xBoat + 15, yBoat - 20 - 10)
For i = 1 To nCargo
col = Math.Remainder(Math.Ceiling(i / 2) - 1, 10)
row = Math.Floor((i - 1) / 20)
Shapes.Move(oCargo[i], xBoat + 50 * col, yBoat - 80 - 20 * row)
EndFor
i = Stack.PopValue("local")
EndSub

Sub AddWater
GraphicsWindow.BrushColor = "DarkBlue"
GraphicsWindow.PenColor = "Blue"
oWater = Shapes.AddRectangle(600,100)
Shapes.SetOpacity(oWater, 70)
Shapes.Move(oWater, 0, 300)
EndSub

Sub AddCargo
nCargo = nCargo + 1
GraphicsWindow.BrushColor = colorCargo[Math.GetRandomNumber(nColor)]
GraphicsWindow.PenColor = "Black"
oCargo[nCargo] = Shapes.AddRectangle(50, 20)
EndSub

Sub AddData
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(300, 15, "Number of cargo crates =")
oNumCargo = Shapes.AddText("0")
Shapes.Move(oNumCargo, 460, 15)
GraphicsWindow.DrawText(300, 30, "Total weight =")
oWeight = Shapes.AddText(weight + " [t]")
Shapes.Move(oWeight, 460, 30)
GraphicsWindow.DrawText(300, 45, "Total draft =")
oDraft = Shapes.AddText(draft + " [m]")
Shapes.Move(oDraft, 460, 45)
EndSub