polylines- ??? Urgent

polylines- ??? Urgent

Anonymous
Not applicable
458 Views
10 Replies
Message 1 of 11

polylines- ??? Urgent

Anonymous
Not applicable
Hi,

I have to check if a point lies inside a closed polyline.

The suggested soln was ,

Consider a polygon made up of N vertices (xi,yi) where i ranges from 0 to
N-1. The last vertex (xN,yN) is assumed to be the same as the first vertex
(x0,y0), that is, the polygon is closed. To determine the status of a point
(xp,yp) consider a horizontal ray emanating from (xp,yp) and to the right.
If the number of times this ray intersects the line segments making up the
polygon is even then the point is outside the polygon. Whereas if the number
of intersections is odd then the point (xp,yp) lies inside the polygon. good
luck


This is fine. but when i use AddRay to create a ray, i dont understand how
to specify the second point and how to specify the direction (right
direction) ?

Any help is appreciated.

Thanks,

TI
0 Likes
459 Views
10 Replies
Replies (10)
Message 2 of 11

Anonymous
Not applicable
The second point would be to the right of the first point:

Sub Example_AddRay(x,y,z)
' This example creates a ray in model space.

Dim rayObj As AcadRay
Dim basePoint(0 To 2) As Double
Dim SecondPoint(0 To 2) As Double

' Define the ray
basePoint(0) = x: basePoint(1) = y: basePoint(2) = z
SecondPoint(0) = x+5: SecondPoint(1) = y: SecondPoint(2) = z

' Creates a Ray object in model space
Set rayObj = ThisDrawing.ModelSpace.AddRay(basePoint, SecondPoint)
ZoomAll

End Sub
0 Likes
Message 3 of 11

Anonymous
Not applicable
this may help, I found it one day wbdesign.net a couple years ago, I've never used it but who knows you may be able to use it

Option Explicit

Private Const INSIDE = 0
Private Const OUTSIDE = 1

Public Type Point
x As Double
y As Double
End Type

Private Function MIN(x, y)
If x < y Then MIN = x Else MIN = y
End Function

Private Function MAX(x, y)
If x > y Then MAX = x Else MAX = y
End Function

Public Function InsidePolygon(polygon() As Point, N As Long, p As Point)
Dim counter As Long
Dim i As Long
Dim xinters As Double
Dim p1 As Point, p2 As Point
p1 = polygon(0)
For i = 1 To N Step 1
p2 = polygon(i Mod N)
If (p.y > MIN(p1.y, p2.y)) Then
If (p.y <= MAX(p1.y, p2.y)) Then
If (p.x <= MAX(p1.x, p2.x)) Then
If (p1.y <> p2.y) Then
xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x
If ((p1.x = p2.x) Or (p.x <= xinters)) Then
counter = counter + 1
End If
End If
End If
End If
End If
p1 = p2
Next
If (counter Mod 2 = 0) Then
InsidePolygon = OUTSIDE
Else
InsidePolygon = INSIDE
End If
End Function
0 Likes
Message 4 of 11

Anonymous
Not applicable
How about this:

Public Sub CheckPolygon()
Dim rayObj As AcadRay
Dim pnt As Variant
Dim spt(0 To 2) As Double

Dim PickObj As AcadEntity
On Error GoTo quitthis
Do
ThisDrawing.Utility.GetEntity PickObj, pnt, vbCrLf & "Select a polygon"
ThisDrawing.Utility.Prompt PickObj.ObjectName
Loop Until PickObj.ObjectName = "AcDbPolyline"

Do
pnt = ThisDrawing.Utility.GetPoint(, vbCrLf & "Enter a point: ")
ThisDrawing.Utility.Prompt IsInside(PickObj, pnt)
Loop While UBound(pnt)

quitthis:

End Sub

Private Function IsInside(obj As AcadEntity, pt As Variant) As Boolean
Dim pt2(0 To 2) As Double, ptlist As Variant

pt2(0) = pt(0) + 4
pt2(1) = pt(1)
pt2(2) = pt(2)
' Creates a Ray object in model space
Set rayObj = ThisDrawing.ModelSpace.AddRay(pt, pt2)
' determine the intersection points
ptlist = rayObj.IntersectWith(obj, acExtendNone)
rayObj.Delete
If VarType(ptlist) <> vbEmpty Then
IsInside = ((UBound(ptlist) + 1) Mod 6 = 3)
End If
End Function
0 Likes
Message 5 of 11

