usingMaths.com
From Theory to Practice - Math You Can Use.







<< PreviousNext >>

Quadratic Equation in C++ | How to Solve and Animate Quadratic Curves



Understanding the Quadratic Function | Maths Explanation for C++ Kids

In this tutorial, we'll explore how to solve quadratic equations in C++ and use them to plot and animate a quadratic curve on a C++ window frame. Understanding the quadratic equation in programming helps you create parabolic motion, simulations, and engaging math visuals.

A quadratic equation has the general form y = ax2 + bx + c; where a, b, and c are constants. This tutorial explains how to solve, plot, and animate a quadratic function using C++ and the C++ window frame.

C++ graph of quadratic curve
Figure: C++ graph of quadratic curve

Generating Quadratic Curves for C++

To generate a quadratic curve in C++, you need two points - the starting point and the vertex (turning point - maximum or minimum).

Quadratic equation graph in C++ showing quadratic points - start point and turning (maximum) point.
Figure: Quadratic equation graph in C++ showing quadratic points - start point and turning (maximum) point.

The general quadratic function is:

y = ax2 + bx + c

dy/dx = yI = 2ax + b
At maximum / minimum point, yI = 0
yI|(x = xmax) = 0
2axmax + b = 0
b = -2axmax

Substituting b in the general equation
y = ax2 + bx + c
  = ax2 - 2axmaxx + c

         At (xstart, ystart):
ystart = axstart2 - 2axmaxxstart + c
         At (xmax, ymax):
ymax = axmax2 - 2axmax2 + c
    = -axmax2 + c     --------- (eqn *)

Subtracting both derived equations
ystart - ymax = axstart2 - 2axmaxxstart + axmax2
(xstart2 - 2xmaxxstart + xmax2)a = ystart - ymax

Hence:
a   =    ystart - ymax  =  ystart - ymax
xstart2 - 2xmaxxstart + xmax2 (xstart - xmax)2

b = -2axmax
       & from (eqn *)
c = ymax + axmax2


C++ Code Example: Quadratic Path Animation

Once we have the equation, we can generate a quadratic curve with C++ to visualize its motion. The following example demonstrates how to animate an object along a quadratic curve in C++ using the C++ window frame. This is a simple form of quadratic motion simulation that helps visualize parabolic motion, such as a ball being thrown.

To make a body travel by the equation of a quadratic curve, continuously increment x by some interval, and use the quadratic equation to get the corresponding y value.

Create a new C++ project; call it Dymetric.
Create 2 new C++ class files;
Call them Facet and QuadraticPath.
Type out the adjoining C++ code for animating an image body through the path of a quadratic curve.
This simple example demonstrates C++ quadratic animation.


Key Takeaways on Quadratic Path Animation in C++

  • A quadratic function in C++ models parabolic motion or curves.
  • The quadratic equation C++ code can be used for plotting and animations.
  • The constants a, b, and c control the shape and direction of the parabola.

Applications in C++ Programming and STEM Education

The quadratic equation in C++ is useful for programming concepts like projectile motion, trajectory planning, and smooth animation curves. Teachers can use this example to show how maths and coding connect - making parabolas come alive through C++ programming.

Teachers and students can use this C++ quadratic formula tutorial to explore math and programming together. It's a practical example of using maths with code.

Summary: Visualizing Quadratic Equations in C++

In this tutorial, you've learnt how to solve quadratic equations in C++ using the quadratic formula. We've also explore how to plot and animate a quadratic curve on a C++ window frame. Understanding how to code the quadratic equation in C++ is useful for creating simulations, parabolic motion, and smooth animations.

By combining mathematics and C++, you can solve and animate quadratic equations easily. Whether you're plotting parabolas, simulating motion, or building educational tools, mastering the quadratic formula in C++ gives you a solid foundation in computational math.



So! C++ Fun Practice Exercise - Animate along Quadratic Path

As a fun practice exercise, try adjusting the coefficients a, b, and c to change the curve's shape or motion pattern. This will be a great way to connect mathematics and programming, and help you understand more about C++ animations and quadratic equations.







C++ Quadratic Path Dymetric Window Code Stub


C++ Quadratic Path Canvas Frame and Button Controls - Header File

#pragma once

class Facet
{
public:
    Facet(HWNDintint);
    virtual ~Facet();
    bool decorateButton(WPARAMLPARAM);
    bool actionPerformed(HWNDWPARAMLPARAM);
    void informPaint();
};


C++ Quadratic Path Canvas Frame and Button Controls - Class File

#include "stdafx.h"
#include "Facet.h"
#include "QuadraticPath.h"

QuadraticPath* qpath;

/*
* Our custom class that interfaces between the parent window
* and the subsequent daemonstrator classes
*/

Facet::Facet(HWND hWndint window_widthint window_height)
{
    qpath = new QuadraticPath(hWndwindow_widthwindow_height);
}


/*
* This guy decorates buttons with colour and title text
*/

bool Facet::decorateButton(WPARAM wParamLPARAM 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() {
    qpath->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 hWndWPARAM wParamLPARAM lParam)
{
    switch (LOWORD(wParam))
    {
    case 12321:
        qpath->moveQuadratic();
        return TRUE;
    default:
        return FALSE;
    }
}

Facet::~Facet()
{
    delete qpath;
}

C++ Animation Code for Quadratic Path - Header File

#pragma once

#define aWIDTH 10
#define aHEIGHT 10

class QuadraticPath
{
public:
    QuadraticPath(HWNDintint);
    virtual ~QuadraticPath();
    void paint();
    void moveQuadratic();
protected:
    HWND hWindow;
    HDC hdc;
    int window_width;
    int window_height;
    COLORREF ball_colour;
    int x_start;
    int y_start;
    int x_max;
    int y_max;
    int x;
    int y;
    double a, b, c; // coefficients and constant
    HPEN ball_pen;
    HBRUSH ball_brush;
};


C++ Animation Code for Quadratic Path - Class File

#include "stdafx.h"
#include "QuadraticPath.h"
#include <math.h>


QuadraticPath::QuadraticPath(HWND hWndint window_widthint 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;
    y_start = 400;
    x_max = 350;
    y_max = 150;
    x = x_start;
    y = y_start;

    // constants
    a = (double) (y_start - y_max) / pow((x_start - x_max), 2);
    b = -2 * a * x_max;
    c = y_max + a * pow(x_max, 2);

    // 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 QuadraticPath::paint() {
    // draw a dot
    Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}

/*
Repeatedly draws ball so as to simulate a continuous motion
*/

void QuadraticPath::moveQuadratic() {
    // condition for continuing motion
    while (x + aWIDTH <= window_width && y <= y_start) {
        paint();

        x += 20;
        y = (int)round(a*x*x + b*x + c);
        // introduce a delay between renderings
        Sleep(50);
    }
}


QuadraticPath::~QuadraticPath()
{
    DeleteObject(ball_pen);
    DeleteObject(ball_brush);

    ReleaseDC(hWindow, hdc);
}
 





<< PreviousNext >>