Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Dim as Point
For a bit of code where the author defines a function like this:
Function pnpoly(npol As Integer, poly() as Point, x as Integer, Y as Integer) as Boolean, I am pretty sure the poly() input defined "as Point" is looking for an xy point value for an edge of a polygon, but I am unsure how it is derived. Any ideas? My thanks in advance for any comments or suggestions... this is a function to identify whether an x-y data point falls inside of or outside of a convex polygon. Cheers! Brad |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Dim as Point
Tom, thanks, but no, it has no declaration for "Point".... this is actually
just a bit of code that someone posted in response to a question regarding determining whether an x-y point on a graph is inside or outside of a convex polygon outlined on the same x-y graph. The whole thing looks like this: Function pnpoly(npol As Integer, poly() As Point, X As Integer, Y As Integer) As Boolean Dim inpoly As Boolean Dim i, j As Integer i = 0 j = npol - 1 inpoly = False While (i < npol) If ((((poly(i).Y <= Y) And (Y < poly(j).Y)) Or _ ((poly(j).Y <= Y) And (Y < poly(i).Y)))) Then If (X < (poly(j).X - poly(i).X) * (Y - poly(i).Y) / (poly(j).Y - poly(i).Y) + poly(i).X) Then inpoly = Not (inpoly) End If End If j = i i = i + 1 Wend pnpoly = inpoly End Function Now, what I was actually trying to do was take Andy Pope's interesting "bounding areas" chart where he draws bounding lines around two overlapping convex polygons, and simply count the number of x-y points inside of and outside of a polygon. So, I hunted around and found this code, referred to generally as the crossing number algorithm, for just this sort of thing. Most often what I found was listed as pseudo-code, but in this one case, a snippet of VBA code. On the psuedo-code, it does have: typedef struct (int x, y;) Point; and I am certain this is most likely what you asked about. I am still not sure how to apply it in the function above.... thanks, Brad "Tom Ogilvy" wrote in message ... Does he have a declaration at the top like: Private Type POINT x As Long y As Long End Type Then point would be a structure that contains both and x and y coordinate. with the declaration at the top of the module, then Usage example: Dim a() as point for i = 1 to 10 a(i).x = int(rnd()*100 + 1) a(i).y = int(rnd()*50 + 1) Next -- Regards, Tom Ogilvy "Brad" wrote: For a bit of code where the author defines a function like this: Function pnpoly(npol As Integer, poly() as Point, x as Integer, Y as Integer) as Boolean, I am pretty sure the poly() input defined "as Point" is looking for an xy point value for an edge of a polygon, but I am unsure how it is derived. Any ideas? My thanks in advance for any comments or suggestions... this is a function to identify whether an x-y data point falls inside of or outside of a convex polygon. Cheers! Brad |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Dim as Point
I would see it used like this:
Option Explicit Option Base 0 Private Type POINT x As Double y As Double End Type Sub Main() Dim XP, YP, XP1, YP1 Dim poly() As POINT Dim b As Boolean Dim x As Double, y As Double Dim i As Long, j As Long ReDim poly(0 To 19) ' Polygon XP = Array(0#, 1#, 1#, 0#, 0#, 1#, _ -0.5, -1#, -1#, -2#, -2.5, _ -2#, -1.5, -0.5, 1#, 1#, 0#, _ -0.5, -1#, -0.5) YP = Array(0#, 0#, 1#, 1#, 2#, 3#, 2#, _ 3#, 0#, -0.5, -1#, -1.5, -2#, _ -2#, -1.5, -1#, -0.5, -1#, _ -1#, -0.5) ' Test Points XP1 = Array(0.5, 0.5, -0.5, 0.75, 0, _ -0.5, -1, -1.5, -2.25, 0.5, 0.5, -0.5) YP1 = Array(0.5, 1.5, 1.5, 2.25, 2.01, _ 2.5, -0.5, 0.5, -1, -0.25, -1.25, -2.5) ' writing to the worksheet is not required ' but supports graphing the data to verify Cells(2,1) = "XP" : Cells(3, 1) = "YP" Cells(4,1) = "XP1" : Cells(5,1) = "YP1" Cells(2, 2).Resize(1, 20) = XP Cells(3, 2).Resize(1, 20) = YP Cells(4, 2).Resize(1, 12) = XP1 Cells(5, 2).Resize(1, 12) = YP1 j = 0 For i = LBound(XP) To UBound(XP) poly(j).x = XP(i) poly(j).y = YP(i) j = j + 1 Next j = 1 For i = LBound(XP1) To UBound(XP1) x = XP1(i): y = YP1(i) b = pnpoly(20, poly, x, y) Cells(6, j).Offset(0, 1) = b j = j + 1 Next End Sub Function pnpoly(npol As Integer, poly() As POINT, _ x As Double, y As Double) As Boolean Dim inpoly As Boolean Dim i As Integer, j As Integer i = 0 j = npol - 1 inpoly = False While (i < npol) If ((poly(i).y <= y) And (y < poly(j).y)) Or _ ((poly(j).y <= y) And (y < poly(i).y)) Then If x < (poly(j).x - poly(i).x) * (y - poly(i).y) / _ (poly(j).y - poly(i).y) + poly(i).x Then inpoly = Not (inpoly) End If End If j = i i = i + 1 Wend pnpoly = inpoly End Function Note that I have changed the function a little to support the data used. (integer=double) See description at: http://tinyurl.com/y93rhr or "spelled out" http://209.85.165.104/search?q=cache...ct=clnk& cd=5 -- Regards, Tom Ogilvy "Brad" wrote in message ... Tom, thanks, but no, it has no declaration for "Point".... this is actually just a bit of code that someone posted in response to a question regarding determining whether an x-y point on a graph is inside or outside of a convex polygon outlined on the same x-y graph. The whole thing looks like this: Function pnpoly(npol As Integer, poly() As Point, X As Integer, Y As Integer) As Boolean Dim inpoly As Boolean Dim i, j As Integer i = 0 j = npol - 1 inpoly = False While (i < npol) If ((((poly(i).Y <= Y) And (Y < poly(j).Y)) Or _ ((poly(j).Y <= Y) And (Y < poly(i).Y)))) Then If (X < (poly(j).X - poly(i).X) * (Y - poly(i).Y) / (poly(j).Y - poly(i).Y) + poly(i).X) Then inpoly = Not (inpoly) End If End If j = i i = i + 1 Wend pnpoly = inpoly End Function Now, what I was actually trying to do was take Andy Pope's interesting "bounding areas" chart where he draws bounding lines around two overlapping convex polygons, and simply count the number of x-y points inside of and outside of a polygon. So, I hunted around and found this code, referred to generally as the crossing number algorithm, for just this sort of thing. Most often what I found was listed as pseudo-code, but in this one case, a snippet of VBA code. On the psuedo-code, it does have: typedef struct (int x, y;) Point; and I am certain this is most likely what you asked about. I am still not sure how to apply it in the function above.... thanks, Brad "Tom Ogilvy" wrote in message ... Does he have a declaration at the top like: Private Type POINT x As Long y As Long End Type Then point would be a structure that contains both and x and y coordinate. with the declaration at the top of the module, then Usage example: Dim a() as point for i = 1 to 10 a(i).x = int(rnd()*100 + 1) a(i).y = int(rnd()*50 + 1) Next -- Regards, Tom Ogilvy "Brad" wrote: For a bit of code where the author defines a function like this: Function pnpoly(npol As Integer, poly() as Point, x as Integer, Y as Integer) as Boolean, I am pretty sure the poly() input defined "as Point" is looking for an xy point value for an edge of a polygon, but I am unsure how it is derived. Any ideas? My thanks in advance for any comments or suggestions... this is a function to identify whether an x-y data point falls inside of or outside of a convex polygon. Cheers! Brad |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Dim as Point
Tom, thanks so much! Brad
"Tom Ogilvy" wrote in message ... I would see it used like this: Option Explicit Option Base 0 Private Type POINT x As Double y As Double End Type Sub Main() Dim XP, YP, XP1, YP1 Dim poly() As POINT Dim b As Boolean Dim x As Double, y As Double Dim i As Long, j As Long ReDim poly(0 To 19) ' Polygon XP = Array(0#, 1#, 1#, 0#, 0#, 1#, _ -0.5, -1#, -1#, -2#, -2.5, _ -2#, -1.5, -0.5, 1#, 1#, 0#, _ -0.5, -1#, -0.5) YP = Array(0#, 0#, 1#, 1#, 2#, 3#, 2#, _ 3#, 0#, -0.5, -1#, -1.5, -2#, _ -2#, -1.5, -1#, -0.5, -1#, _ -1#, -0.5) ' Test Points XP1 = Array(0.5, 0.5, -0.5, 0.75, 0, _ -0.5, -1, -1.5, -2.25, 0.5, 0.5, -0.5) YP1 = Array(0.5, 1.5, 1.5, 2.25, 2.01, _ 2.5, -0.5, 0.5, -1, -0.25, -1.25, -2.5) ' writing to the worksheet is not required ' but supports graphing the data to verify Cells(2,1) = "XP" : Cells(3, 1) = "YP" Cells(4,1) = "XP1" : Cells(5,1) = "YP1" Cells(2, 2).Resize(1, 20) = XP Cells(3, 2).Resize(1, 20) = YP Cells(4, 2).Resize(1, 12) = XP1 Cells(5, 2).Resize(1, 12) = YP1 j = 0 For i = LBound(XP) To UBound(XP) poly(j).x = XP(i) poly(j).y = YP(i) j = j + 1 Next j = 1 For i = LBound(XP1) To UBound(XP1) x = XP1(i): y = YP1(i) b = pnpoly(20, poly, x, y) Cells(6, j).Offset(0, 1) = b j = j + 1 Next End Sub Function pnpoly(npol As Integer, poly() As POINT, _ x As Double, y As Double) As Boolean Dim inpoly As Boolean Dim i As Integer, j As Integer i = 0 j = npol - 1 inpoly = False While (i < npol) If ((poly(i).y <= y) And (y < poly(j).y)) Or _ ((poly(j).y <= y) And (y < poly(i).y)) Then If x < (poly(j).x - poly(i).x) * (y - poly(i).y) / _ (poly(j).y - poly(i).y) + poly(i).x Then inpoly = Not (inpoly) End If End If j = i i = i + 1 Wend pnpoly = inpoly End Function Note that I have changed the function a little to support the data used. (integer=double) See description at: http://tinyurl.com/y93rhr or "spelled out" http://209.85.165.104/search?q=cache...ct=clnk& cd=5 -- Regards, Tom Ogilvy "Brad" wrote in message ... Tom, thanks, but no, it has no declaration for "Point".... this is actually just a bit of code that someone posted in response to a question regarding determining whether an x-y point on a graph is inside or outside of a convex polygon outlined on the same x-y graph. The whole thing looks like this: Function pnpoly(npol As Integer, poly() As Point, X As Integer, Y As Integer) As Boolean Dim inpoly As Boolean Dim i, j As Integer i = 0 j = npol - 1 inpoly = False While (i < npol) If ((((poly(i).Y <= Y) And (Y < poly(j).Y)) Or _ ((poly(j).Y <= Y) And (Y < poly(i).Y)))) Then If (X < (poly(j).X - poly(i).X) * (Y - poly(i).Y) / (poly(j).Y - poly(i).Y) + poly(i).X) Then inpoly = Not (inpoly) End If End If j = i i = i + 1 Wend pnpoly = inpoly End Function Now, what I was actually trying to do was take Andy Pope's interesting "bounding areas" chart where he draws bounding lines around two overlapping convex polygons, and simply count the number of x-y points inside of and outside of a polygon. So, I hunted around and found this code, referred to generally as the crossing number algorithm, for just this sort of thing. Most often what I found was listed as pseudo-code, but in this one case, a snippet of VBA code. On the psuedo-code, it does have: typedef struct (int x, y;) Point; and I am certain this is most likely what you asked about. I am still not sure how to apply it in the function above.... thanks, Brad "Tom Ogilvy" wrote in message ... Does he have a declaration at the top like: Private Type POINT x As Long y As Long End Type Then point would be a structure that contains both and x and y coordinate. with the declaration at the top of the module, then Usage example: Dim a() as point for i = 1 to 10 a(i).x = int(rnd()*100 + 1) a(i).y = int(rnd()*50 + 1) Next -- Regards, Tom Ogilvy "Brad" wrote: For a bit of code where the author defines a function like this: Function pnpoly(npol As Integer, poly() as Point, x as Integer, Y as Integer) as Boolean, I am pretty sure the poly() input defined "as Point" is looking for an xy point value for an edge of a polygon, but I am unsure how it is derived. Any ideas? My thanks in advance for any comments or suggestions... this is a function to identify whether an x-y data point falls inside of or outside of a convex polygon. Cheers! Brad |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Data point on line is not over the point/tick in X axis... | Charts and Charting in Excel | |||
X Y Scatter With Point name on each point ? | Charts and Charting in Excel | |||
I need more general XY point to point plotting than XY scatter in | Charts and Charting in Excel | |||
red point | Excel Programming | |||
averaging from a fixed point to a non-fixed point? | Excel Programming |