Sarcoma - 10 months ago 26

PHP Question

I have built a simple modular scale calculator, where I can enter a base number (say font size or line height) and an important number (maybe column width, page width, or another font size) and select a ratio (golden ratio for example) and the calculator will display a double stranded scale for use in page layout. *see example below*

I have been toying with the idea of allowing users to input points and picas and then displaying the scale in one or the other.

The problem is that picas are base 12 numbers (12 points to a pica), I figured if I could just convert the input (something like 16p6) to base 12 I could do the calculation and go from there.

I just can't work out how to do basic calculations in another base. I'm really just messing around to see what I can come up with, let me know if you think I'm barking up the wrong tree.

**So my question is this how do I do calculations in base 12?**

`<?php`

// basic modular scale calculation

$goldenRatio = 1.618;

$baseNumber = 16;

$i = 0;

while ($i <= 10) {

echo round($baseNumber,1). "<br>";

$baseNumber = $baseNumber * $goldenRatio;

$i++;

}

echo "<hr><br>";

// Attempt at base 12 calculation

$a=base_convert(16,10,12);

$b=base_convert(12,10,12);

$r = ($a*$b);

echo $a."*".$b."=";

echo $r;

I'm really just messing around to se what I can come up with, let me know if you think I'm barking up the wrong tree.

To solve the problem of converting Picas to base points from a string like

`'12p6'`

`function isPica($data) {`

if (preg_match('/^[0-9]+(?i)p([0-1]?[0-9])?$/i',$data)) {

return true;

}

return false;

}

function makePoints($data) {

$data = preg_replace('/^([0-9]+)((?i)p)(([0-1]?[0-9])?)$/i','$1.$3',$data);

$data = explode('.',$data);

$points = floor($data[0] * 12);

$points = $data[1] + $points;

return $points;

}

Modular Scale Calculator

Git Hub — Modular Scale Calculator

Answer

base_convert just converts the string representation. You can't do calculations using strings of numbers in base 12 in php. When dealing with imperial units, you usually have multiple "bases" to deal with. So it has to be done manually. When you're doing calculations, the base doesn't matter.

- Convert all the different units to the smallest one (points). $a = 3*12 + 7;//3picas, 7points.
- Do the calculations.
Convert back to original units.

$points = (int)$val % 12; $picas = (int)($val / 12);

or

```
$picas = floor($val / 12);
$points = $val - 12*$picas;
```