Using Polynomial equations in C++
In this C++ polynomial equation tutorial, you'll learn how to model and animate a moving object along a cubic curve using mathematical formulas. This hands-on project demonstrates how to solve polynomial equations and translate them into visual motion-ideal for senior secondary students learning both math and coding.
Understanding Polynomial and Cubic Equations | Maths Explanation for C++ Kids
                        A polynomial equation expresses a relationship involving powers of a variable. 
                        For a cubic curve, the general form is:
                        y = ax3 + bx2 + cx + d;
                        Here, a, b, c, and d are constants. Every third-degree polynomial equation 
                        has both a maximum and a minimum point. 
                        These turning points are useful in generating smooth motion when graphing or animating curves with C++.
                    
Deriving the Equation of a Cubic Curve | Maths Explanation for C++ Kids
To generate a cubic equation, all we will need are the maximum and minimum points of the curve.
                        y = ax3 + bx2 + cx + d  ----- (eqn 0)
                    
By differentiating y = ax³ + bx² + cx + d, we get dy/dx = 3ax² + 2bx + c. Setting the derivative equal to zero at both the maximum and minimum points allows us to calculate a, b, c, and d.
                            dy/dx = yI = 3ax2 + 2bx + c
                            At maximum point, yI = 0
                            yI|(x = xmax) = 0
                            3axmax2 + 2bxmax + c = 0  ----- (eqn 1)
                            At minimum point, yI = 0
                            yI|(x = xmin) = 0  ----- (eqn 2)
                            3axmin2 + 2bxmin + c = 0
                            Subtracting both derived equations
                            yI|(x = xmax) -
                            yI|(x = xmin)
                            ⇒
                            3a(xmax2 - xmin2) 
                            + 2b(xmax - xmin) = 0
                            2b(xmax - xmin) = 
                            -3a(xmax2 - xmin2)
                        
| b = | -3a(xmax - xmin)(xmax + xmin) | 
| 2(xmax - xmin) | 
                            b = -3/2a(xmax + xmin)
                            
                            Substituting b in (eqn 1)
                            3axmax2 + 2bxmax + c = 0
                            3axmax2 + 
                            2(-3a/2)(xmax + xmin)xmax 
                            + c = 0
                            3axmax2 - 
                            3axmax(xmax + xmin)
                            + c = 0
                            3axmax2 - 3axmax2
                            - 3axmaxxmin + c = 0
                            c = 3axmaxxmin
                            
                            From the general equation(eqn 0)
                            y = ax3 + bx2 + cx + d
                            ymax = axmax3 + 
                            bxmax2 + cxmax + d
                            
                            Substituting for b & c
                            ⇒ ymax = axmax3 - 
                            3/2a(xmax + xmin)xmax2
                            + 3axmaxxminxmax + d
                            
                            ymax = axmax3 - 
                            3/2axmax3 - 
                            3/2axmax2xmin
                            + 3axmax2xmin + d
                            
                            ymax = 1/2[2axmax3
                            - 3axmax3 - 3axmax2xmin
                            + 6axmax2xmin + 2d]
                            
                            ymax = 1/2[
                            -axmax3 +
                            3axmax2xmin + 2d]
                            
                            2ymax = -a(xmax - 
                            3axmin)xmax2 + 2d
                            
                            2d = 2ymax + a(xmax - 
                            3axmin)xmax2 
                            
                            d = ymax + a/2(xmax - 
                                3axmin)xmax2 
                            
                            
                            
                            From the general equation(eqn 0)
                            y = ax3 + bx2 + cx + d
                            
                            ymax = axmax3 + 
                            bxmax2 + cxmax + d
                            
                            ymin = axmin3 + 
                            bxmin2 + cxmin + d
                            
                            Subtracting both derived equations
                            ymax - ymin =
                            a(xmax3 - xmin3)
                            + b(xmax2 - xmin2)
                            + c(xmax - xmin)
                            
                            ymax - ymin = 
                            (xmax - xmin)[a(xmax2 
                            + xmaxxmin + xmin2)
                            + b(xmax + xmin) + c]
                            
                            Substituting for b & c
                            
                            ymax - ymin = 
                            (xmax - xmin)[a(xmax2 
                            + xmaxxmin + xmin2)
                            - 3a/2(xmax + xmin)2
                            + 3axmaxxmin]
                            
                            ymax - ymin = 
                            a(xmax - xmin)[xmax2 
                            + xmaxxmin + xmin2
                            - 3/2(xmax2 + 
                            2xmaxxmin + xmin2)
                            + 3xmaxxmin]
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)[2xmax2 
                            + 2xmaxxmin + 2xmin2
                            - 3(xmax2 + 2xmaxxmin 
                            + xmin2) + 6xmaxxmin]
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)(2xmax2 
                            + 2xmaxxmin + 2xmin2
                            - 3xmax2 - 6xmaxxmin 
                            - 6xmin2 + 6xmaxxmin)
                            
                            2(ymax - ymin) = 
                            a(xmax - xmin)(-xmax2 
                            + 2xmaxxmin - xmin2)
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)(xmax2 
                            - 2xmaxxmin + xmin2)
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)(xmax 
                            -  xmin)2
                            
                            2(ymax - ymin) = 
                            -a(xmax - xmin)3
                            
                        
