Java 在其他语言中,map()的等价物是什么?
在(一种基于java的语言)中,有一个非常方便的函数,名为Java 在其他语言中,map()的等价物是什么?,java,processing,Java,Processing,在(一种基于java的语言)中,有一个非常方便的函数,名为map(),它接受5个参数。它将一个数字从一个范围重新映射到另一个范围。请查看官方文档以了解详细信息 我很难在其他语言中找到它的等价物,特别是在java中。还是制作我自己的函数的最佳解决方案?这是一个简单的计算: static double map(double value, double start1, double stop1, double start2, double stop2) { return (value - st
map()
,它接受5个参数。它将一个数字从一个范围重新映射到另一个范围。请查看官方文档以了解详细信息
我很难在其他语言中找到它的等价物,特别是在java中。还是制作我自己的函数的最佳解决方案?这是一个简单的计算:
static double map(double value, double start1, double stop1, double start2, double stop2) {
return (value - start1) / (stop1 - start1) * (stop2 - start2) + start2;
}
这只是插入一个值,相当于(以下是处理参数名称):
这是相当琐碎的,实际上你把你的值转换成
[0.0,1.0]
中的一个值,它告诉你离开始或停止有多远(0.0
在start1,1.0
在stop1),然后你把它转换成与另一个间隔成比例的值。处理是开源的,因此,可以通过查看GitHub上的源代码来回答这些问题
具体来说,是直接指向map()
函数的链接,如下所示:
static public final float map(float value,
float start1, float stop1,
float start2, float stop2) {
float outgoing =
start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
String badness = null;
if (outgoing != outgoing) {
badness = "NaN (not a number)";
} else if (outgoing == Float.NEGATIVE_INFINITY ||
outgoing == Float.POSITIVE_INFINITY) {
badness = "infinity";
}
if (badness != null) {
final String msg =
String.format("map(%s, %s, %s, %s, %s) called, which returns %s",
nf(value), nf(start1), nf(stop1),
nf(start2), nf(stop2), badness);
PGraphics.showWarning(msg);
}
return outgoing;
}
static public final float map(float value,
float start1, float stop1,
float start2, float stop2) {
float outgoing =
start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
String badness = null;
if (outgoing != outgoing) {
badness = "NaN (not a number)";
} else if (outgoing == Float.NEGATIVE_INFINITY ||
outgoing == Float.POSITIVE_INFINITY) {
badness = "infinity";
}
if (badness != null) {
final String msg =
String.format("map(%s, %s, %s, %s, %s) called, which returns %s",
nf(value), nf(start1), nf(stop1),
nf(start2), nf(stop2), badness);
PGraphics.showWarning(msg);
}
return outgoing;
}