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







<< PreviousNext >>

Detect a Point Inside a Quadratic Region Using C# | Senior Secondary Maths Tutorial



Understanding the Quadratic Region Concept | Maths Explanation for C# Kids

Understanding quadratic regions is a fundamental topic in senior secondary mathematics. When combined with C#, students can move beyond theory and create interactive visualizations that make quadratic inequalities easier to understand.

In this tutorial, you will learn how to detect a point inside a quadratic region using C#. We will explore how a quadratic function defines a parabolic region and how to determine whether a point lies inside or outside that region using mathematical logic and code.


What is a Quadratic Region? | Maths Explanation for C# Kids

A quadratic region is the area defined by a quadratic function (parabola) and a boundary condition, usually represented as a quadratic inequality.

For example:

\[ y \leq ax^2 + bx + c \]

This inequality describes the region under a quadratic curve.

Every quadratic equation has two x-values (roots) for any given y-value, except at its turning point (maximum or minimum).
We can use these roots as boundaries for region detection.

For example:

  • If \( y \leq ax^2 + bx + c \), the region lies below the parabola.
  • If \( y \geq ax^2 + bx + c \), the region lies above the parabola.

In graphical terms, we are identifying the region under a quadratic curve or above it, depending on the inequality.

In mathematics, students learn to:

  • Identify the quadratic function
  • Sketch the parabola
  • Determine whether a point satisfies the quadratic inequality
  • Interpret the shaded region graphically

Using C#, we can automate this detection process and visualize the result dynamically.


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 = ax^2 + bx + c \\ ax^2 + bx + (c-y) = 0 \\ x = \frac{-b \pm \sqrt{b^2 - 4a(c-y)}}{2a} \]

Our range will then be:

