Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#26 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-28 15:52:56

Thank you for being this patient with me, Bob, but I think with my explanations I put you somewhat on the wrong track. Probably due to my talking about a 'Width to Height' ratio. This way I have been taking up way too much of your time than I intended. So please, if you get fed up with me, just say so.

As I understand your last explanation yields a 'character width', while I am searching for a 'character height'.  In CSS 'font-size' is a height value, while the width of a character is determined by the used font, as-in 'beyond my control'.

The new version Version 2.2 uses your math and shows the values calculated, but when you select  all text ([Ctrl-A], click selected text to unselect) you will see that all boxes have room to spare due to

being a width value (at least, if I read your explanation right).

Initially I used the following math:
CSS 'font-size' = fontHeight = sqrt((finalBoxWidth * finalBoxHeight) / (fullTextWidth + avgCharacterWidth))
or following your explanation:

where I use


to get some space left and right of the text. This math yields somewhat larger font sizes.

I am stuck, again.

#27 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-27 02:39:12

Seems to be the same sun here today, finally. Much rain the last few months, dang cold. And currently, of course, eerily quiet everywhere in the streets with the voluntary Corona house arrest here in NL. Semi lockdown until April 6th, some areas  until June 1st even. Where in the world are u located?

1) I don't understand how to use the equation in your previous answer. Could you please elaborate and use 'names' like I did? Silly enough does my brain really shut down when single letters are used. Always did, ever since High School (40 something years ago).

2) I asked to 'verify' the logic, but that got lost in translation, I meant to 'confirm' my logical reasoning (very close meaning in NL). Sorry for that.

3) V2.1 seemed ok, but still has a few 'hidden features'.

4) 'Lorem Ipsum' won't translate to anything, it is just page filler, but here's another credo I use all the time (when creating database apps): 'everything has an one-to-many relation, unless it's not...'

How about this one: w8b4udo: who-what-where-when-which-whos-with-why => how (advise to junior (and some advanced) programmers)

#28 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-26 15:49:51

Hi Bob,

I think I have figured it out. Could you please verify the below logic:

box: Width x Height => 200 x 200 pixels => 40000 pixels to spare
font: height 1px monospace
string: 154 characters: 'Lorem ipsum dolor sit amet, exerci dolorem est ad. Sumo rebum prompta vim ad. Legendos expetendis id sed. Ex ius quem accusamus, pri et deleniti copiosae.'

After measuring
string width: 153.44 pixels wide, 1px high (consumes only 153.44 pixels of 40000)

The whole point is that, although (generally speaking) the string wraps inside the box, you can treat it as a box itself of a specific wxh.

So a string of

1px high = 1 * 1 * 153.44 = 1h * 153.44w
2px high = 2 * 2 * 153.44 = 2h * 306.88w
3px high = 3 * 3 * 153.44 = 3h * 460.32w

etc.

or fontHeight^2 * stringWidth = boxWidth * boxHeight
=> fontHeight^2 = boxWidth * boxHeight / stringWidth
=> fontHeight = sqrt(boxWidth * boxHeight / stringWidth)

So for the example string fontHeight = sqrt(200 * 200 / 153.44) = sqrt(40000 / 153.44) = sqrt(260,6882..) =  ~16.1458 pixels

