Imports System.Numerics
Module Module1
Sub Main()
' 定义多边形的顶点
Dim polygonPoints As New List(Of PointLatLng) From {
New PointLatLng(40.7128, -74.0060),
New PointLatLng(40.7128, -74.0050),
New PointLatLng(40.7118, -74.0050),
New PointLatLng(40.7118, -74.0060),
New PointLatLng(40.7128, -74.0060)
}
' 待检测的点
Dim testPoint As New PointLatLng(40.7125, -74.0055)
' 检查点是否在多边形内
Dim isInside As Boolean = IsPointInPolygon(testPoint, polygonPoints)
Console.WriteLine($"The point ({testPoint.Latitude}, {testPoint.Longitude}) is inside the polygon? {isInside}")
Console.ReadLine()
End Sub
Public Class PointLatLng
Public Property Latitude As Double
Public Property Longitude As Double
Public Sub New(latitude As Double, longitude As Double)
Me.Latitude = latitude
Me.Longitude = longitude
End Sub
End Class
Public Function IsPointInPolygon(ByVal point As PointLatLng, ByVal polygon As List(Of PointLatLng)) As Boolean
Dim x As Double = point.Longitude
Dim y As Double = point.Latitude
Dim inside As Boolean = False
For i As Integer = 0, j = polygon.Count - 1 To polygon.Count - 1
Dim xi As Double = polygon(i).Longitude
Dim yi As Double = polygon(i).Latitude
Dim xj As Double = polygon(j).Longitude
Dim yj As Double = polygon(j).Latitude
If ((yi > y) <> (yj > y)) AndAlso (x < (xj - xi) * (y - yi) / (yj - yi) + xi) Then
inside = Not inside
End If
j = i
Next
Return inside
End Function
End Module
添加这个dll的引用:System.Numerics.dll
全局代码
Public Class PointLatLng
Public Property Latitude As Double
Public Property Longitude As Double
Public Sub New(latitude As Double, longitude As Double)
Me.Latitude = latitude
Me.Longitude = longitude
End Sub
End Class
Public Function IsPointInPolygon(ByVal point As PointLatLng, ByVal polygon As List(Of PointLatLng)) As Boolean
Dim x As Double = point.Longitude
Dim y As Double = point.Latitude
Dim inside As Boolean = False
For i As Integer = 0, j = polygon.Count - 1 To polygon.Count - 1
Dim xi As Double = polygon(i).Longitude
Dim yi As Double = polygon(i).Latitude
Dim xj As Double = polygon(j).Longitude
Dim yj As Double = polygon(j).Latitude
If ((yi > y) <> (yj > y)) AndAlso (x < (xj - xi) * (y - yi) / (yj - yi) + xi) Then
inside = Not inside
End If
j = i
Next
Return inside
End Function
窗口按钮
Dim polygonPoints As New List(Of PointLatLng) From {
New PointLatLng(40.7128, -74.0060),
New PointLatLng(40.7128, -74.0050),
New PointLatLng(40.7118, -74.0050),
New PointLatLng(40.7118, -74.0060),
New PointLatLng(40.7128, -74.0060)
}
' 待检测的点
Dim testPoint As New PointLatLng(40.7125, -74.0055)
' 检查点是否在多边形内
Dim isInside As Boolean = IsPointInPolygon(testPoint, polygonPoints)
if isInside
output.show("待检测的点在多边形内")
endif
For i As Integer = 0, j = polygon.Count - 1 To polygon.Count - 1
这句代码语法错误
dim
j = polygon.Count - 1For i As Integer = 0 To polygon.Count - 1