[VBA]判断点px是否在多边形内
<FONT class=red_3 size=3><B>判断点px是否在多边形内</B></FONT> <FONT class=htd id=goodfont1 style="FONT-SIZE: 9pt">document.write ( code_jk_my("'判断点px是否在多边形内;<BR>'即可适用于凹多边形的判断,也适用于凸多边形的判断<BR>'所选射线px(x0 y0)--pxy(x0+2*max|x0-xi| y0+min|y0-yi|) 不与多边形任何一顶点相交<BR>'入口参数多边形:(n, ptx(), pty(), px , py)<BR>'返回值False (在多边形外)、True(在多边形上及在多边形内)<BR>Public Function dzdbxn(n As Long, ptx() As Double, pty() As Double, px As Double, py As Double) As Boolean<BR>Dim j As Long<BR>Dim d1 As Double<BR>Dim d2 As Double<BR>Dim d3 As Double<BR><BR>dzdbxn = False<BR><BR>For i = 1 To n<BR>j = i + 1: If i = n Then j = 1<BR>d1 = Abs(ptx(i) * pty(j) + ptx(j) * py + px * pty(i) - ptx(i) * py - ptx(j) * pty(i) - px * pty(j))<BR>d2 = Pold(ptx(i), pty(i), ptx(j), pty(j))<BR>d3 = Abs(d2 - Pold(ptx(i), pty(i), px, py) - Pold(ptx(j), pty(j), px, py))<BR>d1 = d1 / d2<BR>Print "i=" + Str(i) + " j=" + Str(j) + " d1=" + Str(d1) + " d3=" + Str(d3)<BR>'注意:d1 d3判断值1前的0个数=多边形区域坐标值中小数位数-1<BR>If d1 < 0.0001 And d3 < 0.0001 Then dzdbxn = True: Exit Function<BR>Next i<BR><BR>If dzdbxn = False Then<BR>Dim dx As Double<BR>Dim xmax As Double<BR>Dim dy As Double<BR>Dim ymin As Double<BR><BR>For i = 1 To n<BR>dx = Abs(ptx(i) - px): dy = Abs(pty(i) - py)<BR>If i = 1 Then<BR>xmax = dx: ymin = dy<BR>Else<BR>If dx > xmax Then xmax = dx<BR>If dy < ymin Then ymin = dy<BR>End If<BR>Next i<BR><BR>Dim sum As Long<BR><BR>sum = 0: xmax = 2# * xmax<BR>For i = 1 To n<BR>j = i + 1: If i = n Then j = 1<BR>d1 = ymin * (ptx(j) - ptx(i)) - xmax * (pty(j) - pty(i))<BR>d2 = xmax * (pty(i) - py) - ymin * (ptx(i) - px)<BR>d3 = (ptx(j) - ptx(i)) * (pty(i) - py) - (pty(j) - pty(i)) * (ptx(i) - px)<BR>If (d2 * (d1 - d2)) >= 0# And d3 * d1 >= 0# Then sum = sum + 1<BR>Next i<BR>'Print "sum=" + Str(sum) + " px=" + Str(px) + " py=" + Str(py)<BR>If sum > 0 And sum <> 2 * Int(sum / 2) Then<BR>dzdbxn = True<BR>Else<BR>dzdbxn = False<BR>End If<BR>End If<BR>End Function<BR>Function Pold(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double<BR>'两点间距离计算<BR>Pold = Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))<BR>End Function<BR>")); '判断点px是否在多边形内;<BR>'即可适用于凹多边形的判断,也适用于凸多边形的判断<BR>'所选射线px(x0 y0)--pxy(x0+2*max|x0-xi| y0+min|y0-yi|) 不与多边形任何一顶点相交<BR>'入口参数多边形:(n, ptx(), pty(), px , py)<BR>'返回值False (在多边形外)、True(在多边形上及在多边形内)<BR>Public Function dzdbxn(n As Long, ptx() As Double, pty() As Double, px As Double, py As Double) As Boolean<BR> Dim j As Long<BR> Dim d1 As Double<BR> Dim d2 As Double<BR> Dim d3 As Double<BR> <BR> dzdbxn = False<BR> <BR> For i = 1 To n<BR> j = i + 1: If i = n Then j = 1<BR> d1 = Abs(ptx(i) * pty(j) + ptx(j) * py + px * pty(i) - ptx(i) * py - ptx(j) * pty(i) - px * pty(j))<BR> d2 = Pold(ptx(i), pty(i), ptx(j), pty(j))<BR> d3 = Abs(d2 - Pold(ptx(i), pty(i), px, py) - Pold(ptx(j), pty(j), px, py))<BR> d1 = d1 / d2<BR> Print "i=" + Str(i) + " j=" + Str(j) + " d1=" + Str(d1) + " d3=" + Str(d3)<BR> '注意:d1 d3判断值1前的0个数=多边形区域坐标值中小数位数-1<BR> If d1 < 0.0001 And d3 < 0.0001 Then dzdbxn = True: Exit Function<BR> Next i<BR> <BR> If dzdbxn = False Then<BR> Dim dx As Double<BR> Dim xmax As Double<BR> Dim dy As Double<BR> Dim ymin As Double<BR> <BR> For i = 1 To n<BR> dx = Abs(ptx(i) - px): dy = Abs(pty(i) - py)<BR> If i = 1 Then<BR> xmax = dx: ymin = dy<BR> Else<BR> If dx > xmax Then xmax = dx<BR> If dy < ymin Then ymin = dy<BR> End If<BR> Next i<BR> <BR> Dim sum As Long<BR> <BR> sum = 0: xmax = 2# * xmax<BR> For i = 1 To n<BR> j = i + 1: If i = n Then j = 1<BR> d1 = ymin * (ptx(j) - ptx(i)) - xmax * (pty(j) - pty(i))<BR> d2 = xmax * (pty(i) - py) - ymin * (ptx(i) - px)<BR> d3 = (ptx(j) - ptx(i)) * (pty(i) - py) - (pty(j) - pty(i)) * (ptx(i) - px)<BR> If (d2 * (d1 - d2)) >= 0# And d3 * d1 >= 0# Then sum = sum + 1<BR> Next i<BR> 'Print "sum=" + Str(sum) + " px=" + Str(px) + " py=" + Str(py)<BR> If sum > 0 And sum <> 2 * Int(sum / 2) Then<BR> dzdbxn = True<BR> Else<BR> dzdbxn = False<BR> End If<BR> End If<BR>End Function<BR>Function Pold(x1 As Double, y1 As Double, x2 As Double, y2 As Double) As Double<BR> '两点间距离计算<BR> Pold = Sqr((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))<BR>End Function<BR></FONT> 有一个更简单,以多边形做一个面域,再以点为圆心做一个很小的圆,取二者并集,若并后的面积与多边形面一样大则点在多边形内,若并后的面积等于多形面积加上圆的面积则点在其外,否则在多边形线上!
页:
[1]