When you look at the multi colored 'Lorum Ipsum' box in version 2.1, which has a font-size of 13px (on my display of 1920x1200 pixels), you can see that there would probably be enough room for a font of ~16 pixels high as it has an almost empty row and some whitespace around the text. (haven't tested this yet, but looks promissing).

This would be 'the rule', still requires anticipating the exceptions...

Your thoughts on this??

#29 Re: Help Me ! » Probability of exhuming a female. » 2020-03-26 01:28:22

Wadda ya know, scored myself another F.  Hope you've got glue, Fruityloop....

#30 Re: Help Me ! » Probability of exhuming a female. » 2020-03-25 15:30:10

Purely on intuition: the chance that you'll find a woman is twice the probability you find a man: two graves => might be 2 women. So 2 women and 1 man would yield 2/3 chance you will find a woman. This is because you know that there will at least be a woman in one of the graves. Without that knowledge it would be 50/50.

Shred that book...

#31 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-25 02:26:32

Huh? Are u using IE11 by any chance? Codepen is particular about IE11 (just saw this). Using Firefox myself, code runs perfectly fine in Firefox, Chrome and Edge (all latest versions). Dunno about Apple devices. Haven't changed a bit (online) since y'day...

UPDATE 1, created a live version in my Github pages Version 2.1, for the code itself go to my Github mathisfun folder. This version runs in IE11, Chrome, Edge and Firefox.

UPDATE 2, found this on Wikipedia, Line wrap and word wrap: Algorithm.

Otherwise:

This is what U should see:

forum-so2.png
(you should see an image if not go here example)

#32 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-24 13:33:59

Hi Bob,

Here a link to the current snapshot of the code I am working on on Codepen.

This is the version I am using without your last suggestions. It looks ok, but it is far from. Slightly different text but everything resizes with the browser.

I decided to go for 'W' or 'M' as you suggested (depending on the font one of them will be the widest character). The math for smaller squares inside a large square side small squares = RoundUp(SquareRoot(character count)) seems to work, circle too inner square  = outer square side / SquareRoot(2). Although I need to address some measuring bugs.

Regarding soft line breaks and soft hyphens, you are absolutely right. And there are boat loads of other troubles ahead, I'm sure (2 character Unicode, diacritics, Asian fonts, Arabic, reading/writing directions, to name a few). But they all require the base math to be correct. Check my profile again and you will see my real challenge.

My credo for many years: design the rule and anticipate the exceptions (in this instance ...later... ).

Regarding the Codepen, I dunno if you are into programming HTML/CSS/JS, but the code is simple enough and I put in loads of comment explaining what I am doing, why and doubts (TODO). You might enjoy to see me using y=mx+b to get responsive sizes for: page main font, main left/right margin and the scaling of the main circles/squares. Works like a charm time and again. And I learned to use it from 'mathisfun.com'. Oddly enough, hardly ever used by other developers, I kid you not.

Function resizeBoxes() does all the hard work.

If you want squares instead of circles, simply change javascript var settings = { circle: true }; to false (just before function resizeBoxes(), line 134).
For a border around the circles change CSS .box{.... border: 0px solid Crimson; ....} (line 55) to something larger than '0px'. Codepen will refresh the page as you go.

Drag the side of the main window or resize your browser to see the magic happen.

The next version will have the changes you requested, so we can have a look how that pans out...

#33 Re: Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-22 03:21:30

Hi Bob,

Thank you for putting this little puzzle on your to do list, highly appreciated!

Your first question

If you use any 'average' character width (or height) won't there be a problem of fit if the text has too many bigger than average characters?

actually implies that I should use method 2 to get an average H:W ratio as text total width / character count will yield just that. Assigning a new character height to a font will make all characters scale to proportion automatically.

Should I go for the biggest character (usually the 'W') then the parent box will not be filled optimally as the final font size will probably turn out (somewhat) too small.

Padding out the text with spaces would be stricktly prohibit as I don't know what the text is and what it is used for. Furthermore, developers using this can change/set text alignment themselves (left,right,centered and justified).

However, I too think the final solution is simple, but all the values simply make my head hurt and obfuscate the underlying problem. So, lets forget about fonts for a moment as the problem might just as well have regarded cutting a large piece of cardboard into smaller rectangles:

I have a 2mx3m piece of cardboard which I need to cut it into 90 smaller pieces. The pieces must be equal and have a height to width ratio of 1.6:1. What is the optimal height of a smaller piece yielding the least amount of waste? Rotation of the smaller pieces in not allowed (would look rather funny when using fonts, extra option in version 2 maybe?). Looks like a cutlist problem to me, now that I wrote this, same math (I think), different use...

FYI: as stated before, this will be used to post correct code on Stackoverflow, not to earn big bucks. After that, the final optimized code (Javascript with HTML and CSS examples) will be posted in Public Domain (Github) for others to use freely.


EXTRA
For testing I use the following lines of text:

0
0000
1234 5678
iwiw IWIW IWIWIW iwiwiwiw
Lorem ipsum dolor sit amet, exerci dolorem est ad. Sumo rebum prompta vim ad. Legendos expetendis id sed. Ex ius quem accusamus, pri et deleniti copiosae.


Below you can see the test output I am using to check my math (not really up to par right now...). The fully colored circles don't use inner squares, the others do. The green border depicts the limit.

forum-so.png
(you should see an image if not go here examples)

I really was afraid I'd give to much info and cause you an info overload as well.

#34 Help Me ! » How to fill a rectangle with N smaller rectangles with given H:W ratio » 2020-03-21 13:40:33

renevanderlende
Replies: 18

Hey everyone,

I've been staring at this for over a week now and I cannot wrap my head around the math involved, probably due to an overload of info combined with a 'math-block'. I will use your answer/suggestions to improve the code in my answer on Stackoverflow question 60610396: Resize text within Circle..., which for now works, but seems flawed and incomplete.

As the subject says, (I think) I am looking for an equation to fill a large rectangle of width=X and height=Y with N smaller rectangles which have a given height to width ratio. Catch: the final result must be the height of a small rectangle.

I probably posted much more info than you actually need, but I want to make sure you understand what I am working on...


Background:

A text must fit on a large button which is either a square, rectangle or circle. The text can be 1 to N characters long and must be scaled to fit with minimal space left, possibly wrapping over multple lines. The only way to scale the characters is assigning the proper font height. The point of this is that when a user resizes the browser, the text must be dynamically scaled to proportion.

The widths and heights of the 3 variations are given, for the circle the inner square has to be used. To get the sides of the inner square of the circle I use:

1) diameter / sqrt(2) ( => a circle with d = 233 has an inner square with sides of 233 / 1.41421... = 164, rounded down to keep final text within inner square).

To fill the square of 164x164 with 100 characters with a H:W ratio of 1:1, the height of a character would be:

