What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Bezier curve formula in C++

Bezier curve formula in C++
Author: Message:
Reaper
Veteran Member
*****

Avatar

Posts: 1393
Reputation: 23
35 / Male / Flag
Joined: Jun 2004
O.P. Bezier curve formula in C++
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.
03-26-2008 06:26 PM
Profile E-Mail PM Find Quote Report
Adeptus
Senior Member
****


Posts: 732
Reputation: 40
Joined: Oct 2005
RE: Bezier curve formula in C++
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.
03-26-2008 10:16 PM
Profile E-Mail PM Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Bezier curve formula in C++
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
Messenger Plus! en espańol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
03-26-2008 10:42 PM
Profile PM Find Quote Report
mezzanine
Full Member
***

Avatar

Posts: 106
Reputation: 23
– / – / Flag
Joined: May 2005
Status: Away
RE: Bezier curve formula in C++
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.

This post was edited on 03-26-2008 at 11:03 PM by mezzanine.
03-26-2008 11:01 PM
Profile PM Web Find Quote Report
Choli
Elite Member
*****

Avatar
Choli

Posts: 4714
Reputation: 42
42 / Male / Flag
Joined: Jan 2003
RE: Bezier curve formula in C++
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 :)
Messenger Plus! en espańol:
<< http://www.msgpluslive.es/ >>
<< http://foro.msgpluslive.es/ >>
:plus4:
03-26-2008 11:19 PM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On