Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Check if xy point is within a polygon Console App

Check if xy point is within a polygon Console App

Scheduled Pinned Locked Moved C#
helpquestioncsharpdatabasemysql
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 11889214
    wrote on last edited by
    #1

    I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?

    L OriginalGriffO 2 Replies Last reply
    0
    • U User 11889214

      I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What exactly is the problem? If the calculations work in a Windows Form app, then they will work in any other app.

      U 1 Reply Last reply
      0
      • U User 11889214

        I have create a Win form app and create Polygons as follows: private List> Polygons = new List>(); I store this polygons with Xml.Serialization, into a MySQL database as string and Deserialize it with my console app into a list of polygons again. In my console app, which eventually is going to run from Raspberry Pi, I'm getting data from USB, which consist of floating X and floating Y coordinates. Now I want to know how do I check if this point is within one of the polygons. I've tried it first with WinForms app and it works great. public static bool IsPointInPolygon(IList polygon, PointF testPoint) { try { bool result = false; int j = polygon.Count() - 1; for (int i = 0; i < polygon.Count(); i++) { if (polygon[i].Y < testPoint.Y && polygon[j].Y >= testPoint.Y || polygon[j].Y < testPoint.Y && polygon[i].Y >= testPoint.Y) { if (polygon[i].X + (testPoint.Y - polygon[i].Y) / (polygon[j].Y - polygon[i].Y) * (polygon[j].X - polygon[i].X) < testPoint.X) { result = !result; } } j = i; } return result; } catch (Exception ex) { MessageBox.Show("error - " + ex.ToString()); return false; } I don't want to draw something, I know it's a console app, just getting a bool or int value back from the Test. Any suggestions or help please?

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        If you create a GraphicsPath object for the points, you can use that to check:

                System.Drawing.Drawing2D.GraphicsPath poly = new System.Drawing.Drawing2D.GraphicsPath();
                poly.AddLine(10, 10, 90, 10);
                poly.AddLine(90, 10, 90, 90);
                poly.AddLine(90, 90, 10, 90);
                poly.AddLine(10, 90, 10, 10);
                Point inPoly = new Point(50, 50);
                Point outPoly = new Point(0, 0);
                Console.WriteLine(poly.IsVisible(inPoly) ? "IN" : "OUT");
                Console.WriteLine(poly.IsVisible(outPoly) ? "IN" : "OUT");
        

        The beauty of that is that the poly can be extremely complex and the framework will sort it out. For example, an hourglass:

                System.Drawing.Drawing2D.GraphicsPath poly = new System.Drawing.Drawing2D.GraphicsPath();
                poly.AddLine(10, 10, 90, 90);
                poly.AddLine(90, 90, 10, 90);
                poly.AddLine(10, 90, 90, 10);
                poly.AddLine(90, 10, 10, 10);
                Point inPoly = new Point(50, 40);
                Point outPoly = new Point(40, 50);
                Console.WriteLine(poly.IsVisible(inPoly) ? "IN" : "OUT");
                Console.WriteLine(poly.IsVisible(outPoly) ? "IN" : "OUT");
        

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • L Lost User

          What exactly is the problem? If the calculations work in a Windows Form app, then they will work in any other app.

          U Offline
          U Offline
          User 11889214
          wrote on last edited by
          #4

          I didn't add the drawing reference. Not my day. Thank you Richard. It works

          L OriginalGriffO 2 Replies Last reply
          0
          • U User 11889214

            I didn't add the drawing reference. Not my day. Thank you Richard. It works

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            But the calculation is the same. Create a set of points which describe the polygon. Now do the comparisons with your test points.

            1 Reply Last reply
            0
            • U User 11889214

              I didn't add the drawing reference. Not my day. Thank you Richard. It works

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Says who? Of course you can! See the code I gave you in my reply - that code works just fine in a Console app!

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups