Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling values in PHP

Tags:

php

math

scale

I cant get around on how to express this is PHP.

I have 100 and 420 as my min and max value that I wish to obtain.

Than lets suppose I have:

1000
4534
34566
123145
12312265

Now, how can I say:

Take 4534 and knowing that 1000 = 420 and 12312265 = 100 determine 4534 value.

To make it more clear, Im trying to represent web page ranks with squares, so if the rank is 1 it should be translated into my maximum value/size 420, however if the page ranks low on popularity, say 13000 then its size should be close to the minimum 100. I know all the values.

Thank you.

Im still having trouble figuring this out.

So far using the code from the first answer I have:

$srcmin=1185;
$srcmax=25791525;

$destmin=100;
$destmax=420;

$pos = (($RANK - $srcmin) / ($srcmax-$srcmin)) ;
$rescaled = ($pos * ($destmax-$destmin)) + $destmin;*/

$percentage = (($RANK - $MIN) * 100) / $MAX;
$SIZE = (($percentage / 320) * 100) + 100

Being $RANK my values for the ranks of web pages and $SIZE the value that I need to size them accordingly. This does not work (my mistake no doubt) all I get from $SIZE is 100.

like image 775
Marvin Avatar asked Jun 28 '10 23:06

Marvin


Video Answer


1 Answers

This should illustrate....

$values=array(1000, 4534, 34566, 123145, 12312265);
$srcmin=$values[0];
$srcmax=$values[count($values)-1];

$destmin=420;
$destmax=100;

foreach($values as $x)
{
     //how far in the source range is $x (0..1)
     $pos = (($x - $srcmin) / ($srcmax-$srcmin)) 

     //figure out where that puts us in the destination range
     $rescaled = ($pos * ($destmax-$destmin)) + $destmin;
}

You want to know how far through the source range each number is, that's what the $pos value gives you. Given that, you can translate that into how far through the destination range you are.

like image 151
Paul Dixon Avatar answered Sep 27 '22 21:09

Paul Dixon