Understanding the Quadratic Region Concept | Maths Explanation for C++ Kids
In this tutorial, you'll learn how to detect the region under a quadratic curve using C++. The curve is defined by the equation y = a x² + b x + c, and we'll use the discriminant method to find when a point or object lies within the quadratic region. This concept helps students connect algebraic reasoning with programming and visualization using the C++ window frame.
What is a Quadratic Region? | Maths Explanation for C++ Kids
                        A quadratic region in C++ represents the area bounded by a quadratic curve.
                        Every quadratic equation has two x-values (roots) for any given y - except at its turning point (maximum or minimum).
                        We can use these roots as boundaries for region detection.
                    
                        More technically, a quadratic region is the area defined by a quadratic inequality such as 
                        y ≤ ax² + bx + c.  
                        This concept is useful in computer graphics, physics simulations, and 
                        quadratic curve collision detection (JS) projects.
                    
Checking the Boundaries of a Quadratic Curve in C++
To visualize the region under a quadratic curve, we'll use C++ to calculate the upper and lower limits dynamically. This makes it possible to detect when an object (like a moving ball) enters or exits the quadratic region.
                        As discussed in the Animating along a Straight Line in C++ tutorial, 
                        any quadratic equation always have two roots for any value of y (except at it's maximum or minimum point).
 
                        All we need to do is use these two roots (x values) as boundaries for our check.
                        
                            y = ax2 + bx + c
                            ax2 + bx + (c-y) = 0
                        
                    
| x = | -b ± √(b2 - 4a(c-y)) | 
| 2a | 
Our range will then be:
| -b - √(b2 - 4a(c-y)) | ≤ x ≤ | -b + √(b2 - 4a(c-y)) | 
| 2a | 2a | 
                        where a, b, and c are constants.
                        We will reuse the moving ball graphic from the 
                        Solving and Animating a Quadratic Equation in C++ 
                        tutorial and check for when it enters the region of our curve.
                    
C++ Code Example: Detecting Entrance into a Quadratic Region
                        To check for when our ball enters the quadratic curve, we will continually check the x position
                        of the ball against the x position gotten using the quadratic equation at the same y position 
                        as that of the ball.
                        We'll designate the coordinates of the ball as (xb, yb), 
                        and those of the curve as (xq, yq).
                    
                        To detect a point inside a parabola using C++, 
                        you can compare its coordinates to the quadratic curve.
                        We'll determine whether a moving ball lies within this region by solving 
                        for x using the quadratic formula. 
                        If y is less than or equal to the value of the quadratic equation, 
                        the point lies within the region.
                    
                        Create a new C++ project;
                        call it Dymetric.
                        Create 2 new C++ class files;
                        Call them Facet and QuadraticRegion.
                        Type out the adjoining C++ code for detecting the instance a travelling 
                        body crosses the boundary of a quadratic curve.
                    
Summary: Detecting Quadratic Boundaries with C++
In this senior secondary C++ math tutorial, you've learnt how to identify whether a moving point lies inside a quadratic region. We've used simple algebra and the C++ canvas to visualize and draw the quadratic region bounded by a parabolic curve.
Formula Recap:
The general form of a quadratic equation is y = a x² + b x + c. To find the region under the curve, we can rearrange this equation to get a x² + b x + (c - y) = 0 and use the discriminant D = b² - 4a(c - y).
For any given y-value, if D is positive, the quadratic crosses that y-level at two x-values. The region between these two x-values represents the quadratic region.
                        
                        y = ax² + bx + c 
                        ⟹ ax² + bx + (c - y) = 0 
                        ⟹ x = (-b ± √(b² - 4a(c - y))) / 2a
                        
                    
                        Thus, the quadratic region boundaries are: 
                        (-b - √(b² - 4a(c - y))) / 2a ≤ x ≤ (-b + √(b² - 4a(c - y))) / 2a
                    
Understanding how to compute and visualize quadratic regions in C++ bridges mathematical theory and practical coding. It helps students apply concepts from coordinate geometry in a real-world programming context.
Applying the Line Region Detection Logic in C++
This tutorial teaches you to:
- Compute the region under a quadratic function in C++
 - Use real-time region detection to track an object's position
 - Apply mathematical concepts like discriminants and boundaries in interactive graphics
 
To determine if a point lies inside a quadratic region, we've used a C++ quadratic region detection function. This approach is often used in interactive canvas demos and collision detection algorithms.
So! C++ Fun Practice Exercise - Detect Quadratic curve Boundary
                        As a fun practice exercise, try experimenting with different coefficients (a, b, and c)
                        to see how the quadratic region changes shape.  
                        You can also animate a point moving across the screen to test when it enters or exits the region on the C++ window frame.
                        Experiment with different equations and visualize how region boundaries change dynamically in C++.
                        This is a great way to explore the relationship between algebra and geometry in senior secondary mathematics.
                    
C++ Quadratic Curve Boundary Dymetric Window Code Stub
C++ Quadratic Curve Boundary 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++ Quadratic Curve Boundary Canvas Frame and Button Controls - Class File
#include "Facet.h"
#include "QuadraticRegion.h"
QuadraticRegion* qregion;
/*
* Our custom class that interfaces between the parent window
* and the subsequent daemonstrator classes
*/
Facet::Facet(HWND hWnd, int window_width, int window_height)
{
qregion = new QuadraticRegion(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() {
qregion->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:
qregion->checkBoundary();
return TRUE;
default:
return FALSE;
}
}
Facet::~Facet()
{
delete qregion;
}
C++ Animation Code for Quadratic Region - Header File
#define aWIDTH 5
#define aHEIGHT 5
#define ballWIDTH 100
#define ballHEIGHT 100
class QuadraticRegion
{
public:
QuadraticRegion(HWND, int, int);
virtual ~QuadraticRegion();
void paint();
void checkBoundary();
protected:
HWND hWindow;
HDC hdc;
int window_width;
int window_height;
COLORREF ball_colour;
int x_ball;
int y_ball;
int previous_x;
int previous_y;
int xq_start;
int yq_start;
int xq_min;
int yq_min;
double xq_lb;
double xq_ub;
int xq_stop;
int x;
int y;
double a, b, c; // coefficients and constant
double discriminant;
HPEN background_pen;
HBRUSH background_brush;
HPEN ball_pen;
HBRUSH ball_brush;
HBRUSH curve_brush;
};
C++ Animation Code for Quadratic Region - Class File
#include "QuadraticRegion.h"
#include <math.h>
QuadraticRegion::QuadraticRegion(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, 255, 0); // yellow for our travelling ball colour
x_ball = 50;
y_ball = 200;
previous_x = x_ball;
previous_y = y_ball;
xq_start = 300;
yq_start = 150;
xq_min = 450;
yq_min = 400;
xq_stop = 600;
x = xq_start;
y = yq_start;
// constants
a = (double)(yq_start - yq_min) / pow((xq_start - xq_min), 2);
b = -2 * a * xq_min;
c = yq_min + a * pow(xq_min, 2);
discriminant = sqrt(b * b - 4 * a * (c - (y_ball + (double)(ballHEIGHT / 2))));
if (a < 0) {// a is negative
xq_lb = (double)(-b + discriminant) / (2 * a); // upper boundary
xq_ub = (double)(-b - discriminant) / (2 * a); // lower boundary
}
else {
xq_lb = (double)(-b - discriminant) / (2 * a); // lower boundary
xq_ub = (double)(-b + discriminant) / (2 * a); // upper boundary
}
// Pen and brush matching background colour
background_pen = CreatePen(PS_SOLID, 1, RGB(192, 192, 192));
background_brush = CreateSolidBrush(RGB(192, 192, 192));
// Pen and brush for travelling ball
ball_pen = CreatePen(PS_SOLID, 1, RGB(0, 0, 0));
ball_brush = CreateSolidBrush(ball_colour);
// drawing brush for our curve
curve_brush = CreateSolidBrush(RGB(0, 0, 0));
hdc = GetDC(hWindow);
}
/*
* draws the ball/circle using the apt color
*/
void QuadraticRegion::paint() {
SelectObject(hdc, ball_pen); // use a black pen
SelectObject(hdc, curve_brush); // use a black brush
//draw quadratic curve
for (; x < xq_stop; x++) {
// redraw dot
y = (int)round(a * x * x + b * x + c);
Ellipse(hdc, x, y, x + aWIDTH, y + aHEIGHT);
}
x = xq_start;
SelectObject(hdc, background_pen); // select background colour
SelectObject(hdc, background_brush); // select background colour
// erase previous circle
Ellipse(hdc, previous_x, previous_y, previous_x + ballWIDTH, previous_y + ballHEIGHT);
SelectObject(hdc, ball_pen); // select ball colour
SelectObject(hdc, ball_brush);
// draw a circle
Ellipse(hdc, x_ball, y_ball, x_ball + ballWIDTH, y_ball + ballHEIGHT);
previous_x = x_ball;
previous_y = y_ball;
}
/*
Repeatedly draws ball so as to simulate a continuous motion
*/
void QuadraticRegion::checkBoundary() {
// condition for continuing motion
while (x_ball + ballWIDTH <= window_width) {
if ((x_ball <= xq_lb && x_ball + ballWIDTH >= xq_lb)
|| (x_ball <= xq_ub && x_ball + ballWIDTH >= xq_ub)) {
ball_colour = RGB(255, 0, 0); // trespassing color(red) for our moving body(circle)
}
else if (x_ball >= xq_lb && x_ball + ballWIDTH <= xq_ub) {
ball_colour = RGB(0, 255, 0); // zone color(green) for our moving body(circle)
}
else {
ball_colour = RGB(255, 255, 0); // out of zone color(yellow) for our moving body(circle)
}
DeleteObject(ball_brush); // delete former brush
ball_brush = CreateSolidBrush(ball_colour); // recreate brush with a new colour
paint();
x_ball += 5;
// introduce a delay between renderings
Sleep(50);
}
}
QuadraticRegion::~QuadraticRegion()
{
DeleteObject(background_pen);
DeleteObject(background_brush);
DeleteObject(ball_pen);
DeleteObject(ball_brush);
DeleteObject(curve_brush);
ReleaseDC(hWindow, hdc);
}