fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. float cubicBezier(float x2, float y2, float x3, float y3, float t)
  5. {
  6. if(t > 1)t = 1;
  7. else if (t < 0)t = 0;
  8. if(x2 > 1)x2 = 1;
  9. else if (x2 < 0)x2 = 0;
  10. if(y2 > 1)y2 = 1;
  11. else if (y2 < 0)y2 = 0;
  12. if(x3 > 1)x3 = 1;
  13. else if (x3 < 0)x3 = 0;
  14. if(y3 > 1)y3 = 1;
  15. else if (y3 < 0)y3 = 0;
  16. float x1, y1 = 0;
  17. float x4, y4 = 1;
  18. std::cout << "x: " << ((x1*(pow ((1-t), 3.0)))+(3*x2*t*(pow ((1-t), 2.0)))+(3*x3*(pow (t, 2.0))*(1-t))+(x4*(pow ((t), 3.0)))) << std::endl;
  19. std::cout << "y: " << ((y1*(pow ((1-t), 3.0)))+(3*y2*t*(pow ((1-t), 2.0)))+(3*y3*(pow (t, 2.0))*(1-t))+(y4*(pow ((t), 3.0)))) << std::endl;
  20. std::cout << "t: " << t << std::endl;
  21. }
  22. int main()
  23. {
  24. for(float t = 0; t <= 1.1; t+=0.1)
  25. cubicBezier(0, 0, 1, 1, t);
  26. return 0;
  27. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
x: 0
y: 0
t: 0
x: 0.027
y: 0.028
t: 0.1
x: 0.096
y: 0.104
t: 0.2
x: 0.189
y: 0.216
t: 0.3
x: 0.288
y: 0.352
t: 0.4
x: 0.375
y: 0.5
t: 0.5
x: 0.432
y: 0.648
t: 0.6
x: 0.441
y: 0.784
t: 0.7
x: 0.384
y: 0.896
t: 0.8
x: 0.243
y: 0.972
t: 0.9
x: 0
y: 1
t: 1