// main.cs // C:>csc /t:winexe main.cs using System; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; class AWindow : System.Windows.Forms.Form { private int m_mouse_x; private int m_mouse_y; int[] coords0 = new int[] {1,1,0,198,299,198,298,1,280,63,285,71,283,80,279,91,271,102,276,134,273,160,265,176,252,187,231,188,209,174,203,157,215,132,227,118,228,111,209,110,188,91,171,78,152,80,146,85,146,93,158,97,179,100,191,107,198,122,191,142,172,152,154,151,135,137,128,131,105,130,101,135,100,156,88,173,67,183,41,183,24,172,16,146,21,125,36,111,46,108,64,105,70,99,66,84,50,77,33,82,30,85,21,87,15,77,19,67,41,60,60,60,78,67,89,89,87,107,80,116,65,124,48,126,34,137,34,153,44,165,70,164,83,151,85,129,94,116,114,111,134,113,149,124,160,133,170,133,178,126,168,115,143,113,127,97,131,71,154,61,179,60,195,70,210,88,214,90,234,92,245,99,246,120,233,142,222,155,222,159,239,169,248,166,256,152,256,124,251,96,254,92,264,80,264,69,270,62,278,60}; private void OnPaint(object sender, System.Windows.Forms.PaintEventArgs e) { System.Drawing.Graphics g = e.Graphics; //int[] coords0 = new int[] {5,5, 80, 80 }; //int[] coords0 = new int[] {30,30, 180,30, 180,180, 30,180}; int iLen = coords0.Length; iLen -= 2; GraphicsPath path = new GraphicsPath(); for(int i=0; i 0 ) totalIntersections++; } if( (totalIntersections % 2)==0 ) // even this.Text = "even"; else this.Text = "odd"; */ }// End of OnPaint(..) // Returns positive if the point is on the positive side of the line. - clockwise direction bool isInsideLine(double px, double py, double x0, double y0, double x1, double y1) { double dirx = x1 - x0; double diry = y1 - y0; double ang = -3.14/2.0; double nx = dirx*Math.Cos( ang ) - diry*Math.Sin( ang ); double ny = dirx*Math.Sin( ang ) + diry*Math.Cos( ang ); // Normalize our normals double length = Math.Sqrt(Math.Abs(nx*nx + ny*ny)); nx = nx/length; ny = ny/length; double d = nx*x1 + ny*y1; double k = nx*px + ny*py; //this.Text = "Normal: " + px.ToString() + " " + py.ToString(); if( k 0 ) totalIntersections++; } if( (totalIntersections % 2)==0 ) // even return true; else return false; }// End insidePoly(..) int IntersectLines2D( double p0x, double p0y, double p1x, double p1y, double p2x, double p2y, double p3x, double p3y) { // Uses parametric lines. (e.g. p = p0 + (p1-p0)*t ) where t is from 0 to 1; double p0vx = p1x - p0x; double p0vy = p1y - p0y; double p1vx = p3x - p2x; double p1vy = p3y - p2y; // Test -1- Check for parallel ines. If the direction vectors are scalar // multiples then the lines are parallel and can't possibly intersect unless // the lines overlap double det_p0p1 = (p0vx*p1vy - p0vy*p1vx); if( Math.Abs( det_p0p1) <= 0.000001 ) return 0; // Parallel lines - hence no intersection // Test -2- Computer the t0 and t1 values for intersection, where we have // two lines of hte form // p = p0 + v*t // p1 = p10 + v1*t1 // p1.x = p10.x + v1.x*t1 // p1.y = p10.y + v1.y*t1 // p2 = p20 + v2*t2 // p2.x = p20.x + v2.x*t2 // p2.y = p20.y + v2.y*t2 // Solve the system when x1 = x2 and y1 = y2 double t0 = (p1vx*(p0y - p2y) - p1vy*(p0x - p2x) ) / det_p0p1; double t1 = (p0vx*(p0y - p2y) - p0vy*(p0x - p2x) ) / det_p0p1; if( (t0>=0) && (t0<=1) && (t1>=0) && (t1<=1) ) return 1; // We have an intersection else return 0; // Intersects out of bounds. }//End IntersectLines2D(..) private void OnMouseMoveEvent(object sender, System.Windows.Forms.MouseEventArgs e) { int mouse_x = e.X; int mouse_y = e.Y; m_mouse_x = mouse_x; m_mouse_y = mouse_y; if( insidePoly( coords0, mouse_x, mouse_y) ) this.Text = "even"; else this.Text = "odd"; Invalidate(); this.Update(); } AWindow() { this.Paint += new System.Windows.Forms.PaintEventHandler(this.OnPaint); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OnMouseMoveEvent); }// End of AWindow() // Application Entry Point static void Main() { System.Windows.Forms.Application.Run(new AWindow()); }// End Main() }// End of AWindow class