以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  这段代码在狐表要怎么执行,需要引入什么?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=192832)

--  作者:zhenghangbo
--  发布时间:2024/7/25 13:49:00
--  这段代码在狐表要怎么执行,需要引入什么?
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
--  作者:有点蓝
--  发布时间:2024/7/25 14:05:00
--  
添加这个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

--  作者:zhenghangbo
--  发布时间:2024/7/25 14:27:00
--  
For i As Integer = 0, j = polygon.Count - 1 To polygon.Count - 1
这句代码语法错误

--  作者:有点蓝
--  发布时间:2024/7/25 14:32:00
--  
dim j = polygon.Count - 1
For i As Integer = 0 To polygon.Count - 1