VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Ray Intersection point

14 REPLIES 14
Reply
Message 1 of 15
Anonymous
776 Views, 14 Replies

Ray Intersection point

Hi,
In my program i am drawing a ray and calculating how many times it intersects with a polygon. My workflow is like this: User is asked to choose a point>>Ray is drawnat 0 degree from the point selected by the user>>Displayed no. of ponts the ray intersects. So in one specific situation (situation I have described at the end) I am getting an intersection point in my program whereas if i am seeing it in autocad ..my ray is not intersecting with the polygon at any point(Ya..for sure it is just passing above the polygon). Can anyone tell me WHY IT IS SO AND WHAT IS THE SOLUTION ( Although I have found a way by drawing my ray three times at different angle to confirm, if it intersects with the polygon.)
My point Situation:
X=618774.1541: Y = 6223287.8280
Polygon Crds. which my ray will intersect
pt1(x) = 672460.4942 : pt1(y) =6223272.2128
pt2(x) = 672865.0766: pt2(y) =6223287.7940
pt3(x) = 672881.0684: pt3(y) =6222883.3314
pt4(x) = 672476.2884: pt4(y) =6222867.7451
It is a closed polygon.
Crds of intersection point my program is finding:
X = 672865.075288237 : Y = 6223287.82799452
Any help will be greatly appreciated.
Ya 🙂 and you guess it right i am working to know if a pt. is inside a polygon or outside.
I am also attaching the drawing file.
Thanks
14 REPLIES 14
Message 2 of 15
Bryco
in reply to: Anonymous

A ray doesn't always work.
http://softsurfer.com haas some good stuff.
Message 3 of 15
Anonymous
in reply to: Anonymous

Did you try to search the solution
on this NG?

http://discussion.autodesk.com/search.jspa?numResults=25&inputEntered=true&source=thread-threaded%7C33&q=point+inside&objID=f33&search.x=15&search.y=7

~'J'~
Message 4 of 15
Anonymous
in reply to: Anonymous

Thanks for the above two replies. But the above will help to a question something like this..."How to find if a point is inside a polygon or not?" ...But MY QUESTION IS why my ray is giving the point of intersection in my program whereas if in AutoCAD if you will see (in the attached drawing) it is not intersecting anywhere.
Any helpful help will be appreciated. Thanks
Message 5 of 15
Anonymous
in reply to: Anonymous

Hi Anshu
Give this a try, seems to
be working for me but very
quick and dirty
Change it to your suit
Hth

~'J'~

Option Explicit

Sub TestIntersect()
Dim oEnt As AcadEntity
Dim varPt As Variant
Dim oPline As AcadLWPolyline
Dim oRay As AcadRay
Dim icnt As Integer

On Error GoTo Err_Control
ThisDrawing.Utility.GetEntity oEnt, varPt, vbNewLine & "Select Polyline "
If Err Then Exit Sub
If TypeOf oEnt Is AcadLWPolyline Then
Set oPline = oEnt
End If
ThisDrawing.Utility.GetEntity oEnt, varPt, vbNewLine & "Select Ray "
If Err Then Exit Sub
If TypeOf oEnt Is AcadRay Then
Set oRay = oEnt
End If
Dim intPt As Variant
intPt = oPline.IntersectWith(oRay, acExtendNone)
If Not IsEmpty(intPt) Then
icnt = (UBound(intPt) + 1) \ 3
MsgBox "Ray is intersecting with polyline in " & icnt & " point(s)"
End If
Err_Control:
MsgBox Err.Description
End Sub
Message 6 of 15
Anonymous
in reply to: Anonymous

There are a few rare instances where the following code fails, but it is relaible for non mission-critical tasks. This only works with all Z points @ 0.
If your problem is that the Z of the points of the geometry is not @ 0, I would recomend iterating through the points of your poly line and setting all Z's @ 0.
Enjoy.

