Jump to content




Centering text on a monitor


  • You cannot reply to this topic
8 replies to this topic

#1 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 01 June 2014 - 02: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?

#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 June 2014 - 02: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 by KingofGamesYami, 01 June 2014 - 03:02 PM.


#3 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 01 June 2014 - 02:39 PM

View PostKingofGamesYami, on 01 June 2014 - 02:31 PM, said:

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.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 01 June 2014 - 03:01 PM

View PostGamerNebulae, on 01 June 2014 - 02:39 PM, said:

View PostKingofGamesYami, on 01 June 2014 - 02:31 PM, said:

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 by KingofGamesYami, 01 June 2014 - 03:02 PM.


#5 Whitecatblack

  • Members
  • 49 posts
  • LocationUnited States (EST)

Posted 01 June 2014 - 03: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

#6 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 01 June 2014 - 03:34 PM

View PostWhitecatblack, on 01 June 2014 - 03:18 PM, said:

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!

#7 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 01 June 2014 - 06:45 PM

View PostGamerNebulae, on 01 June 2014 - 03:34 PM, said:

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


#8 Whitecatblack

  • Members
  • 49 posts
  • LocationUnited States (EST)

Posted 01 June 2014 - 07: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

#9 GamerNebulae

  • Members
  • 216 posts
  • LocationNetherlands

Posted 01 June 2014 - 07:29 PM

View PostWhitecatblack, on 01 June 2014 - 07:12 PM, said:

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.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users