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

Analogue Clock- Monitors

Started by nitrogenfingers, 18 August 2012 - 02:39 PM
nitrogenfingers #1
Posted 18 August 2012 - 04:39 PM
This is something I hadn't intended to post, it's just a little thing but it's quite nice so why not.


local w,h = term.getSize()
local ttfl = (os.time()/24) * 2 * math.pi
local day = ttfl/8
function printFrame()
  for i=0,math.pi*2,math.pi/128 do
	local xco = math.cos(i) * w/2 + w/2 + 1
	local yco = math.sin(i) * h/2 + h/2 + 1

	term.setCursorPos(xco,yco)
	term.write("*")
  end
end
function printHands()
  local ttf = (os.time()/24) * math.pi * 2
  local inc = ttf - ttfl
  if ttf < ttfl then inc = 1 + ttf - ttfl end

  day = day + inc/8
  day = day % (math.pi * 2)
  
  ttfl = ttf
				
  for i=0,0.9,0.05 do
	local xco = math.cos(ttf-math.pi/2) * w/2 * i + w/2 + 1
	local yco = math.sin(ttf-math.pi/2) * h/2 * i + h/2 + 1

	term.setCursorPos(xco,yco)
	term.write("&amp;")
  end

  for i=0,0.5,0.05 do
	local xco = math.cos(day-math.pi/2) * w/2 * i + w/2 + 1
	local yco = math.sin(day-math.pi/2) * h/2 * i + h/2 + 1
  
	term.setCursorPos(xco,yco)
	term.write("#")
  end
end
while true do
  term.clear()
  printFrame()
  printHands()
  
  sleep(1)
end

Stick this on a computer, best when attached to a monitor and run it- it will display a little analogue clock. The longer hand refers to each of the 24 minutes that make up a minecraft "day", while the smaller hand for 12 days, because well I guess that's how a usual clock works. Would probably make more sense to have this correspond to the lunar cycle in minecraft… well if you want to do that just change the various 12's with however many phases the moon goes through. Edit: have made this change.

What makes it a bit cool is the graphics are entirely dynamic- trig is used to determine the display for the clock round and the hands so it will fit any size or screen ratio, which is kind of cool. It was designed for 6x6 and slightly off screen ratios look a bit squashy but I was quite partial to 1x2- makes me think of those really expensive watches girls sometimes wear.

The smallest and least impressive of apps but hey, nice little thing to stick on an empty monitor to keep track of time. It also demonstrates that you really can solve problems with math- I guess you really did have the last laugh Mr. Whiffen.
BigSHinyToys #2
Posted 18 August 2012 - 08:50 PM
That clock tower is epic. cool little app.
DKay #3
Posted 03 September 2012 - 08:16 PM
Something doesn't seem right, are you sure it isn't


local ttf = os.time() * math.pi * 2


On line 14?
nitrogenfingers #4
Posted 04 September 2012 - 03:21 AM
We divide os.time by 24 because there are 24 minutes in a minecraft day. If we didn't then the result would be feeding the sin and cos functions values between 0 and 24, making our clock hand rotate 24 times every day.
The minute hand represents the passing of a full minecraft day, rather than a minecraft minute (that would be os.time % 24), simply because the hand would move very fast and not be especially meaningful.

You can feed sine and cosine values greater than 2pi, but I don't consider it good practice so I try to keep everything normalized.
DKay #5
Posted 04 September 2012 - 06:25 PM
I see, I did not read your description fully. I was looking for a clock that accurately depicts minecraft hours/minutes on it's hours/minutes hands.

However, for anyone else interested in doing this, simply changing line 14 will cause incremental delays if the clock is left running too long.

I also had to change lines 13-14 from the bottom :


local xco = math.cos(ttfl/12-math.pi/2) * w/2 * i + w/2 + 1
local yco = math.sin(ttfl/12-math.pi/2) * h/2 * i + h/2 + 1


I'm guessing the inc function was messing with things since I changed the value of ttf. There's probably a more elegant way to do it but I'm not a coder.


PS : What does the % symbol do? Is it a modulo operation?
bigboss031 #6
Posted 21 November 2012 - 12:38 PM
I don't understand why i don't have the clock on monitor when i place it like you oO ? I have the clock into the computer but not on the screen. Could you help me ?
Thanks
nitrogenfingers #7
Posted 21 November 2012 - 01:05 PM
On your computer screen rather than a monitor? You have to run it using the monitor program:


monitor <side> <programname>

So in the example above, it would be


monitor top clock
overfritz #8
Posted 21 November 2012 - 05:36 PM
Definitely gonna test this out, maybe tweak it a little (don't worry, I'm not going to re-brand it…yet. xD).

Also, Minecraft has 8 lunar phases. I've taken the liberty to even find the Minecraft Wiki article on the moon.

The Moon goes through eightlunar phases. The phases are (in chronological order):
  • Full Moon
  • Waning Gibbous
  • Last Quarter
  • Waning Crescent
  • New Moon
  • Waxing Crescent
  • First Quarter
  • Waxing Gibbous
This would allow the player to keep track of the passage of time via the Lunar calendar. A full cycle of all eight phases is equal to one Lunar month or Lunation. Thirteen and 1/2 lunations is about equal to one lunar year.

So, in your code, I would replace the function dividing by 12 with 8?
nitrogenfingers #9
Posted 21 November 2012 - 07:29 PM
Yep. It still makes sense because 8 is a factor of 24, so I'll make the change to my provided code as well.

Honestly didn't expect… any interest in this program. Always nice to be proven wrong :(/>/>
overfritz #10
Posted 22 November 2012 - 12:10 PM
Nice fixing. Definitely will download and use for a lot of things. Also, perhaps you could upload it to pastebin to make downloading it easier?