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







<< PreviousNext >>

Detecting Circular Regions in C++ | C++ Window Frame Tutorial



Using the Circle Equation for Region Detection

In this tutorial, you'll learn how to detect a circular region in C++ using the circle equation. The equation of a circle, (x - a)² + (y - b)² = r², defines all points (x, y) that are exactly r units away from the center (a, b). This formula helps determine whether a point or moving object lies inside or outside a circular region on an C++ Window Frame. Understanding how to check whether a point or object lies inside a circle region is useful in C++ geometry programming, especially for animations, canvas graphics, and collision detection.


Understanding the Circle Equation | Maths Explanation for C++ Kids

As already explained extensively in the How to Draw and Animate a Circle in C++ tutorial, the equation of a circle with centre (a, b) and radius (r) is: (x - a)2 + (y - b)2 = r2 ;
It can be deduced that y = b ± √(r2 - (x - a)2) ;
And conversely x = a ± √(r2 - (y - b)2).

Hence, the boundaries of any circle lie in the range
b - √(r2 - (xexternal - a)2) ≤ y ≤ b + √(r2 - (xexternal - a)2)
and
a - √(r2 - (yexternal - b)2) ≤ x ≤ a + √(r2 - (yexternal - b)2)

In other words,
* If (x, y) satisfies this equation, the point lies on the circle.
* If (x - a)^2 + (y - b)^2 < r^2, the point is inside the circular region.
* If (x - a)^2 + (y - b)^2 > r^2, the point is outside the circle.



Algorithm to Detect Entrance into Circular Region in C++

To detect when a second shape enters the circle, we use its coordinates in the circle equation to check if they fall within the upper, lower, left, and right boundaries:
That is, whether the y position of the second body lies between the top and bottom limits of the circle boundary at the x position of the second body:
y2nd_img(top) > b - √(r2 - (x2nd_img - a)2)
and y2nd_img(bottom) < b + √(r2 - (x2nd_img - a)2)
;
And at the same time, whether the x position of the second body lies between the left and right limits of the circle boundary at the y position of the second body:
x2nd_img(left) > a - √(r2 - (y2nd_img - b)2)
and x2nd_img(right) < a + √(r2 - (y2nd_img - b)2)

C++ circle region detection example on C++ window frame
Figure: C++ circle region detection example on C++ window frame

Create a new C++ project; call it Dymetric.
Create 2 new C++ class files;
Call them Facet and CircularRegion.
Type out the adjoining C++ code for detecting the instance a travelling body crosses the boundary of a circle.


How the C++ Circular Region Detection Code Works

The code compares the distance of a point from the circle's centre with the radius. If the distance is smaller than or equal to the radius, the point is inside the circular region.

🟢 A green point shows it's inside the circular region.
🔴 A red point shows it's outside.

The code above demonstrates C++ circle collision detection, a common concept in canvas-based animations and game design. This example shows how maths meets programming - turning the circle equation into real-time C++ geometry detection.

Key Takeaways on Circular Region Detection in C++

In this tutorial, you've learned that:

  • The circle equation defines a circular region mathematically.
  • With a few lines of C++ code, you can detect whether a point is inside or outside the circle.
  • This principle links senior secondary maths and practical C++ applications, preparing you for real-world coding projects.

With just a few lines of C++, you've been able to check when a point enters or leaves a circular boundary - a technique useful in games, animations, and simulations. The tutorial also features a C++ canvas example that visualizes circle region detection in real time.



FAQs: Circle Equation and C++

What is a circular region in C++?

A circular region refers to the area within a circle defined by its radius on the C++ window frame. In C++, you can detect whether a point or shape lies inside it using the circle equation.

How do you detect a circle boundary in C++?

You can calculate the distance between a point and the circle's center and compare it to the radius - if the distance is less than the radius, the point is inside the circle.

Can this be used for games or simulations?

Yes! Circle region detection is common in C++ game development, collision detection, and animations.

Summary: Visualizing Circular Region in C++

In this lesson, you've learnt how to detect a circular region in C++ using the circle equation from coordinate geometry: (x - a)² + (y - b)² = r².

This powerful formula helps determine whether a point or object is inside, on, or outside a circle. It connects senior secondary mathematics with C++ geometry programming through step-by-step examples and code.

By combining mathematics and C++ coding, you can easily detect when objects cross a circular boundary. This exercise strengthens your understanding of circle equations and introduces essential concepts in C++ graphics programming.



So! C++ Fun Practice Exercise - Detect Circular Region

As a fun practice exercise, try changing the values of (a), (b), (r), (x), and (y) to test different points and circle sizes. You can also extend this idea to moving body detection inside a circle, or collision detection in small games and interactive animations.







