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

Centering text on a monitor

Started by GamerNebulae, 01 June 2014 - 01:17 PM
GamerNebulae #1
Posted 01 June 2014 - 03:17 PM
Hello everyone!

I was trying to get my logical brain to work again, but I can't seem to wrap my head around this. If I do my logical math, it would like this:

--Size of the monitor
x = 39
y = 26
#text = 8 --#text is that same as string.length(text)
m = peripheral.wrap("right") --Wrapping the monitor
--Calculations
center = math.ceil(39 / 2) = 20 --Half of the monitor
xText = center - (#text / 2) = 20 -4 = 16 --Where the text starts
m.setCursorPos(xText, 1)
m.write(text)
--Controlling the figures
16 + 8 = 24
39 - 24 = 15 --About centered


But still the text looks very off-centered. Any thoughts?
KingofGamesYami #2
Posted 01 June 2014 - 03:31 PM
Try this code, It's what I usually use.

function printCenter( mon, text )
 local x, y = mon.getSize()
 mon.setCursorPos( x/2 - (#text / 2), 1)
 mon.write(text)
end
Edited on 01 June 2014 - 02:02 PM
GamerNebulae #3
Posted 01 June 2014 - 03:39 PM
Try this code, It's what I usually use.

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x - (#text / 2), 1)
mon.write(text)
end

How does that work? It seems like to me that it just gets the x-size of the monitor and subtracts the half of the length of the text, so half of it would "fall" off of the screen.
KingofGamesYami #4
Posted 01 June 2014 - 04:01 PM
Try this code, It's what I usually use.

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x - (#text / 2), 1)
mon.write(text)
end

How does that work? It seems like to me that it just gets the x-size of the monitor and subtracts the half of the length of the text, so half of it would "fall" off of the screen.
My mistake… Haven't written it in a long time.

Edit: fixed code.
Edited on 01 June 2014 - 02:02 PM
Whitecatblack #5
Posted 01 June 2014 - 04:18 PM
KingfGamesYami said:
My mistake… Haven't written it in a long time.

Edit: fixed code.
I believe another correction is due:

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x/2 - (#text / 2) + 1, 1)
mon.write(text)
end
This will center your text, and if the text length is an even amount and the monitor length is an odd amount, or vice versa, it will be centered slightly towards the left side of the monitor. Also, if you wanted to center it vertically as well:

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x/2 - (#text / 2) + 1, y/2)
mon.write(text)
end
Which will center it vertically unless the monitor height is an even amount, then it will center it slightly towards the top of the screen.

Whitecatblack
GamerNebulae #6
Posted 01 June 2014 - 04:34 PM
I believe another correction is due:

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x/2 - (#text / 2) + 1, 1)
mon.write(text)
end
This will center your text, and if the text length is an even amount and the monitor length is an odd amount, or vice versa, it will be centered slightly towards the left side of the monitor. Also, if you wanted to center it vertically as well:

function printCenter( mon, text )
local x, y = mon.getSize()
mon.setCursorPos( x/2 - (#text / 2) + 1, y/2)
mon.write(text)
end
Which will center it vertically unless the monitor height is an even amount, then it will center it slightly towards the top of the screen.

Whitecatblack

It was more sort of leaned towards to centering it in horizontal direction. I'll try it and!
TheOddByte #7
Posted 01 June 2014 - 07:45 PM
It was more sort of leaned towards to centering it in horizontal direction. I'll try it and!
Just to make sure you didn't mix up horizontal and vertical here

local w, h = term.getSize()

term.setCursorPos( math.ceil( w/2 ), 1 ) --# Horizontal, aka 'x' axis
term.setCursorPos( 1, math.ceil( h/2 ) ) --# Vertical, aka 'y' axis

If you meant horizontal then try this

local function centerPrint( text, y, mon )
    local w, h = mon.getSize() or term.getSize()
    if mon then
        mon.setCursorPos( math.ceil( w/2 - #text/2 ), y )
        mon.write( text )
    else
        term.setCursorPos( math.ceil( w/2 - #text/2 ), y )
        term.write( text )
    end
end
Whitecatblack #8
Posted 01 June 2014 - 08:12 PM
TheOddByte said:
-snip-
Although this code has an input for a y-value, it still has the same problem as Yami's did: when "text" is an odd value length, it doesn't center correctly. You would need to change it the same as I changed Yami's:

local function centerPrint( text, y, mon )
	local w, h = mon.getSize() or term.getSize()
	if mon then
		mon.setCursorPos( math.ceil( w/2 - #text/2 + 1 ), y )
		mon.write( text )
	else
		term.setCursorPos( math.ceil( w/2 - #text/2 ), y )
		term.write( text )
	end
end
With math.ceil(), instead of even value texts on odd value monitors, and vice versa, centering slightly to the right of the center, it centers them slightly to the left of the center instead.

Whitecatblack
GamerNebulae #9
Posted 01 June 2014 - 08:29 PM
Although this code has an input for a y-value, it still has the same problem as Yami's did: when "text" is an odd value length, it doesn't center correctly. You would need to change it the same as I changed Yami's:

local function centerPrint( text, y, mon )
	local w, h = mon.getSize() or term.getSize()
	if mon then
		mon.setCursorPos( math.ceil( w/2 - #text/2 + 1 ), y )
		mon.write( text )
	else
		term.setCursorPos( math.ceil( w/2 - #text/2 ), y )
		term.write( text )
	end
end
With math.ceil(), instead of even value texts on odd value monitors, and vice versa, centering slightly to the right of the center, it centers them slightly to the left of the center instead.

Whitecatblack

Yeah because of the way that math.floor actually rounds the number down to a lower number and math.ceil round the number upwards.