| a = | -2(ymax - ymin) | 
| (xmax - xmin)3 | 
                        b = -3/2a(xmax + xmin)
                        
                        c = 3axmaxxmin
                        
        &
                        d = ymax + a/2(xmax - 
                            3axmin)xmax2
                    
These formulas form the mathematical basis of our C++ polynomial solver.
Generating and Animating along a Cubic Polynomial Curve in C++
Once we determine the constants, we can implement a C++ cubic equation solver to animate motion along the curve. The following example shows how to code a polynomial equation in C++ using simple variables and C++ window frame graphics.
To animate an object along a polynomial curve, increment x continuously and compute its corresponding y value using the cubic polynomial equation.
This C++ code allows you to visualize the trajectory of a polynomial equation by plotting the curve dynamically on a C++ window frame. The roots of the polynomial equation and the coefficients determine the shape and symmetry of the curve.
                        Create a new C++ project;
                        call it Dymetric.
                        Create 2 new C++ class files;
                        Call them Facet and CubicPath.
                        Type out the adjoining C++ code for animating an image body through 
                        the path of a cubic / polynomial curve.
                    
Key Takeaways on Cubic Path Animation in C++
In this tutorial, you learned how to:
- Understand and derive cubic polynomial equations
 - Find coefficients from maximum and minimum points
 - Implement a polynomial equation solver using C++
 - Animate an object along a polynomial curve
 
By combining algebraic reasoning with code, senior secondary students can see how mathematics powers real-world applications like animation, computer graphics, and game design.
Applications of Polynomial Equations C++ Programming and STEM Education
Polynomial equations are used in:
- Data modeling and curve fitting
 - Graphics programming for drawing smooth curves
 - Physics simulations and motion paths
 - Machine learning and optimization problems
 
Learning how to solve polynomial equations in C++ provides a strong foundation for both mathematics and computational thinking.
Summary: Visualizing Polynomial Equations in C++
Polynomial equations are powerful tools for generating smooth, curved motion in graphics and animations. In this tutorial, you've learnt how to solve polynomial equations in C++, understand the mathematics of cubic curves, and create a simple animation that moves an image body along a polynomial equation path.
This interactive C++ polynomial solver visually demonstrates how mathematical equations can be represented as real motion on a graph. It's a simple yet powerful example of combining coding and mathematics for educational purposes.
So! C++ Fun Practice Exercise - Animate along Cubic Path
                        As a fun practice exercise, try modifying the values of xmax, xmin, ymax, 
                        and ymin to observe how they affect the polynomial equation graph. You can also:
                    
- Write a function to calculate the roots of the polynomial.
 - Compare your results with a quadratic equation solver.
 - Build a reusable polynomial equation solver in C++.
 
