# Math Help

## Recommended Posts

Hello, i've got a question, how can i invert my text size?

To clarify these are my maths:

```
local text = "~HyPeX~ASdasdasd"
local size = dxGetTextWidth(text,1,"default")
local max = 100
local size = size/max
local tsize = size * -4+4
outputConsole(tsize)

```

So far, it works kinda good, but the sizes vary too much. I.e: big texts are excesively small and small texts are excesively big..

What exactly are you trying to do?

What exactly are you trying to do?

Make the text always fit in a 100 pixel width space, having the size 1 as maximum and getting smaller if it is too big.

What exactly are you trying to do?

Make the text always fit in a 100 pixel width space, having the size 1 as maximum and getting smaller if it is too big.

I'm not sure how the math wouldbe, but you could use a while loop, something like this:

```
local text = "~HyPeX~ASdasdasd"
local size = dxGetTextWidth(text,1,"default")
local tSize = 1;

if ( size < 100 ) then
while ( size < 100 ) do
tSize = tSize + 0.5;
size = dxGetTextWidth(text,tSize,"default")
end
elseif ( size > 100 ) then
while ( size > 100 ) then
tSize = tSize - 0.5;
size = dxGetTextWidth(text,tSize,"default")
end
end

outputConsole ( tSize );

```

What exactly are you trying to do?

Make the text always fit in a 100 pixel width space, having the size 1 as maximum and getting smaller if it is too big.

I'm not sure how the math wouldbe, but you could use a while loop, something like this:

```
local text = "~HyPeX~ASdasdasd"
local size = dxGetTextWidth(text,1,"default")
local tSize = 1;

if ( size < 100 ) then
while ( size < 100 ) do
tSize = tSize + 0.5;
size = dxGetTextWidth(text,tSize,"default")
end
elseif ( size > 100 ) then
while ( size > 100 ) then
tSize = tSize - 0.5;
size = dxGetTextWidth(text,tSize,"default")
end
end

outputConsole ( tSize );

```

Would that work in a clientRender loop, or is it adviced to do it before?

If you don't use onClientRender, whenever the text changes the size won't be updated.

What i was asking was if it was ideal in performance terms, i could add a check to see if the text changes and then do the size update, my question was if it was worth the work.

(But i guess not so much since its client side stuff)

The check would be better, but it probably wouldn't be noticeable.

Great, Thanks! Already tested both, didnt see any difference at all.

You won't because it's a small portion of text and the check is done once per client render. Imagine a scenario with a large string and a loop, this can be costly.

## Create an account

Register a new account