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)^2 + (y - b)^2 = r^2 \), 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# Windows Form.
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:
It can be deduced that
And conversely
Hence, the boundaries of any circle lie in the range
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:
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:
Create a new C# Windows Forms Application
project
;
call it Dymetric_CS.
Create 2 new C# classes;
Call them Dymetric 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 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.
Why Use the Circle Equation in C#?
Using the circle equation in C# ensures:
- Accurate 2D circular region detection
- Efficient performance without unnecessary calculations
- Strong connection between mathematics and programming
- Clear demonstration of geometry concepts for senior secondary learners
This lesson reinforces how algebraic formulas can be directly applied in real-world programming.
Applications of Circular Region Detection with C#
- Collision detection in games and simulations
- Interactive maths exercises for students
- Physics and geometry simulations using C#
- Dynamic graphics on C# windows form
This technique is fundamental in both mathematics programming tutorials and real-world web development projects.
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.
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 Window Display Code Stub
C# Circular Boundary Code for Dymetric Class
namespace Dymetric
{
class Dymetric
{
private CircularRegion cycle_zone;
private bool do_simulation;
public Dymetric(int screen_width, int screen_height)
{
cycle_zone = new CircularRegion(screen_width, screen_height);
do_simulation = false;
}
// decide what course of action to take
public void decideAction(PaintEventArgs e, bool click_check)
{
if (do_simulation && click_check)
{
// do animation
cycle_zone.inPlay(e);
do_simulation = false;
}
else
{
// Put ball on screen
cycle_zone.clearAndDraw(e);
do_simulation = true;
}
}
}
}
C# Animation Code for Circular Region Class
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
namespace Dymetric
{
class CircularRegion
{
// square coordinates
private int x_square, y_square, previous_x, previous_y;
private const int squareLENGTH = 80;
// circle variables
private int a, b, r;
private const int dotDIAMETER = 10;
// we'll be drawing to and from a bitmap image
private Bitmap offscreen_bitmap;
Graphics offscreen_g;
private Brush bg_colour;
private Pen square_pen;
public CircularRegion(int screen_width, int screen_height)
{
square_pen = new Pen(Color.Yellow);
square_pen.Width = 5;
bg_colour = new SolidBrush(Color.LightGray);
// Create bitmap image
offscreen_bitmap = new Bitmap(screen_width, screen_height - 55,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// point graphic object to bitmap image
offscreen_g = Graphics.FromImage(offscreen_bitmap);
// Set background of bitmap graphic
offscreen_g.Clear(Color.LightGray);
previous_x = x_square = 10;
previous_y = y_square = offscreen_bitmap.Height / 2 - squareLENGTH / 2;
// circle centre coordinates
a = offscreen_bitmap.Width / 2;
b = offscreen_bitmap.Height / 2;
// circle radius
r = offscreen_bitmap.Height / 3;
}
// draw first appearance of square on the screen
public void clearAndDraw(PaintEventArgs e)
{
/*
* draw to offscreen bitmap
*/
// draw a circle
offscreen_g.DrawEllipse(Pens.Black, a - r, b - r, 2 * r, 2 * r);
// clear previous square
offscreen_g.FillRectangle(bg_colour, previous_x - 5, previous_y - 5, squareLENGTH + 10, squareLENGTH + 10);
// draw square
offscreen_g.DrawRectangle(square_pen, x_square, y_square, squareLENGTH, squareLENGTH);
previous_x = x_square;
previous_y = y_square;
// draw to screen
Graphics gr = e.Graphics;
gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
}
// repetitively clear and draw square on the screen - Simulate motion
public void inPlay(PaintEventArgs e)
{
// condition for continuing motion
while (x_square < offscreen_bitmap.Width - squareLENGTH)
{
int square_left = x_square;
int square_right = x_square + squareLENGTH;
int square_top = y_square;
int square_bottom = y_square + squareLENGTH;
// determinants for each side of the square
int x_left_det = (int)Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_left - a), 2)));
int x_right_det = (int)Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_right - a), 2)));
int y_up_det = (int)Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_top - b), 2)));
int y_down_det = (int)Math.Round(Math.Sqrt(Math.Pow(r, 2) - Math.Pow((square_bottom - b), 2)));
// check the bounds of the circle
square_pen.Dispose();
// yellow outside the circle
square_pen = new Pen(Color.Yellow);
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 inside the circle
square_pen = new Pen(Color.Green);
}
square_pen.Width = 5;
// redraw square
clearAndDraw(e);
x_square += 10;
// take a time pause
Thread.Sleep(50);
}
x_square = 10;
y_square = offscreen_bitmap.Height / 2 - squareLENGTH / 2;
}
}
}