This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkrising's profile picture

[Solved] [Lua] [Maths] Terminal Glasses Bar Height Percentages

Started by darkrising, 08 August 2013 - 09:41 AM
darkrising #1
Posted 08 August 2013 - 11:41 AM
Hello, I've made a dynamic overlay that relays information from a rail craft boiler to terminal glasses.

At the moment I have three numbers in the top left of my screen showing the amount of steam, water and fuel.

They look like this: (numbers not exact)

Steam 5000 / 5000
Water 3000 / 3000
fuel 1000 / 1000

I'm trying to convert these numbers to bars graphs. However maths isn't my strong point.

I have written a function to convert numbers into a percentage but I'm having difficulty adjusting the bars height. The bar has to be 100px high.

function per(num, den)
  return math.ceil((num / den) * 100)
end

I would like to adjust the bar height to whatever needed instead of the 100px that the percentage gives but still maintain the percentage filled of the bar.

heres my testing code:

glass = peripheral.wrap("bottom")
glass.clear()
text = glass.addText(16,1,"",0xffffff)
box = glass.addBox(1,1,140,39, 0x000000,0.5)
box.setZIndex(-1)
line = glass.addBox(1,1,10,10, 0xffffff, 1)

ms = 500

function per(num, den)
  return math.ceil((num / den) * 100)
end

for s = 1, ms do
  sleep(0.1)
  num = per(s,ms)
  line.setHeight(s)
  text.setText(tostring(num).."%")
end
Vilsol #2
Posted 08 August 2013 - 11:51 AM
Just take the height you want to use, divide it by 100 and then use that to multiply with your percentage!
darkrising #3
Posted 08 August 2013 - 12:34 PM
Just take the height you want to use, divide it by 100 and then use that to multiply with your percentage!

Thanks, as easy as that, I feel stupid now :P/>