C++ Cubic Path Dymetric Window Code Stub
C++ Cubic Path Canvas Frame and Button Controls - Header File
class Facet
{
public:
Facet(HWND, int, int);
virtual ~Facet();
bool decorateButton(WPARAM, LPARAM);
bool actionPerformed(HWND, WPARAM, LPARAM);
void informPaint();
};
C++ Cubic Path Canvas Frame and Button Controls - Class File
#include "Facet.h"
#include "CubicPath.h"
CubicPath* cub_path;
/*
* Our custom class that interfaces between the parent window
* and the subsequent daemonstrator classes
*/
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
cub_path = new CubicPath(hWnd, window_width, window_height);
}
/*
* This guy decorates buttons with colour and title text
*/
bool Facet::decorateButton(WPARAM wParam, LPARAM lParam) {
// button glide calling
if (wParam == 12321)
{
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
SetDCBrushColor(lpDIS->hDC, RGB(255, 192, 203));
SelectObject(lpDIS->hDC, GetStockObject(DC_BRUSH));
RECT rect = { 0 };
rect.left = lpDIS->rcItem.left;
rect.right = lpDIS->rcItem.right;
rect.top = lpDIS->rcItem.top;
rect.bottom = lpDIS->rcItem.bottom;
RoundRect(lpDIS->hDC, rect.left, rect.top, rect.right, rect.bottom, 50, 50);
// button text
DrawText(lpDIS->hDC, TEXT("MOVE"), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
return TRUE;
}
return FALSE;
}
/*
* Call the target class' draw method
*/
void Facet::informPaint() {
cub_path->paint();
}
/*
* Say there is more than a single push button,
* this guy picks out the correct button that got clicked
* and calls the corresponding apt function
*/
bool Facet::actionPerformed(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
switch (LOWORD(wParam))
{
case 12321:
cub_path->moveCubic();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete cub_path;
}
C++ Animation Code for Cubic Path Header file
#define aWIDTH 10
#define aHEIGHT 10
class CubicPath
{
public:
CubicPath(HWND, int, int);
virtual ~CubicPath();
void paint();
void moveCubic();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF ball_colour;
int x_start;
int x_max;
int y_max;
int x_min;
int y_min;
int x;
int y;
double a, b, c, d; // coefficients and constant
HPEN ball_pen;
HBRUSH ball_brush;
};
C++ Animation Code for Cubic Path Class file
#include "CubicPath.h"
#include <math.h>
CubicPath::CubicPath(HWND hWnd, int window_width, int window_height)
{
hWindow = hWnd; // save away window handle
this->window_width = window_width; // save away window width
this->window_height = window_height; // save away window width
ball_colour = RGB(255, 0, 0);
x_start = 50;
x_max = 200;
y_max = 110;
x_min = 500;
y_min = 410;
x = x_start;
// constants
a = (double)(-2 * (y_max - y_min)) / pow((x_max - x_min), 3);
b = -((double)3 / 2) * a * (x_max + x_min);
c = 3 * a * x_max * x_min;
d = y_max + ((double)a / 2) * (x_max - 3 * x_min) * pow(x_max, 2);
y = (int)round(a * pow(x, 3) + b * pow(x, 2) + c * x + d);
// Pen and brush for travelling ball
ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
ball_brush = CreateSolidBrush(ball_colour);
hdc = GetDC(hWindow);
SelectObject(hdc, ball_pen); // select ball pen colour
SelectObject(hdc, ball_brush); // select ball brush colour
}
/*
* draws the ball/circle using the apt color
*/
void CubicPath::paint() {
// draw a dot
Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}
/*
Repeatedly draws ball so as to simulate a continuous motion
*/
void CubicPath::moveCubic() {
// condition for continuing motion
while (x + aWIDTH <= window_width && y >= y_max) {
paint();
x += 20;
y = (int)round(a * pow(x, 3) + b * pow(x, 2) + c * x + d);
// introduce a delay between renderings
Sleep(50);
}
}
CubicPath::~CubicPath()
{
DeleteObject(ball_pen);
DeleteObject(ball_brush);
ReleaseDC(hWindow, hdc);
}