Private Function ChkInternalPnt(StartPnt As Variant, entLoop As AcadEntity) As Boolean
'function determines if a point is inside a closed loop (2D)
'StartPnt expects an array ~ (0 to 1) as double
'uses 8 rays at 45 degree angles to determine intersections with loop
'if a ray's number of intersections are odd then the point is inside
'per that specific test and passes the test.
'If it passes all 8 (45 degreee) tests, then it is inside
'the loop and is function is true.
Dim objRay As AcadRay
Dim relDir(0 To 2) As Double
Dim pntAry As Variant
Dim NumIntSects As Integer
Dim rayCastCnt As Integer
Dim rayDir(0 To 1, 0 To 7) As Integer
rayDir(0, 0) = 0: rayDir(1, 0) = 1
rayDir(0, 1) = 1: rayDir(1, 1) = 1
rayDir(0, 2) = 1: rayDir(1, 2) = 0
rayDir(0, 3) = 1: rayDir(1, 3) = -1
rayDir(0, 4) = 0: rayDir(1, 4) = -1
rayDir(0, 5) = -1: rayDir(1, 5) = -1
rayDir(0, 6) = -1: rayDir(1, 6) = 0
rayDir(0, 7) = -1: rayDir(1, 7) = 1
relDir(2) = 0
For i = 0 To 7
relDir(0) = StartPnt(0) + rayDir(0, i)
relDir(1) = StartPnt(1) + rayDir(1, i)
NumIntSects = 0
Set objRay = ThisDrawing.ModelSpace.AddRay(StartPnt, relDir)
pntAry = objRay.IntersectWith(entLoop, acExtendNone)
objRay.Delete
Set objRay = Nothing
If VarType(pntAry) vbEmpty Then
NumIntSects = UBound(pntAry) + 1
If (NumIntSects Mod 2) - 1 = 0 Then
rayCastCnt = rayCastCnt + 1
End If
End If
pntAry = vbEmpty
Next i
Erase pntAry
If rayCastCnt = 8 Then ChkInternalPnt = True
End Function
Message 7 of 15
Anonymous
in reply to: Anonymous

Whoops.. I apologize I posted some development code. The next version of that code rotates the ray instead of creating and deleting them. This leaves less of a footprint on the drawing.

Unfortunately I do not have that code here at work with me.
Good luck!
Message 8 of 15
Anonymous
in reply to: Anonymous

mistake post~ please remove Message was edited by: rstrandmark
Message 9 of 15
Anonymous
in reply to: Anonymous

Hi Fatty,
I appreciate your work. But this code is to know the no. of points which our ray is going to intersect. I am also having the same code ( somewhat different but trust me it is working pretty good 🙂 ). My question is not to know ..that at how many points a ray is going to intersect...My question is...why my program is giving the intersection point whereas if we see in AutoCAD the ray is not intersectiing. I had already described the situation in my main post.
...Might be this time your help can be helful. But I really appreciate your labour you did for me.
Thanks Fatty
Message 10 of 15
Anonymous
in reply to: Anonymous

Tanks rstrandmark for your reply
Message 11 of 15
Anonymous
in reply to: Anonymous

Anshu
Shame on me but I don't know why
it's happens with points in AutoCAD,
Sorry

~'J'~
Message 12 of 15
sonsun179
in reply to: Anonymous

Hi, J,

Is there any method the find out the coordinates(x, y) of the intersection point of ray and polygon?

Please help me to get the coordinates.

 

Thanks in advance,

 

SS

Message 13 of 15
Ed.Jobe
in reply to: sonsun179

The IntersectWith method returns an array of doubles representing a 3D point.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Message 14 of 15
sonsun179
in reply to: Ed.Jobe

Hi, Ed,

Thanks for your kind response,

I am looking for the method to get the coordinates(x, y) of the multiple intersection with polygon using Ray,

in the situation that there are multiple circles located regularly with consistant distance within the irregular polygon,

I would like to get the coordinate of the intersection with polygon for each circle using Ray as shown in the drawing 

attached.

I have the VBA code to get the coordinate of  the intersection with polyline selecting ray and polyline each, 

but not with polygon,

so, please help me with a sample VBA code so that I can get the distance from the circle to the intersection point

for the multiple case.

I appreciate your kind cooperations,

 

SS

Message 15 of 15
Ed.Jobe
in reply to: sonsun179

That's not a polygon, it's a polyline. A polygon is a regular shape like a square or octagon, circumscribed about a circle. All the sides are equal.

 

The return for the IntersectWith method is an array of xyz coordinates. If it finds 5 intersections, then the array dimensions will by 0 to 14.

 

It is helpful if you post your code. I don't know what have so far.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost