[英]percentage function with positive or negative numbers
虽然看起来很简单,但我在这里找不到这个问题。
当我知道开始和结束位置时,我想说我想计算总距离的百分比,即:
function getPercentage(startpos,endpos,currentpos)
{
var distance = endpos-startpos;
return (currentpos/distance)*100;
}
很容易,但是如果起始点或当前位置在负轴上,那么咖啡不起作用已经很晚了
startpos = -734;
endpos = 65;
currentpos = -38;
值得注意的是,任何位置在任何一点都可以是正轴或负轴。
距离的当前位置的百分比是多少....任何人都可以扔我一块骨头,它是凌晨1点:-)
你也忘了翻译当前的位置。
function getPercentage(startpos, endpos, currentpos)
{
var distance = endpos - startpos;
var displacement = currentpos - startpos;
return (displacement / distance) * 100;
}
很容易做的是抵消位置,使startpos变为零:
endpos = endpos - startpos;
currentpos = currentpos - startpos;
var perc = currentpos/endpos*100;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.