Determine if a point (x,y) is internal or external a region composed by n(3,4,5...) lines.

Posted on 2006-03-20
Last Modified: 2011-09-20
Hi guys,

I'm writing a small 2D car game in VB. I have problems finding out a good (and FAST) code to determine if the car (x,y) is on or out of the road.
What I've done is divide the road in many regions composed by a number of lines (usually 4 lines), and I set the lines as invisible.
Now I need the code to determine if the car is inside one of the polygons or out of them.

Region1(line1(x1,x2,y1,y2), line2(x1,x2,y1,y2),  line3(x1,x2,y1,y2), ...)
Region2(line1(x1,x2,y1,y2), line2(x1,x2,y1,y2),  line3(x1,x2,y1,y2), ...)

If you suggest any faster way to do what I'm looking for feel free, it will be very appreciated. Please if possible post the code.
Question by:Togno
    LVL 85

    Accepted Solution

    You could use GDI functions...

    Create a Polygon to represent each region via the CreatePolygonRgn() API:

    Then you can use the PtInRegion() API to determine if a point is contained by a Region:
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Don't forget that all GDI calls use PIXELS so convert your coordinates if necessary.

    Author Comment

    Fantastic!! Exactly what I was looking for.
    Not tested in the game yet, but seems perfect, I hope it's fast enough not to slow down the game! I let you know.

    I thank you so much!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction In a recent article ( for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now