Anonymous
Not applicable
Hi,

One further thing to watch for is the special case where the Point is on the
Polyline.

You have to decide how you are going to both detect and handle that case.


--


Laurie Comerford
CADApps
www.cadapps.com.au

"10west" <10west@sprynet.com> wrote in message
news:f198736.1@WebX.maYIadrTaRb...
> this may help, I found it one day wbdesign.net a couple years ago, I've
never used it but who knows you may be able to use it
> Option Explicit
>
> Private Const INSIDE = 0
> Private Const OUTSIDE = 1
>
> Public Type Point
> x As Double
> y As Double
> End Type
>
> Private Function MIN(x, y)
> If x < y Then MIN = x Else MIN = y
> End Function
>
> Private Function MAX(x, y)
> If x > y Then MAX = x Else MAX = y
> End Function
>
> Public Function InsidePolygon(polygon() As Point, N As Long, p As Point)
> Dim counter As Long
> Dim i As Long
> Dim xinters As Double
> Dim p1 As Point, p2 As Point
> p1 = polygon(0)
> For i = 1 To N Step 1
> p2 = polygon(i Mod N)
> If (p.y > MIN(p1.y, p2.y)) Then
> If (p.y <= MAX(p1.y, p2.y)) Then
> If (p.x <= MAX(p1.x, p2.x)) Then
> If (p1.y <> p2.y) Then
> xinters = (p.y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y) + p1.x
> If ((p1.x = p2.x) Or (p.x <= xinters)) Then
> counter = counter + 1
> End If
> End If
> End If
> End If
> End If
> p1 = p2
> Next
> If (counter Mod 2 = 0) Then
> InsidePolygon = OUTSIDE
> Else
> InsidePolygon = INSIDE
> End If
> End Function
>
0 Likes
Message 6 of 11

Anonymous
Not applicable
Also should consider whether or not the polyline is open or closed.....
0 Likes
Message 7 of 11

Anonymous
Not applicable
An earlier post on this subject commented that a closed polygon will have
the terminating coords = to the started coords. Maybe, but not on a closed
polyline. I have found that there are coordinates in the polyline defintion
for each individual vertex. Setting the 'closed' property of a polyline to
true does just that, sets a property and does not add another point.. I
have gotten around this by 'opening' a closed polyline and adding a new
vertex at the end = to the starting point.

Allen Johnson wrote:

> Also should consider whether or not the polyline is open or closed.....
0 Likes
Message 8 of 11

Anonymous
Not applicable
Hi , my name's Lorenzo and I have a question for you that I think is similar to the one you already answered:
I have a red closed polyline representing the perimeter of a house. Now I draw a couple of green lines inside the red one in order to represent the various rooms of the house. Now my goal is to compute all the building costs for each room, and in order to do this I have to let autocad recognize the various polygons obtained from the intersections of the green lines and delimited by the red polyline. Please can you help me in someway! I'm quite new to autocad, so I feel pretty lost!
Lorenzo
0 Likes
Message 9 of 11

Anonymous
Not applicable
I needed to do something similar. My solution was to iterate through each of the vertices of both (in your case) the red polyline and the green lines, choosing points slightly NE/NW/SE/SW of the vertex, determining if they lie within the red polyline, and if so doing a Boundary function to identify a room. Of course this produces duplicate results so you have to check as you go if you've identified this room previously.



This seemed a bit clunky to me but it did the job. I'd be interested if anybody can suggest anything more efficient.



Regards



Wayne Ivory

IT Analyst Programmer

Wespine Industries Pty Ltd
0 Likes
Message 10 of 11

Anonymous
Not applicable
Hi I see you used the boundary function.
I need to use it but I don't find the vba sintaxys of this method in the help. Would you please send the syntaxis?
Since I know the boundary command, I'm using: ThisDrawing.SendCommand "-boundary " & rst!x & "," & rst!y & " "
but I know "You should never use this method(sendcommand) to issue a command for which there is an ActiveX method available. " I need to know ir there is an ActiveX method for bondary
Thanx
Villaco
Villaco
0 Likes
Message 11 of 11

Anonymous
Not applicable
Nope, I'm afraid I'm doing the same as what you're doing because there isn't a VBA command for Boundary.



Regards



Wayne
0 Likes