퇴근5분전

훈스 닷넷에 질문글로 올라왔던 다각형 내부 클릭체크에 대한 답변으로 
C++로 된것을 찾아서 C#으로 변경함. 

사각형을 그렸을 때.( 일반 Rectangle이라면 Rectangle로도 충분히 체크가 가능하다. )

            pts[0] = new PointF();
            pts[0].X = 200;
            pts[0].Y = 100;

            pts[1] = new PointF();
            pts[1].X = 200;
            pts[1].Y = 200;

            pts[2] = new PointF();
            pts[2].X = 100;
            pts[2].Y = 200;

            pts[3] = new PointF();
            pts[3].X = 100;
            pts[3].Y = 100;

            pts[4] = new PointF();
            pts[4].X = 200;
            pts[4].Y = 100;

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
               //   0 To  mouse.X ;

            PointF orgin = new PointF(0, 0);
            int cnt = 0;
            PointF mDown = new PointF( Convert.ToSingle( e.X ), Convert.ToSingle( e.Y ));
            for (int i = 0; i < pts.Length - 1; i++)
                GetIntersectPoint(orgin, mDown, pts[i], pts[i + 1], ref cnt);

            if (cnt % 2 == 1)     this.Text = "내부";
            else                         this.Text = "외부";       
        }




        bool GetIntersectPoint(PointF AP1, PointF AP2,  PointF BP1,  PointF BP2,ref int Cnt)
        {
            float t;
            float s;
            float under = (BP2.Y - BP1.Y) * (AP2.X - AP1.X) - (BP2.X - BP1.X) * (AP2.Y - AP1.Y);
            if (under == 0)
            {
             //   Cnt = Cnt;
                return false;
            }

            float _t = (BP2.X - BP1.X) * (AP1.Y - BP1.Y) - (BP2.Y - BP1.Y) * (AP1.X - BP1.X);
            float _s = (AP2.X - AP1.X) * (AP1.Y - BP1.Y) - (AP2.Y - AP1.Y) * (AP1.X - BP1.X);    
            
            t = _t/under;
            s = _s/under;
            if (t < 0.0 || t > 1.0 || s < 0.0 || s > 1.0)
            {
            //    Cnt = Cnt;
                return false;
            }
            if (_t == 0 && _s == 0)
            {
             //   Cnt = Cnt;
                return false;
            }

            Cnt ++;
            
            return true;
        } 

'# 2) .Net ( Vs 2005 ) > WinForm' 카테고리의 다른 글

판넬 슬라이드 애니메이션  (0) 2009.05.01
데이타베이스 브라우져 ver3  (0) 2009.05.01
프로시져 생성기  (0) 2009.05.01
유저컨트롤-[도구모음]  (0) 2009.05.01
이미지뷰어!  (0) 2009.05.01