多边形的判断

wangj0001 2004-03-26 04:07:48
给出平面上的任意的n个点的坐标(完全精确的)

是否存在如果有一个不自交的n边形使得其顶点为恰巧为此n个点?
(顶角不能为平角)

求一个尽量好的算法.
"好"是指时间复杂度小
...全文
89 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhennehzil 2004-04-06
  • 打赏
  • 举报
回复
...
还真不会算时间复杂度之类的.当时就没学好.好像主要是算循环次数吧.
那么就是4个单项循环.还有一个双向循环(冒泡排序,如果有更好的排序算法,这里就可以省时间了)
那就是4*n+n^2吧!
wangj0001 2004-04-06
  • 打赏
  • 举报
回复

万分谢谢

请问你的这个程序的时间复杂度是多少?
表示成顶点个数n的函数.
并且是指最少保证得到结果的时间.
lizhennehzil 2004-04-06
  • 打赏
  • 举报
回复
我用VB写了一个程序,大概能完成你的功能,不过我不太了结不"自交"什么意思,就按照不"内交"理解的.就是所有的边不能交叉.另外,我按照10个点做的,但是想扩展成任意个点就很容易了,添加一个常量就可以了.还有一点平角,我没有按照完全的平角,是按照一个范围内就算平角,界面上有个输入上下限角度的地方,譬如输入15,那么任意两条边所构成的角度如果大于180-15=165并且小于180+15=195度的都算平角.恩.不知道我说明白没有.另外没有容错处理.

下面是源程序.新建一个程序,界面上添加两个按钮,一个文本框,一个picture.然后运行.
Option Explicit


Dim int点(1 To 10, 1 To 2) As Integer
Dim int当前位置 As Integer
Dim inttemp As Integer
Dim int中心位置X As Integer
Dim int中心位置Y As Integer

Dim int角度(1 To 10, 1 To 2) As Double

Private Sub 确定中心位置()
Dim lngtemp As Long
Dim lngtmp As Long
Dim inttmp As Integer
lngtmp = 0
lngtemp = 0
inttmp = 0
For inttemp = 1 To 10
lngtmp = lngtmp + int点(inttemp, 1)
lngtemp = lngtemp + int点(inttemp, 2)
inttmp = inttmp + 1
Next
int中心位置X = lngtmp / inttmp
int中心位置Y = lngtemp / inttmp

End Sub


Private Sub 画多边形()
Dim inttmp As Integer
Dim int临时 As Integer
Dim douswap As Double
For inttemp = 1 To 10
int角度(inttemp, 1) = inttemp
'判断在哪个象限
If int点(inttemp, 1) > int中心位置X And int点(inttemp, 2) < int中心位置Y Then
int角度(inttemp, 2) = Atn((int中心位置Y - int点(inttemp, 2)) / (int点(inttemp, 1) - int中心位置X))
End If
If int点(inttemp, 1) < int中心位置X And int点(inttemp, 2) < int中心位置Y Then
int角度(inttemp, 2) = (1.5707963 - Atn((int中心位置Y - int点(inttemp, 2)) / (int中心位置X - int点(inttemp, 1)))) + 1.5707963
End If
If int点(inttemp, 1) < int中心位置X And int点(inttemp, 2) > int中心位置Y Then
int角度(inttemp, 2) = Atn((int点(inttemp, 2) - int中心位置Y) / (int中心位置X - int点(inttemp, 1))) + 3.1415926
End If
If int点(inttemp, 1) > int中心位置X And int点(inttemp, 2) > int中心位置Y Then
int角度(inttemp, 2) = (1.5707963 - Atn((int点(inttemp, 2) - int中心位置Y) / (int点(inttemp, 1) - int中心位置X))) + 4.7123889
End If
Next

'排序
For inttemp = 1 To 9
For inttmp = inttemp + 1 To 10
If int角度(inttemp, 2) > int角度(inttmp, 2) Then
'交换
douswap = int角度(inttemp, 2)
int角度(inttemp, 2) = int角度(inttmp, 2)
int角度(inttmp, 2) = douswap
douswap = int角度(inttemp, 1)
int角度(inttemp, 1) = int角度(inttmp, 1)
int角度(inttmp, 1) = douswap
End If
Next
Next

