Shoutbox

Bezier curve formula in C++ - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Bezier curve formula in C++ (/showthread.php?tid=82682)

Bezier curve formula in C++ by Reaper on 03-26-2008 at 06:26 PM

I need to use a formula and output a variable in C++ but I have an issue. Im using the variable in the forumla but I want the formula to ouput the value of the variable. Sounds complicated so here is my code:

code:
#include <cmath>

#include <iostream>

int v1;

int =v2;

int vc1;

int vc2;

float t;

float ans;

using namespace std;



void main()

{

cout << "Enter v1: ";

cin >> v1;

cout << "Enter v2: ";

cin >> v2;

cout << "Enter vc1: ";

cin >> vc1;

cout << "Enter vc2: ";

cin >> vc2;

ans = (v1 * ((1-t)*(1-t)*(1-t))) + vc1 * (3 * ((1-t)*(1-t))) * t + vc2 * ( 3 * ((1-t) * (t*t)))+ (v2 * (t*t*t));

cout << "Answer = " << ans << endl;

}

Basically, the "t" in the formula is what I want. I need the output to be in terms of "t".

Its kinda hard to explain.
RE: Bezier curve formula in C++ by Adeptus on 03-26-2008 at 10:16 PM

The statement that computes your formula doesn't alter "t", so you can simply "cout << t" before or after "ans", or do whatever you want with it.  It's just a variable.

I should also note that in your code, "t" is never initialized to anything before it is used, which is almost certainly a mistake.


RE: Bezier curve formula in C++ by Choli on 03-26-2008 at 10:42 PM

I think he wants to output the result of that expression after simplifying it using symbolic calculus. That can't be done automatically in C++ (you can do it in Maple, Mathematica or Matlab). So what you have to do is: simplify it by hand, using v1, v2 and v3 as symbolic variables, and after that, susbtitute them by the numbers. ie:

(v1 * ((1-t)*(1-t)*(1-t))) + vc1 * (3 * ((1-t)*(1-t))) * t + vc2 * ( 3 * ((1-t) * (t*t)))+ (v2 * (t*t*t))

that is equal to:

(v1 * ((1-t)*(1-t)*(1-t))) + 3 * vc1 * t*(1-t)*(1-t) + 3 * vc2 * t * t * (1-t) + v2*t*t*t

so now, you do the "cout <<" thing but only with the v1, v2, vc1, vc2 variables and the rest of the expression you keep it as a string:

code:
cout << "Answer = " << v1 << " * (1 - t) * (1 - t) * (1 - t) + (" << 3 * vc1 << ") * t * (1 - t) * (1 - t) + (" << 3 * vc2 << ") * t *t * (1 - t) + (" << v2 << ") * t * t * t" << endl;

Note how i've used parenthesis in the expression around the calculated values of 3*vc2, etc... to make mathematically correct the expression in the case where vc2 is a negative number :P
RE: Bezier curve formula in C++ by mezzanine on 03-26-2008 at 11:01 PM

The cubic Bézier curve formula resolves to a cubic equation in terms of t, which is quite difficult to solve. If you're really brave, you can write the equation in depressed form and make use of the Chebyshev radicals to find the roots.


RE: Bezier curve formula in C++ by Choli on 03-26-2008 at 11:19 PM

quote:
Originally posted by mezzanine
The cubic Bézier curve formula resolves to a cubic equation in terms of t, which is quite difficult to solve. If you're really brave, you can write the equation in depressed form and make use of the Chebyshev radicals to find the roots.
but he has a cubic formula in terms of t, but he equals it to "ans" which is undefined, so he can't solve anything :P

Anyway, cubic equations do have a formula to find their roots, however it's quite complex (it uses several square and cubic roots and the imaginary number i). You can find the exact formula clicking here, typing "ax^3+bx^2+cx+d= 0" (without quotes) in the text area next to where it says "solve" and pressing the button Solve :)