我如何将一个ease in out函数从c++翻译成python ?
How would I translate an ease in out function from c++ to python?
我一直在尝试将一个函数从c++翻译到Python有一段时间了,但我不能很好地理解这个函数,无法自己翻译。
//C++
float Cubic::easeInOut(float t,float b , float c, float d) {
if ((t/=d/2) < 1) return c/2*t*t*t + b;
return c/2*((t-=2)*t*t + 2) + b;
}
//Python
def rotate(t, b, c, d):
t = t/(d/2)
if (t < 1):
return c/2*t*t*t + b
t = t-2
return c/2*((t)*t*t + 2) + b
编辑:这是我到目前为止得到的,但它没有返回一个从0.0上升到1.0的列表。以前有人用python做过这个吗? 提示:首先,简化c++
struct Cubic {
float easeInOut(float t,float b , float c, float d) {
t = t / (d/2);
if (t < 1)
return c/2*t*t*t + b;
t = t - 2;
return c/2*(t*t*t + 2) + b;
}
}
如果你不知道如何把翻译成python,那么你需要学习更多的python。我可以把它翻译成python,我甚至不知道 python 。
实际上,既然你已经发布了你的python,并且你声称它是错误的,我突然想到python中的所有数字都是(可能,我在这里猜测)双精度,这意味着每次你除以它都会与c++略有不同。快速浏览一下Python文档,上面写着"The / (division) and // (floor division) operators yield the quotient of their arguments.
",所以如果你想让它像c++一样工作,显然你应该使用//
。
如果将所有的数字常量(例如2)替换为它们的等效浮点常量(例如2.0),会有帮助吗?
def rotate(t, b, c, d):
t = t/(d/2.0)
if t < 1.0:
return c/2.0*t*t*t + b
t = t-2.0
return c/2.0*((t)*t*t + 2.0) + b
您的代码是一个正确的翻译,但它不打算返回一个列表。这个缓和函数返回给定时间(t)的单个缓和时间值。它被多次调用,其t值从0到d变化,并以平滑(非线性)的方式返回从b到b+c变化的结果。
你想让返回值从0到1,所以你应该用b=0.0和c=1.0来调用它。d值应该设置为要缓过的持续时间。
要获得从0到1的简化值列表,对于从0到10的t,您可以这样做:
[rotate(t,0.0,1.0,10.0) for t in range(11)]
结果:[0.0, 0.004000000000000001, 0.03200000000000001, 0.108, 0.25600000000000006, 0.5, 0.744, 0.8919999999999999, 0.968, 0.996, 1.0]
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 对函数的 out 字符串参数使用 swap 与赋值
- 在 Metal 着色器代码中,如何定义函数的 in/out 参数变量?
- C++ 调用析构函数后动态模板队列"double free or corruption (out)"
- 在析构函数中调用"delete"运算符时"compiler is out of heap space"编译器错误
- GLSL 错误:"out"限定符仅对 GLSL 1.10 中的函数参数有效
- 包装一个使用char**[in/out]的C函数调用,以在cython中返回一个python列表
- 将 MATLAB 函数编译成可以在 linux 终端上运行的东西,w.out MATLAB
- SFINAEd-out 函数是否隐藏了从基类显式导入的重载
- 将向量传递到函数时出现"vector subscript out of range"错误:c++
- 如何使用"char *filename"在构造函数中初始化"ostream &out"
- c++中的System.out.println函数语法
- 我如何将一个ease in out函数从c++翻译成python ?
- c++双精度释放或损坏(out):即使使用复制构造函数和赋值操作符
- 使用使用C风格字符串作为OUT参数的WinAPI函数
- 将模板成员函数的专门化定义(带OUT默认体)放在源文件中是否安全
- 将 [out] 数组传递给C++函数
- 析构函数给出我不明白的输出("./a.out"中的错误:双重释放或损坏(快速顶部):)