• 大小: 34KB
    文件类型: .zip
    金币: 2
    下载: 0 次
    发布日期: 2024-02-01
  • 语言: C#
  • 标签: C#  射线法  

资源简介

使用射线法,精确判断某点是否在由一组点所确定的任意的闭合曲线内(折线图)

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PointInShape
{
    /*
     * 转自:http://blog.csdn.net/kome2000/archive/2009/07/27/4383040.aspx
     * 该方法构思巧妙:
      1、向X轴正方向发射射线,先判断是否与多边形的线段相交,若相交点的X值大于P的x值,则计数器加1.
      2、通过大于等于线段两个端点的最小Y值,小于线段两个端点的最大Y值,判断出射线与线段是否相交。——避免了P在线段的延长线上的情况对计算结果的困扰。 
     * */
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        /// 
        /// 点是否在闭合曲线
        /// 

        /// 测试点
        /// 有Point[]确定的闭合曲线
        /// 
         private bool PointInFences(PointF pnt1 PointF[] fencePnts)
        {
            int j=0 cnt = 0;
            for (int i = 0; i < fencePnts.Length; i++)
            {
                j = (i == fencePnts.Length - 1) ? 0 : j + 1;
                if ((fencePnts[i].Y!=fencePnts[j].Y)&&
                    (((pnt1.Y >= fencePnts[i].Y) && (pnt1.Y < fencePnts[j].Y)) ||
                    ((pnt1.Y >= fencePnts[j].Y) && (pnt1.Y < fencePnts[i].Y))) && (pnt1.X < (fencePnts[j].X - fencePnts[i].X) * (pnt1.Y - fencePnts[i].Y) / (fencePnts[j].Y - fencePnts[i].Y) + fencePnts[i].X)) 
                    cnt++;
            }
            return (cnt%2>0)?true:false;
        }

        private void button2_Click(object sender EventArgs e)
        {
            PointF[] fencePnts = new PointF[] { new PointF((float)0.33 (float)0.33) 
                                                new PointF((float)0.48 (float)0.51)
                                                new PointF((float)0.74 (float)0.26) 
                                                new PointF((float)0.42 (float)0.12) };//R

            bool res = PointInFences(new PointF((float)0.2 (float)0.2) fencePnts);
            if (res)
            {
                MessageBox.Show(“点在多边形内“);
            }
            else MessageBox.Show(“点在多边形外“);
        }

    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2011-05-25 10:37  PointInShape\
     目录           0  2011-05-25 10:44  PointInShape\PointInShape\
     文件         925  2011-05-20 13:11  PointInShape\PointInShape.sln
     文件       16384  2011-05-25 10:44  PointInShape\PointInShape.suo
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\bin\
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\bin\Debug\
     文件       20480  2011-05-25 10:42  PointInShape\PointInShape\bin\Debug\PointInShape.exe
     文件       24064  2011-05-25 10:42  PointInShape\PointInShape\bin\Debug\PointInShape.pdb
     文件        5632  2005-11-11 22:25  PointInShape\PointInShape\bin\Debug\PointInShape.vshost.exe
     文件        2314  2011-05-25 10:44  PointInShape\PointInShape\Form1.cs
     文件        2022  2011-05-25 10:39  PointInShape\PointInShape\Form1.Designer.cs
     文件        5814  2011-05-25 10:39  PointInShape\PointInShape\Form1.resx
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\obj\
     目录           0  2011-05-25 10:42  PointInShape\PointInShape\obj\Debug\
     文件         842  2011-05-25 10:41  PointInShape\PointInShape\obj\Debug\PointInShape.csproj.GenerateResource.Cache
     文件       20480  2011-05-25 10:42  PointInShape\PointInShape\obj\Debug\PointInShape.exe
     文件         180  2011-05-25 10:41  PointInShape\PointInShape\obj\Debug\PointInShape.Form1.resources
     文件       24064  2011-05-25 10:42  PointInShape\PointInShape\obj\Debug\PointInShape.pdb
     文件         180  2011-05-20 13:12  PointInShape\PointInShape\obj\Debug\PointInShape.Properties.Resources.resources
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\obj\Debug\Refactor\
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\obj\Debug\TempPE\
     文件         303  2011-05-25 10:42  PointInShape\PointInShape\obj\PointInShape.csproj.FileList.txt
     文件        3231  2011-05-20 13:19  PointInShape\PointInShape\PointInShape.csproj
     文件         471  2011-05-20 13:11  PointInShape\PointInShape\Program.cs
     目录           0  2011-05-25 10:37  PointInShape\PointInShape\Properties\
     文件        1182  2011-05-20 13:11  PointInShape\PointInShape\Properties\AssemblyInfo.cs
     文件        2878  2011-05-20 13:11  PointInShape\PointInShape\Properties\Resources.Designer.cs
     文件        5612  2011-05-20 13:11  PointInShape\PointInShape\Properties\Resources.resx
     文件        1095  2011-05-20 13:11  PointInShape\PointInShape\Properties\Settings.Designer.cs
     文件         249  2011-05-20 13:11  PointInShape\PointInShape\Properties\Settings.settings

评论

共有 条评论