gpt4 book ai didi

processing - Processing 中的 map() 函数是如何工作的?

转载 作者:行者123 更新时间:2023-12-03 23:21:55 27 4
gpt4 key购买 nike

我正在上一门使用处理的类(class)。
我在理解 map() 函数时遇到问题。
根据它的文档( http://www.processing.org/reference/map_.html ):

Re-maps a number from one range to another.

In the first example above, the number 25 is converted from a value in the range of 0 to 100 into a value that ranges from the left edge of the window (0) to the right edge(width).

As shown in the second example, numbers outside of the range are not clamped to the minimum and maximum parameters values, because out-of-range values are often intentional and useful.


是否类似于随机函数但范围由用户设置?另外,我无法理解第一个示例的解释:它说数字被转换为 0 到 100 的值,转换为从屏幕边缘到边缘的值。我在想为什么不直接将数字 25 转换为与屏幕有关的值范围?

最佳答案

map() function 是一个有用的捷径,您不会后悔花时间理解它。
这是它的语法:

variable2 = map(variable1, min1, max1, min2, max2);

该函数在之间建立一个比例两个范围的值 :

最小 1 : 最小 2 = 最大 1 : 最大 2

您可以将其读作:min1 是到 min2 max1 是到 最大2。
variable1 存储第一个范围 之间的值最小1~最大1。
variable2 获取第二个范围 之间的值min2~max2。

这是函数为程序员求解的方程:

variable2 = min2+(max2-min2)*((variable1-min1)/(max1-min1))

这是处理 map() 函数背后的 Java 代码:

static public final float map(float value, 
float istart,
float istop,
float ostart,
float ostop) {
return ostart + (ostop - ostart) * ((value - istart) / (istop - istart));
}

关于processing - Processing 中的 map() 函数是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com