C++ Circular Boundary Dymetric Window Code Stub


C++ Circular Boundary 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++ Circular Boundary Canvas Frame and Button Controls - Class File

#include "stdafx.h"
#include "Facet.h"
#include "CircularRegion.h"

CircularRegion* cyc_region;

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

Facet::Facet(HWND hWndint window_widthint window_height)
{
    cyc_region = new CircularRegion(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() {
    cyc_region->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:
        cyc_region->circledSquare();
        return TRUE;
    default:
        return FALSE;
    }
}

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

C++ Animation Code for Circular Region - Header File

#pragma once

#define sqLENGTH 100

class CircularRegion
{
public:
    CircularRegion(HWNDintint);
    virtual ~CircularRegion();
    void paint();
    void circledSquare();
protected:
    HWND hWindow;
    HDC hdc;
    int window_width;
    int window_height;
    
    COLORREF sq_colour;
    // coordinates for the ball(circle)
    int x_square;
    int y_square;
    int previous_x;
    int previous_y;

    //circle coordinates
    int a;
    int b;
    const int r = 150;
    double discriminant;

    HBRUSH background_brush;
    HPEN circle_pen;
    HBRUSH sq_brush;
};


C++ Animation Code for Circular Region - Class File

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


CircularRegion::CircularRegion(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

    sq_colour = RGB(255, 255, 0); // give our square a yellow colour
    // coordinates for the ball(circle)
    x_square = 10;
    y_square = 250;
    previous_x = x_square;
    previous_y = y_square;

    //circle coordinates
    a = 500;
    b = 275;
    double discriminant;

    // Pen and brush matching background colour
    background_brush = CreateSolidBrush(RGB(192, 192, 192));

    // pen(purple colour) for circle
    circle_pen = CreatePen(PS_SOLID, 5, RGB(255, 0, 255));

    // brush for travelling square
    sq_brush = CreateSolidBrush(sq_colour);

    hdc = GetDC(hWindow);
}

/*
* draws the ball/circle using the apt color
*/

void CircularRegion::paint() {
    SelectObject(hdc, circle_pen); // use purple colour
    SelectObject(hdc, background_brush); // use background colour
    // draw a circle
    Ellipse(hdc, a - r, b - r, a + r, b + r);

    // erase previous square
    RECT prev_sq;
    prev_sq.left = previous_x;
    prev_sq.top = previous_y;
    prev_sq.right = previous_x + sqLENGTH;
    prev_sq.bottom = previous_y + sqLENGTH;
    FillRect(hdc, &prev_sq, background_brush);

    // draw square
    RECT sq;
    sq.left = x_square;
    sq.top = y_square;
    sq.right = x_square + sqLENGTH;
    sq.bottom = y_square + sqLENGTH;
    FillRect(hdc, &sq, sq_brush);
    
    previous_x = x_square;
    previous_y = y_square;
}

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

void CircularRegion::circledSquare() {
    // condition for continuing motion
    while (x_square + sqLENGTH < window_width) {
        int square_left = x_square;
        int square_right = x_square + sqLENGTH;
        int square_top = y_square;
        int square_bottom = y_square + sqLENGTH;

        // determinants for each side of the square
        int x_left_det = (int)round(sqrt(pow(r, 2) - pow((square_left - a), 2)));
        int x_right_det = (int)round(sqrt(pow(r, 2) - pow((square_right - a), 2)));
        int y_up_det = (int)round(sqrt(pow(r, 2) - pow((square_top - b), 2)));
        int y_down_det = (int)round(sqrt(pow(r, 2) - pow((square_bottom - b), 2)));

        // check the bounds of the circle
        if (square_top > b - x_left_det && square_bottom < b + x_left_det
            && square_top > b - x_right_det && square_bottom < b + x_right_det
            && square_left > a - y_up_det && square_right < a + y_up_det
            && square_left > a - y_down_det && square_right < a + y_down_det) {
            // green colour for our moving body(square) inside our circle
            sq_colour = RGB(0, 255, 0);
        }
        else {
            // yellow color for our moving body(square) outside our circle
            sq_colour = RGB(255, 255, 0);
        }
        DeleteObject(sq_brush); // delete former brush
        sq_brush = CreateSolidBrush(sq_colour); // recreate brush with a new colour

        paint();

        x_square += 10;
        // introduce a delay between renderings
        Sleep(50);
    }
}


CircularRegion::~CircularRegion()
{
    DeleteObject(background_brush);
    DeleteObject(circle_pen);
    DeleteObject(sq_brush);

    ReleaseDC(hWindow, hdc);
}





<< PreviousNext >>