php - Alternating Algorithm Starting from Center -
i'm trying figure out algorithm come arrayed result equidistant alternating points center (can percent based). so, end result like: x = 20 (separation distance based on # of items) - 100 / 5 (5 items spread out on 100%)
- a = 50 (center point)
- b = 70 (a + 20)
- c = 30 (a - 20)
- d = 90 (b + 20)
- e = 10 (c - 20)
another result if have 10 items (x = 100 / 10):
- a = 50 (center point)
- b = 60 (a + 10)
- c = 40 (a - 10)
- d = 70 (b + 10)
- e = 30 (c - 10)
- f = 80 (d + 10)
- g = 20 (e - 10)
- h = 90 (f + 10)
- i = 10 (g - 10)
- j = 100 (h + 10)
if it's important, i'm trying arrive @ algorithm using php. i'm not of math wiz i'm not sure if there's name type of calculation. thanks!
you write function like:
function getarrayequidistant($startvalue, $step, $nbentries) { $i = 0; $count = 1; $final = array(); $final[] = $startvalue; while($i < $nbentries) { if ($i % 2 == 0) $final[] = $startvalue + ($count * $step); else { $final[] = $startvalue + ($count * $step); $count++; } $i++; } retun $final; }
where $startvalue
initial value (at index 0), $step
value added or sub @ each iteration, , $nbentries
number of entries after initial value.
as example:
print_r(getarrayequidistant(50, 20, 10));
will give you:
array ( [0] => 50 [1] => 70 [2] => 30 [3] => 90 [4] => 10 [5] => 110 [6] => -10 [7] => 130 [8] => -30 [9] => 150 [10] => -50 )
Comments
Post a Comment