The simplest IMO, but not necessarily the most efficient is to use regions. A region can be created from an array of points with createpolygonrgn api. Then the 2 can be combined to a 3rd (null) region using the RGN_AND flag of CombineRgn api. If the region is anything but a null region, then they overlap. Here is an example of intersecting regions from a region class I designed several years ago, maybe it might help. Hopefully no typos & hopefully it works.
Code:
Private Declare Function CreatePolygonRgn Lib "gdi32.dll" (ByRef lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function CombineRgn Lib "gdi32.dll" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetRegionData Lib "gdi32.dll" (ByVal hRgn As Long, ByVal dwCount As Long, ByRef lpRgnData As Any) As Long
Private Const RGN_AND As Long = 1
Public Function RegionIntersect(rgnPts1() As POINTAPI, rgnPts2() As POINTAPI) As Boolean
Dim rtnRgn As Long, Rgn1 As Long, Rgn2 As Long
Rgn1 = CreatePolygonRgn(rgnPts1(0), UBound(rgnPts1) +1, 2)
If Rgn1 = 0& Then Exit Function
Rgn2 = CreatePolygonRgn(rgnPts2(0), UBound(rgnPts2) +1, 2)
If Rgn2 = 0& Then
DeleteObject Rgn1
Exit Function
End If
rtnRgn = CreateRectRgn(0&, 0&, 0&, 0&)
CombineRgn rtnRgn, Rgn1, Rgn2, RGN_AND
' if the total region size is 32bytes, this can be interpreted
' that 1) we have a region & 2) the region has no region rectangles
' since the 1st 32bytes of any region is the header information
' and the remaining bytes are the region rectangles.
' No additional rectangles, no collision
If GetRegionData(rtnRgn, 0&, ByVal 0&) > 32& Then RegionIntersect = True
DeleteObject Rgn1
DeleteObject Rgn2
DeleteObject rtnRgn
End Function
I wouldn't count on this method to use in any game that may require real speed. The mathematical solutions will probably be faster I would think.
Last edited by LaVolpe; Feb 24th, 2009 at 07:16 PM.
Insomnia is just a byproduct of, "It can't be done"
The function in post #9 is nice. It could be adapted to not use any divisions (if the intersection coordinates aren't needed) making it potentially very fast.
Techniques vary depending on whether the polygons are concave or convex.
Edit: I've just seen LaVolpes post; I think it might be quite an expensive way to determine an intersection but it is very simple.
The simplest IMO, but not necessarily the most efficient is to use regions. A region can be created from an array of points with createpolygonrgn api. Then the 2 can be combined to a 3rd (null) region using the RGN_AND flag of CombineRgn api. If the region is anything but a null region, then they overlap. Here is an example of intersecting regions from a region class I designed several years ago, maybe it might help. Hopefully no typos & hopefully it works.
Code:
Private Declare Function CreatePolygonRgn Lib "gdi32.dll" (ByRef lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function CombineRgn Lib "gdi32.dll" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetRegionData Lib "gdi32.dll" (ByVal hRgn As Long, ByVal dwCount As Long, ByRef lpRgnData As Any) As Long
Private Const RGN_AND As Long = 1
Public Function RegionIntersect(rgnPts1() As POINTAPI, rgnPts2() As POINTAPI) As Boolean
Dim rtnRgn As Long, Rgn1 As Long, Rgn2 As Long
Rgn1 = CreatePolygonRgn(rgnPts1(0), UBound(rgnPts1) +1, 2)
If Rgn1 = 0& Then Exit Function
Rgn2 = CreatePolygonRgn(rgnPts2(0), UBound(rgnPts2) +1, 2)
If Rgn2 = 0& Then
DeleteObject Rgn1
Exit Function
End If
rtnRgn = CreateRectRgn(0&, 0&, 0&, 0&)
CombineRgn rtnRgn, Rgn1, Rgn2, RGN_AND
' if the total region size is 32bytes, this can be interpreted
' that 1) we have a region & 2) the region has no region rectangles
' since the 1st 32bytes of any region is the header information
' and the remaining bytes are the region rectangles.
' No additional rectangles, no collision
If GetRegionData(rtnRgn, 0&, ByVal 0&) > 32& Then RegionIntersect = True
DeleteObject Rgn1
DeleteObject Rgn2
DeleteObject rtnRgn
End Function
I wouldn't count on this method to use in any game that may require real speed. The mathematical solutions will probably be faster I would think.
Perfect. I am already using regions like in your code, so this works perfectly.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi!
I tried your code but im getting some problems in the run time error.
I gave declared the POINTAPI AS
PUBLIC TYPE POINTAPI
X() AS LONG
Y() AS LONG
END TYPE
please do help me in this declaration.
it is giving script out of range error.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
seth,
Code:
Public Type POINTAPI
X As Long
Y As Long
End Type
... delcare the array like this
Dim myPts() As POINTAPI
.... Fill like this, after ReDim to appropriate size
myPts(0).X = 123: myPts(0).Y = 456
Insomnia is just a byproduct of, "It can't be done"
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi!
I have i problem.
i will click 4 points at different places in an image & once i click the 4th point the system should automatically draw a circle of that radius.
How to do this.
i will click 4 points in an image & after clicking the 4th point then the system should automatically draw a perfect circle with centerpoint.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi!
Can anybody help me out in getting the intersection points where 2 images overlapp each other.
I have tried out alkl possible methods but it ended up in a mess.
pls help me out in getting the intersection points where 2 images are overlapped each other in visual basic 6
Re: [RESOLVED] Polygons - See if they overlap/intersect?
HI!
I have 1 problem. i have attached 1 image refer that i image.
i have 1 line which displays a center point in pixels.
now i want to draw a circle for that line which meets that line.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
HI!
This requirement is very important for me & it is required urgently....
I have 1 line drawn using mouse & im able to find the length of the line & using the length im finding the center point of the line but can i get the mouse position x & y where the center point lies.
By using the center point of line i want to know the Mouse x posistion & y position.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Not sure if I follow you exactly but calculating the center point is easy, it's just the average of the two endpoints. For the radius use Pythagoras theorem.
Assuming the line is defined x1,y1 - x2,y2
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi!
This problem is fuzzing me from the past 3 days & i have not been able to find out a solution for this.
I'm drawing a line using mouse and im also displaying the length of line in pixels. i'm able to find out the center of the line but i want to know the mouse x position & mouse y position of the center point.
i have the total length of the line measured in pixel is it possible to get the mouse x & mouse y position of the center point .
Re: [RESOLVED] Polygons - See if they overlap/intersect?
This is getting off Topic and maybe should be in its own thread.
Your question makes no sense to me. What do you mean by 'mouse x & mouse y position of the center point'. Do you want the coordinates in relation to the current mouse position?
Can you post a picture with the line, circle, and mouse all indicated.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
I'd seen the previous picture, I asked you to post a picture with more detail as to what you want.
As it is the way you describe the picture and what it shows contradict each other, the second label already is shown at the center.
Here is your picture again with a little more info. The coordinates x1,y1 and x2,y2 are known. Please explain as best you can what you want because as it is it makes no sense and reads to me like "I know how to calculate the center point but how do I calculate the center point."
Please also explain the circle you talk of but don't show.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
hi!
Im sorry i have attached a wrong picture in that picture what you see the center point label is adjusted to show that it is in center.
Im sorry for explaining properly.
Here is my problem.
i will draw a line & im calculating the length of the line in pixels say for example 3451 is the total length of the line.
Now i this i can calculate the center point by 3451/2=1725.5.
My problem is i want to place this value 1725.5 at the center of the line by getting the x position & y position.
this is my problem.
i want to place the center point in the middle of the line.
how to do this?
pls let me know & dont take the previuos as example.
i want to place or point the center point of the line.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi !
just like how you have plotted a point in the center of the line just like that based on the length of the line the center point shoud be made visible in images.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
hi!
Im extremely sorry for not furnishing all the details of my problem.
1) i have 1 image
2) i will draw a st line.
3) i will calculate the length of the line in pixels.
4) i will get the center point of the line by dividing the total length by 2.
********************
5) how to plot a point as you have done in the image which i had attached in the previous mail?
6) i want the center point to be pointed so that the user can know that it i the center point of the line.
how to do this in images?
7) after plotting the center point i should be able to automatically draw a circle which meets the where this line passes through the middle of the circle.
Re: [RESOLVED] Polygons - See if they overlap/intersect?
Hi!
I have 1 problem now which is very urgent.
I have developed a software for inventory control in my office.
I have used Visual Basic 6 & crystal report 9 for report purpose.
Is it possible to automatically export the report in pdf of a current transaction?
i want to export the report automatically as soon as the transaction is saved.