Question
Montecarlo Integration: Area of an Irregular Polygon Write a Sub-Program to verify if a point to be check is inside or outside the polygon. I
Montecarlo Integration: Area of an Irregular Polygon
Write a Sub-Program to verify if a point to be check is inside or outside the polygon.
I found a VBA code and I do not have the idea how to apply in the excel template (picture above). We used in class Arrays, Nested For loop, and Nested If Statement, but I don't see that they are using any of that in the VBA code (showed below).
If there is any other way to test a point inside or outside the polygon, please show me the VBA code using as reference the excel template. Or show me how to use the VBA code that I am attaching in this question thanks.
Function lnside(x_values, y_values, x_point, y_point) As Boolean
Dim N As Integer, J As Integer, C As Integer Dim YC As Double
N = x_values.Count 'Does figure have closure?
If x_values(1) x_values(N) Or y_values(1) y_values(N) Then
Inside = CVErr(xlErrValue):
Exit Function
For J = 1 To N -1
If x_values(J).Formula ="" Or y_values(J).Formula = "" Then
Inside = _ CVErr(xlErrValue):
Exit Function 'Exit if cell is blank
'Both ends of segment to left of point?
If x_point >= x_values(J) And x_point > x_values(J + 1) Then
GoTo EOL 'Both ends of segment to right of point?
If x_point
GoTo EOL 'Both ends of segment below point ?
If y_point >= y_values(J) And y_point > y_values(J + 1) Then
GoTo EOL 'If came here, one or both ends of the segment are above the point.
'Calculate the y coordinate where the "ray" crosses the segment.
YC = y_values(J + 1) + (y_values(J) - y_values(J + 1)) * (x_point - x_values(J + 1)) / (x_values(J) - x_values(J + 1)) 'if the crossing is above the point then add one to the count
If YC - y_point > 0
Then C = C + 1
EOL: Next J
Inside = C Mod 2
End Function
ADDITIONAL INFORMATION:
The question is to find a VBA code that applies the excel template in which a point can be tested if is inside or outside the polygon.
The procedure to test whether a point xa, yA lies within the figure is as follows:
For each of the N edges that make up the figure:
(1) If the x coordinates of both ends of the edge lie to the left of xA, then go to the next edge.
(2) If the x coordinates of both ends of the edge are to the right of xA, then go to the next edge.
(3) If the y coordinates of both ends of the edge are below yA, then go to the next edge.
(4) If none of the above is true, the y coordinates of one or both ends of the edge are above the point. Determine the y coordinate of the "crossing point" where the vertical ray and the edge cross, using the formula
Yc=yleft + [ (yright - yleft ) *(xA - xleft ) ] / [ xright - xleft ]
(5) If yc > yA, the ray crosses the edge of the polygon, so add one to the number of crossings found, and go to the next edge.
(6) When all N edges have been evaluated, if the number of crossings is odd, the point lies inside the figure.
Specific Requirement:
Maximum of 8 edges. Use the input box function to prompt the user to insert the number of edges as well the coordinates for polygon.
Maximun of 20 points to evaluate (inside or outside). Use the input box function to prompt the user to insert the number of points as well the coordinates for points.
The program should be able to classify whether "the point is outside the area" or "the point is inside the area"
specify the points (xy) which are inside the area.
Use Arrays and RC format.
R38C14 of edRes of the Irregular PohR No, to Check 2 7 19 The point is inside the areaStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started