- 积分
- 1609
- 明经币
- 个
- 注册时间
- 2003-8-6
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
问题的提出:
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=13701
代码:
- Option Explicit
- Const CLEARCMD = vbCr & " " & vbCr
- Dim Pi As Double
- Public Sub LinePointArray()
- Dim PointArray() As Double, PointIndex() As Integer, CurPIndex As Integer
- Dim i As Integer, N As Integer
- Dim TempPoint As Variant, Temp As Variant, TempIndex As Integer
- Dim StartPoint(0 To 2) As Double
- Dim TempAngle As Double
- Dim ii As Integer
- Dim BasePoint(0 To 2) As Double, SecendPoint(0 To 2) As Double
- Dim P1(0 To 2) As Double, P2(0 To 2) As Double
- Dim BaseAngle As Double, Direction As Integer
-
- Pi = Atn(1) * 4
- On Error Resume Next
- 'Open "D:\Test.txt" For Output As #1
- Do
- ReDim Preserve PointArray(0 To 2, N)
- TempPoint = ThisDrawing.Utility.GetPoint(, CLEARCMD & "请选择点(" & N & "):")
- If Err Then
- Err.Clear
- N = N - 1
- If N <= 0 Then Exit Sub
- Exit Do
- End If
- PointArray(0, N) = TempPoint(0)
- PointArray(1, N) = TempPoint(1)
- ThisDrawing.ModelSpace.AddText N, TempPoint, 5
- 'Print #1, "点" & N & ":" & TempPoint(0) & "," & TempPoint(1)
- N = N + 1
- Loop
-
- On Error GoTo 0
- Temp = PointArray(0, 0)
- For i = 1 To N
- If Temp >= PointArray(0, i) Then
- Temp = PointArray(0, i)
- TempIndex = i
- End If
- Next i
- StartPoint(0) = PointArray(0, TempIndex)
- StartPoint(1) = PointArray(1, TempIndex)
- BasePoint(0) = StartPoint(0)
- BasePoint(1) = StartPoint(1)
- CurPIndex = TempIndex
- ReDim PointIndex(0)
- PointIndex(0) = TempIndex
- Direction = 1
- BaseAngle = 270
- TempAngle = 360
- For i = 1 To N
- For ii = 0 To N
- If (ii = PointIndex(0) And CurPIndex <> PointIndex(0)) Or (ii <> CurPIndex And (Not IsIn(ii, PointIndex))) Then
- SecendPoint(0) = PointArray(0, ii)
- SecendPoint(1) = PointArray(1, ii)
- Temp = GetAngle(BasePoint, SecendPoint, BaseAngle, Direction)
- 'Print #1, CurPIndex & "," & ii & ":" & Temp
- If Temp <= TempAngle Then
- TempAngle = Temp
- TempIndex = ii
- End If
- End If
- Next ii
- If TempIndex = PointIndex(0) Then
- Direction = (-1) * Direction
- i = i - 1
- TempAngle = 360
- Else
- P1(0) = PointArray(0, PointIndex(UBound(PointIndex)))
- P1(1) = PointArray(1, PointIndex(UBound(PointIndex)))
- ReDim Preserve PointIndex(UBound(PointIndex) + 1)
- PointIndex(UBound(PointIndex)) = TempIndex
- P2(0) = PointArray(0, PointIndex(UBound(PointIndex)))
- P2(1) = PointArray(1, PointIndex(UBound(PointIndex)))
- BaseAngle = GetAngle(BasePoint, P2)
- CurPIndex = TempIndex
- BasePoint(0) = PointArray(0, CurPIndex)
- BasePoint(1) = PointArray(1, CurPIndex)
- TempAngle = 360
- ThisDrawing.ModelSpace.AddLine P1, P2
- ThisDrawing.Application.Update
- End If
- Next i
- P1(0) = PointArray(0, CurPIndex)
- P1(1) = PointArray(1, CurPIndex)
- ThisDrawing.ModelSpace.AddLine P1, StartPoint
- 'Close #1
- End Sub
- Private Function IsIn(Element As Integer, DataArray) As Boolean
- Dim i As Integer
- For i = 0 To UBound(DataArray)
- If Element = DataArray(i) Then
- IsIn = True
- Exit Function
- End If
- Next i
- IsIn = False
- End Function
- Private Function GetAngle(BasePoint, SecendPoint, Optional BaseAngle As Double = 0, Optional Direction As Integer = 1) As Double
- Dim Angle As Double
- If SecendPoint(0) = BasePoint(0) Then
- If SecendPoint(1) > BasePoint(1) Then
- Angle = 90
- Else
- Angle = -90
- End If
- Else
- Angle = (Atn((SecendPoint(1) - BasePoint(1)) / (SecendPoint(0) - BasePoint(0)))) * 180 / Pi
- End If
- If SecendPoint(0) > BasePoint(0) Then
- Angle = 360 + Angle '1,4
- Else
- Angle = 180 + Angle '2,3
- End If
- GetAngle = (Angle - BaseAngle) * Direction
- If GetAngle < 0 Then GetAngle = GetAngle + 360
- End Function
例图:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
参与人数 1 | 威望 +2 |
金钱 +10 |
贡献 +10 |
激情 +5 |
收起
理由
|
mccad
| + 2 |
+ 10 |
+ 10 |
+ 5 |
【好评】好程序 |
查看全部评分
|