2) inner square side / sqrt(number of chars) ( => 164 / sqrt(100) = 16.4 )

This math is rather straighforward and will optimally fit 100 characters (squares) of 16.4x16.4 in a large square of 164x164. In this case a perfect fit.
Alas, non-proportional fonts don't have square characters and the widths vary per character, per font.

To get the final result I will have to use the average character width  combined with the height to width ratio of that avg character:

3) AVG character width method 1: width of character '0' (zero) of given font (derived).

4) AVG character width method 2:  total width of the text / the number of characters (both derived values).

5) AVG height to width ratio: character height/ avg character width.
E.g. method 1: 'monospace': height 16 (given), character '0': width 9.6.... This will yield an average height/width ratio of 16/9.6 = 1.67 (rounded) for this font. (Note that 'monospace' is a proportional font, so in this case all character are 9.6 wide).

E.g. method 2: 'monospace' text width = 1552.6..., number of characters = 162, character width = 1552.6 / 162 = 9.58... at given height 16, slightly less than method 1, but also 1.67 (16/9.58 rounded). The text width is the summed width of all characters, which seems more accurate (as a series of 'i' is less wide than a series of 'w', while the number of characters would be the same).

(note: pls ignore rounding errors, in the final code I will only round the calculated end result)

Statistically more correct would be to count all occurences of the various unique characters in the text and calculate a weighed average width. Would be fine for text with <10 characters, but getting the weighed avg of large paragraphs would take up way too much CPU time.

And here is where I get stuck: I cannot figure out how to connect all the width information to a final font height, especially when the text wraps over several rows.

Can you please help me with the math or point me to some site where I can get further info? I also would like to know whether to choose method 1 or 2 and if my height to width ratio has the correct logic.

Thank you very much!
~

#35 Re: Help Me ! » using result of linear equation to find number of columns » 2020-02-13 11:13:56

Hi Bob,

Thanks a bunch! Nice clarification, now that I see the solution it seems so obvious. Been staring at the puzzle for hours, but no lightbulbs came on flashing.

In the mean time I added the line to my tool (new link, purple line) and noticed that for every 20x there is a 2y skip starting at (0,48) then (20,50), (40,52), (60,54) and so on. Knowing this, is there an alternate way to calculate 'column-count' and 'column-width' instead of '0.1x + 48'? (Yes, I genuinely  s u c k  at this!)

BTW x is always known (100vw, current browser width in pixels)

Arghh:  column-count = 100vw / (0.1 * 100vw + 48) = 8 for x=1920 => 1920 / (192 + 48) = 8

Sorry, never mind the above question, I am caught in a bit of cyclic reasoning....ing....ing....ing

#36 Help Me ! » using result of linear equation to find number of columns » 2020-02-13 03:08:16

renevanderlende
Replies: 2

Hi everyone,

This is not a request for coding help, but a genuine math problem used for:

Writing a tutorial for Codepen (non commercial CSS/HTML developers site) on using 'columns' in CSS.
It shows how to one can use "y = mx + b" to solve a variety of variable size, width and height problems,
saving time and amount of code.

I am using "y=mx+b" for points p1(320,80) and p2(1920,240) => y = 0.1x + 48, meaning:
at 320 we have column width 80 (4 columns)
at 1920 we have column width 240 (8 columns)

So, in this case "y = 0.1x + 48" yields a column width at any given screen width.

X-values 320 and 1920 are given
Y-values 80 and 240 were derived by dividing 320 and 1920 by 4 and 8 respectively.

Given result '0.1x + 48', how do I find the exact column width for 5, 6 and 7 columns
without drawing a xy-graph and find the answer visually (like with a little tool I created on
GeoGebra ERS linear equations (ERS - easy responsiveness series)

In CSS this would look something like:

   column-width: calc(0.1 * 100vw + 48px); /* 100vw = 100% of current viewport width */

Unfortunately in CSS it is not possible to substitute 'column-width' like:

   column-count: calc(column-width / some-equation);

And calc() only accepts +,-,*,/ as math operators


Thanks for your help!!

#37 Introductions » Math illiterate says hi... » 2020-01-06 04:19:02

renevanderlende
Replies: 2

Hey everyone,

I am an ol'skool software developer (ol' as in 1653 AD) from NL who came accross 'mathisfun' when looking for a simple explanation of y=mx+b.

You have to know that I was terrible with math at school and somehow never grasped the scribbles my teachers wrote on the blackboard (what on earth is that?), which they called 'algebra' or 'trigonometry' or something along those lines. (Imagine how blissfully empty my head must be...)

It took me about 2 months to understand (scratch that, instead: learned to use the formula without comprehending) the 'definition of a straight line'.

Now I use y=mx+b on a daily basis in my CSS for the webpages I create. Each time it works like a charm, if only I knew its purpose....

So, now you know.

Cheers!!

#38 Re: Introductions » Hello there » 2020-01-06 03:58:49

Has anyone translated this post already??

It is spam: an advert about 'congested septic tanks' by 'Avico septic tank company'

Board footer

Powered by FluxBB