\[ \frac{-b - \sqrt{b^2 - 4a(c-y)}}{2a} \; \leq \; x \; \leq \; \frac{-b + \sqrt{b^2 - 4a(c-y)}}{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.

visualizing quadratic curve region in C# and ball trajectory crossing into it on C# windows form
Figure: Visualizing quadratic curve region in C# and an object trajectory passing through it on C# windows form.

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$-value of the ball against the $x$-value of the quadratic equation at the same $y$ position as that of the ball.

We'll designate the coordinates of the ball as \( (x_b, y_b) \), and those of the curve as \( (x_q, y_q) \).

Canvas quadratic region detection C# example
Figure: Detecting and visualizing the quadratic region on a C# windows form using C#.

To determine whether a point $(x, y)$ lies inside a quadratic region:

Step 1.

Compute the quadratic function value at the point's x-coordinate in the range \( \frac{-b - \sqrt{b^2 - 4a(c-y)}}{2a} \; \leq \; x \; \leq \; \frac{-b + \sqrt{b^2 - 4a(c-y)}}{2a} \):

\[ y_{curve} = ax^2 + bx + c \]
Step 2.

Compare the point's y-value with the calculated value.

If:

  • \( y_b \leq y_{curve} \) → the point lies inside the quadratic region.
  • \( y_b > y_{curve} \) → the point lies outside the region.

This simple comparison forms the foundation of quadratic region detection in C#.

Create a new C# Windows Forms Application project ; call it Dymetric_CS.
Create 2 new C# classes;
Call them Dymetric and QuadraticRegion.
Type out the adjoining C# code for detecting the instance a travelling body crosses the boundary of a quadratic curve.


Why Combine Mathematics and C#?

Visualising quadratic regions improves conceptual understanding. Instead of simply solving inequalities symbolically, students can:

  • See how the parabola defines the boundary.
  • Observe how changing coefficients affects the region.
  • Understand how inequalities determine shading.
  • Explore how region detection algorithms work in practice.

This approach connects senior secondary mathematics with real-world programming skills.

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.


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.

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.

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# windows form. 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 Window Display Code Stub


C# Quadratic Curve Boundary Code for Dymetric Class

using System.Windows.Forms;

namespace Dymetric
{
    class Dymetric
    {
        private QuadraticRegion quad_region;
        private bool do_simulation;

        public Dymetric(int screen_width, int screen_height)
        {
            quad_region = new QuadraticRegion(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
                quad_region.inPlay(e);
                do_simulation = false;
            }
            else
            {
                // Put ball on screen
                quad_region.clearAndDraw(e);
                do_simulation = true;
            }
        }
    }
}

C# Animation Code for Quadratic Region Class

using System;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;

namespace Dymetric
{
    class QuadraticRegion
    {
        // ball coordinates
        private int x_ball, y_ball, previous_x, previous_y;
        private const int ballDIAMETER = 80;
        // quadratic variables
        private int xq_start, yq_start, xq_min, yq_min, xq_stop, x, y;
        private double xq_lb, xq_ub; // curve lower and upper boundary
        private double a, b, c, discriminant;
        private const int dotDIAMETER = 5;

        // we'll be drawing to and from a bitmap image
        private Bitmap offscreen_bitmap;
        Graphics offscreen_g;

        private Brush ball_colour, dot_colour, bg_colour;

        public QuadraticRegion(int screen_width, int screen_height)
        {
            ball_colour = new SolidBrush(Color.Yellow);
            dot_colour = new SolidBrush(Color.Black);
            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_ball = 10;
            previous_y = y_ball = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;

            xq_start = offscreen_bitmap.Width / 2 - 200;
            yq_start = 10;
            xq_min = offscreen_bitmap.Width / 2;
            yq_min = offscreen_bitmap.Height - 70;
            xq_stop = offscreen_bitmap.Width / 2 + 200;

            // constants
            a = (yq_start - yq_min) / Math.Pow((xq_start - xq_min), 2);
            b = -2 * a * xq_min;
            c = yq_min + a*Math.Pow(xq_min, 2);

            discriminant = Math.Sqrt(b*b - 4*a*(c - (y_ball - ballDIAMETER / 2)));
            if (a < 0) // a is negative
            {
                xq_lb = (-b + discriminant) / (2 * a); // lower boundary
                xq_ub = (-b - discriminant) / (2 * a); // upper boundary
            }
            else
            {
                xq_lb = (-b - discriminant) / (2 * a); // lower boundary
                xq_ub = (-b + discriminant) / (2 * a); // upper boundary
            }
        }

        // draw first appearance of ball on the screen
        public void clearAndDraw(PaintEventArgs e)
        {
            /*
             * draw to offscreen bitmap
             */

            // draw quadratic curve
            for (x = xq_start; x <= xq_stop; x++)
            {
                y = (int)Math.Round(a * x * x + b * x + c);
                // redraw dot
                offscreen_g.FillEllipse(dot_colour, x, y, dotDIAMETER, dotDIAMETER);
            }

            // clear previous ball
            offscreen_g.FillEllipse(bg_colour, previous_x, previous_y, ballDIAMETER, ballDIAMETER);
            // draw ball
            offscreen_g.FillEllipse(ball_colour, x_ball, y_ball, ballDIAMETER, ballDIAMETER);
            previous_x = x_ball;
            previous_y = y_ball;

            // draw to screen
            Graphics gr = e.Graphics;
            gr.DrawImage(offscreen_bitmap, 0, 55, offscreen_bitmap.Width, offscreen_bitmap.Height);
        }

        // repetitively clear and draw ball on the screen - Simulate motion
        public void inPlay(PaintEventArgs e)
        {
            // condition for continuing motion
            while (x_ball < offscreen_bitmap.Width - ballDIAMETER)
            {
                // yellow outside the quadratic region
                ball_colour = new SolidBrush(Color.Yellow);
                if ((x_ball <= xq_lb && x_ball + ballDIAMETER >= xq_lb)
                    || (x_ball <= xq_ub && x_ball + ballDIAMETER >= xq_ub))
                {
                    // red on the quadratic outline
                    ball_colour = new SolidBrush(Color.Red);
                }
                else if (x_ball >= xq_lb && x_ball + ballDIAMETER <= xq_ub)
                {
                    // green inside the quadratic region
                    ball_colour = new SolidBrush(System.Drawing.Color.Green);
                }

                // redraw ball
                clearAndDraw(e);

                x_ball += 5;
                // take a time pause
                Thread.Sleep(50);
            }
            x_ball = 10;
            y_ball = offscreen_bitmap.Height / 2 - ballDIAMETER / 2;
        }
    }
}






<< PreviousNext >>