最近开了算法课,但是我的算法着实不咋地,一直搞web和逆向,就没怎么编程。记录一下0.0
算法倒是不难实现,但是这个动态演示很烦,从纯粹的可视化小白,强行写完了,写完发现非常简单,只是自己不知道的函数太多了,哭了。。。。
蛮力法就不用解释了,通俗的说就是把所有可能试一遍。
凸包问题,就是将n个点中某几个点围成一个多边形,除了这n个点,其余的点都在这个多边形内。
核心算法其实就是一个行列式演变而来,后悔没学好线代。。。。。
参考:https://blog.csdn.net/u011001084/article/details/72768075
贴出我的代码:
import random import matplotlib.pyplot as p input = int(input('输入生成点的数量:')) dot = [[0]*3 for i in range(input)] x = [[0]*2 for a in range(int(input * (input - 1) / 2))] y = [[0]*2 for b in range(int(input * (input - 1) / 2))] fg = p.figure() cn = fg.add_subplot(1, 1, 1) cn.set_xlim(0, 1000) cn.set_ylim(0, 1000) p.ion() for i in range(input): dot[i][0] = random.randrange(1000) dot[i][1] = random.randrange(1000) dot[i][2] = 0 def judge(inp): n = 0 for i in range(inp): for j in range(i+1, inp): a = dot[j][1] - dot[i][1] b = dot[i][0] - dot[j][0] c = (dot[i][0] * dot[j][1]) - (dot[i][1] * dot[j][0]) sign1 = 0 sign2 = 0 x[n][0] = dot[i][0] x[n][1] = dot[j][0] y[n][0] = dot[i][1] y[n][1] = dot[j][1] n += 1 for k in range(inp): if k == j or k == i: continue if a*dot[k][0]+b*dot[k][1] == c: sign1 += 1 sign2 += 1 if a*dot[k][0]+b*dot[k][1] > c: sign1 += 1 if a*dot[k][0]+b*dot[k][1] < c: sign2 += 1 if (sign1 == (inp - 2)) or (sign2 == (inp - 2)): dot[i][2] = 1 dot[j][2] = 1 cn.scatter(dot[i][0], dot[i][1], color='g', marker='.') cn.scatter(dot[j][0], dot[j][1], color='g', marker='.') cn.plot(x[n-1], y[n-1], color='b') cn.scatter(dot[i][0], dot[i][1], color='g', marker='.') cn.scatter(dot[j][0], dot[j][1], color='g', marker='.') cn.plot(x[n-1], y[n-1], color='r') p.pause(0.1) cn.lines.pop() judge(input) print("凸包极点:") for i in range(input): if dot[i][2] == 1: print((dot[i][0], dot[i][1]))
import random
import matplotlib.pyplot as p
input = int(input('输入生成点的数量:'))
dot = [[0]*3 for i in range(input)]
x = [[0]*2 for a in range(int(input * (input - 1) / 2))]
y = [[0]*2 for b in range(int(input * (input - 1) / 2))]
fg = p.figure()
cn = fg.add_subplot(1, 1, 1)
cn.set_xlim(0, 1000)
cn.set_ylim(0, 1000)
p.ion()
for i in range(input):
dot[i][0] = random.randrange(1000)
dot[i][1] = random.randrange(1000)
dot[i][2] = 0
def judge(inp):
n = 0
for i in range(inp):
for j in range(i+1, inp):
a = dot[j][1] - dot[i][1]
b = dot[i][0] - dot[j][0]
c = (dot[i][0] * dot[j][1]) - (dot[i][1] * dot[j][0])
sign1 = 0
sign2 = 0
x[n][0] = dot[i][0]
x[n][1] = dot[j][0]
y[n][0] = dot[i][1]
y[n][1] = dot[j][1]
n += 1
for k in range(inp):
if k == j or k == i:
continue
if a*dot[k][0]+b*dot[k][1] == c:
sign1 += 1
sign2 += 1
if a*dot[k][0]+b*dot[k][1] > c:
sign1 += 1
if a*dot[k][0]+b*dot[k][1] < c:
sign2 += 1
if (sign1 == (inp - 2)) or (sign2 == (inp - 2)):
dot[i][2] = 1
dot[j][2] = 1
cn.scatter(dot[i][0], dot[i][1], color='g', marker='.')
cn.scatter(dot[j][0], dot[j][1], color='g', marker='.')
cn.plot(x[n-1], y[n-1], color='b')
cn.scatter(dot[i][0], dot[i][1], color='g', marker='.')
cn.scatter(dot[j][0], dot[j][1], color='g', marker='.')
cn.plot(x[n-1], y[n-1], color='r')
p.pause(0.1)
cn.lines.pop()
judge(input)
print("凸包极点:")
for i in range(input):
if dot[i][2] == 1:
print((dot[i][0], dot[i][1]))