2012-11-28 32 views
0

首先,让我告诫你,我的数学知识是相当有限的(因此我来这里问这个问题)。最大值到0到最大值算法

这是一个愚蠢的例子,但我基本想要的是有一个可变数量的行,并能够设置一个最大值,然后对每个渐进行减少该值,直到在这一点开始增加值的一半再次回到最后一排。

为了说明这一点......鉴于:maxValue = 20,并rows = 5,我需要能够得到为行值以下值(是的,甚至是0):

row 1: 20 
row 2: 10 
row 3: 0 
row 4: 10 
row 5: 20 

有虽然限制,因为我想在使用SASS的Compass中这样做。有关可用操作,请参阅here,但为了给您提供它的要点,只有基本操作可用。

我可以循环遍历行,所以只需要对系列中每个单独行都有效的计算。这是一种循环的,我可以使用:

$maxValue:20; 
$rows:5; 

@for $i from 1 through $rows { 
    // calculation here. 
} 
+0

Sass确实有一个底层函数:http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#floor-instance_method – cimmanon

+0

受过教育!谢谢:) – Remy

回答

2

我还没有真正与上海社会科学院工作过,但尝试这样的事情用一个基本的,如果和地板的功能,不知道是否会工作

// set various variables 
$maxValue:20; 
$rows:5; 
// get row number before middle row, this instance it will be 2 
$middleRow = floor($maxValue/$rows) 
// get increment amount, this instance should be 10 
$decreaseValue = (max/floor(rows/2)) 

@for $i from 0 through $rows - 1 { 

    @if $i <= $middleRow { 

     ($maxValue- ($decreaseValue * $i)) 

    } 

    @else{ 

    // times by -1 to make positive value 
     ($maxValue - ($decreaseValue * -$i)) * -1 

    } 
} 

我已经测试了上述js(相对语法),并且它产生了所需的结果(jsBin example)。希望这可以帮助

+0

工作就像一个魅力!太感谢了! – Remy