Home > Backend Development > C++ > How to Determine if a Point Lies Within a Polygon in C#?

How to Determine if a Point Lies Within a Polygon in C#?

Mary-Kate Olsen
Release: 2025-01-04 09:56:39
Original
735 people have browsed it

How to Determine if a Point Lies Within a Polygon in C#?

C# Point in Polygon: Determining if a Point Lies Within a Polygon

Consider the scenario where you want to determine if a point exists within the boundaries of a polygon. To accomplish this task in C# using WinForms, follow these steps:

  1. Calculate Polygon Bounds: Establish the bounding box for the polygon by identifying the minimum and maximum X and Y coordinates among its vertices.
  2. Determine if Point is Within Bounds: Verify if the given point lies within the polygon's bounding box. If not, the point is definitely outside the polygon.
  3. Check Point Inclusion in Polygon: To determine if the point is actually within the polygon's shape, employ the Point-in-Polygon algorithm technique.

One such algorithm is the Ray Crossing method, which involves the following steps:

a. Imagine drawing horizontal rays from the given point to infinity.

b. Count the number of times the ray intersects the polygon's edges.

c. If the count is odd, the point is inside the polygon; if even, it is outside the polygon.

Here is an example implementation of the Ray Crossing algorithm in C#:

using System;
using System.Collections.Generic;
using System.Drawing;

public class Polygon
{
    List<PointF> vertices = new List<PointF>();

    public bool Contains(PointF point)
    {
        if (IsOutsideBoundingBox(point)) return false;

        int intersectionCount = 0;
        for (int i = 0; i < vertices.Count; i++)
        {
            int j = (i + 1) % vertices.Count;
            PointF a = vertices[i], b = vertices[j];
            if ((a.Y > point.Y && b.Y <= point.Y)
                || (a.Y <= point.Y && b.Y > point.Y))
            {
                double slope = (double)(b.Y - a.Y) / (b.X - a.X);
                if (point.X < a.X + (point.Y - a.Y) / slope)
                    intersectionCount++;
            }
        }

        return intersectionCount % 2 == 1;
    }

    private bool IsOutsideBoundingBox(PointF point)
    {
        return point.X < Xmin || point.X > Xmax || point.Y < Ymin || point.Y > Ymax;
    }
}
Copy after login

This implementation ensures accuracy and efficiency while determining if a point is within a polygon.

The above is the detailed content of How to Determine if a Point Lies Within a Polygon in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template