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

Posted on 2006-03-20
Medium Priority
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
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 500 total points
ID: 16237265
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 86

Expert Comment

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

Author Comment

ID: 16238468
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 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