If 判断是否存在平角 = False Then
'画画
For inttemp = 1 To 9
inttmp = int角度(inttemp, 1)
int临时 = int角度(inttemp + 1, 1)
Picture1.Line (int点(inttmp, 1), int点(inttmp, 2))-(int点(int临时, 1), int点(int临时, 2))
Next
inttmp = int角度(10, 1)
int临时 = int角度(1, 1)
Picture1.Line (int点(inttmp, 1), int点(inttmp, 2))-(int点(int临时, 1), int点(int临时, 2))
Else
MsgBox "存在平角"
End If
End Sub

Private Sub Command2_Click()
Form_Load
End Sub

Private Sub Command1_Click()
确定中心位置
画多边形
End Sub

Private Function 判断是否存在平角() As Boolean
Dim blntmp As Boolean
Dim doutemp As Double
Dim doutmp As Double
Dim int1 As Integer
Dim int2 As Integer
Dim int3 As Integer
blntmp = False

Dim dou上限 As Double
Dim dou下限 As Double

dou上限 = (CInt(Text1.Text) / 180) * 3.1415926
dou下限 = dou上限

For inttemp = 1 To 8
int1 = int角度(inttemp, 1)
int2 = int角度(inttemp + 1, 1)
int3 = int角度(inttemp + 2, 1)

doutemp = (int点(int2, 2) - int点(int1, 2)) / (int点(int2, 1) - int点(int1, 1))
doutmp = (int点(int3, 2) - int点(int1, 2)) / (int点(int3, 1) - int点(int1, 1))
If (doutemp > (doutmp - dou下限)) And (doutemp < (doutmp + dou上限)) Then

判断是否存在平角 = True
Exit Function
End If
Next

int1 = int角度(9, 1)
int2 = int角度(10, 1)
int3 = int角度(1, 1)

doutemp = (int点(int2, 2) - int点(int1, 2)) / (int点(int2, 1) - int点(int1, 1))
doutmp = (int点(int3, 2) - int点(int1, 2)) / (int点(int3, 1) - int点(int1, 1))
If (doutemp > (doutmp - dou下限)) And (doutemp < (doutmp + dou上限)) Then

判断是否存在平角 = True
Exit Function
End If

int1 = int角度(10, 1)
int2 = int角度(1, 1)
int3 = int角度(2, 1)

doutemp = (int点(int2, 2) - int点(int1, 2)) / (int点(int2, 1) - int点(int1, 1))
doutmp = (int点(int3, 2) - int点(int1, 2)) / (int点(int3, 1) - int点(int1, 1))
If (doutemp > (doutmp - dou下限)) And (doutemp < (doutmp + dou上限)) Then

判断是否存在平角 = True
Exit Function
End If

判断是否存在平角 = False
End Function



Private Sub Form_Load()
Command1.Caption = "画多边形"
Command1.Enabled = False
Command2.Caption = "初始化"
Text1.Text = "10"
Picture1.AutoRedraw = True
Picture1.Cls
int当前位置 = 1
For inttemp = 1 To 10
int点(inttemp, 1) = -1
int点(inttemp, 2) = -1
Next
End Sub

Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
If int当前位置 = 11 Then Exit Sub
int点(int当前位置, 1) = X
int点(int当前位置, 2) = Y
Picture1.Circle (X, Y), 10
Picture1.Print int当前位置
int当前位置 = int当前位置 + 1
Me.Caption = int当前位置 - 1
If int当前位置 = 11 Then
Command1.Enabled = True
Else
Command1.Enabled = False
End If
End If

End Sub

wangj0001 2004-03-26
  • 打赏
  • 举报
回复
为什么要改名字?
wangj0001 2004-03-26
  • 打赏
  • 举报
回复
为什么要改名字?
hscc2008 2004-03-26
  • 打赏
  • 举报
回复
改个名字先
wangj0001 2004-03-26
  • 打赏
  • 举报
回复
为什么我只能打开我发的贴子?
wangj0001 2004-03-26
  • 打赏
  • 举报
回复
为什么我只能打开我发的贴子?

249

社区成员

发帖
与我相关
我的任务
社区描述
其他产品/厂家
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