维诺图

import numpy as np
from matplotlib import tri 
from scipy.linalg import solve
from matplotlib.patches import Circle
import matplotlib.pyplot as plt
#画圆
def draw_circle(x, y, r):
    ax = plt.gca()
    cir = Circle(xy=(x, y), radius=r, alpha=0.5)
    ax.add_patch(cir)
    ax.plot()
#画三角形
def plot_triangle(A, B, C):
    x = [A[0], B[0], C[0], A[0]]
    y = [A[1], B[1], C[1], A[1]]

    ax = plt.gca()
    ax.plot(x, y, linewidth=2)
#获取外接圆圆心坐标、半径
def get_outer_circle(A, B, C):
    xa, ya = A[0], A[1]
    xb, yb = B[0], B[1]
    xc, yc = C[0], C[1]

    # 两条边的中点
    x1, y1 = (xa + xb) / 2.0, (ya + yb) / 2.0
    x2, y2 = (xb + xc) / 2.0, (yb + yc) / 2.0

    # 两条线的斜率
    ka = (yb - ya) / (xb - xa) if xb != xa else None
    kb = (yc - yb) / (xc - xb) if xc != xb else None

    alpha = np.arctan(ka) if ka != None else np.pi / 2
    beta  = np.arctan(kb) if kb != None else np.pi / 2

    # 两条垂直平分线的斜率
    k1 = np.tan(alpha + np.pi / 2)
    k2 = np.tan(beta + np.pi / 2)

    # 圆心
    y, x = solve([[1.0, -k1], [1.0, -k2]], [y1 - k1 * x1, y2 - k2 * x2])
    # 半径
    r1 = np.sqrt((x - xa)**2 + (y - ya)**2)

    return(x, y, r1)
##测试
import matplotlib.pyplot as plt

A = (1., 1.)
B = (5., 2.)
C = (5., 5.)

plt.axis('equal')
plt.axis('off')
plot_triangle(A, B, C)

x, y, r1 = get_outer_circle(A, B, C)
plt.plot(x, y, 'rs')
draw_circle(x, y, r1)
plt.show()

测试画圆-三角形

##将文件中字符组转数组
def strtoarray(filename):
    data=[]
    with open(filename, 'r') as f:
        lines = f.readlines()
        for line in lines:
            value = [float(s) for s in line.split()]
            data.append(value[:2])
#     print(data)
    return(data)
##提取x,y值
x=[]
y=[]
Index=[]
filename='C:/Users/liupengyu/Desktop/8-93.txt'
points=strtoarray(filename)
for i in range(len(points)):
    x.append(points[i][0])
    y.append(points[i][1])
    Index.append(i)
## 解算维诺边
import matplotlib

triangles = tri.Triangulation(x,y) 
plt.triplot(triangles)  
plt.figure(figsize=(12,9))
plt.show()## 输出三角网,方便查看
T = triangles.triangles
n = T.shape[0]
# print(points[T[i,0]],points[T[i,1]],points[T[i,2]],n)

C = np.zeros((n,3))
for i in range(n):
    C[i] = get_outer_circle(points[T[i,0]],points[T[i,1]],points[T[i,2]])
X,Y = C[:,0], C[:,1]
segments = []
for i in range(n):
    for j in range(3):
        k = triangles.neighbors[i][j]
        if k != -1:
            segments.append( [(X[i],Y[i]), (X[k],Y[k])] )
print(C)##打印圆心和半径 可以去除差别大的点(先不去除)

Delaunay三角网

## 打印输出相应结果
plt.figure(figsize=(12,9))
plt.scatter(x, y, color="blue",marker="s")
lines = matplotlib.collections.LineCollection(segments, color='red')
plt.gca().add_collection(lines)
plt.axis((53.6,54.7, 54.9,56.1))## 自定输出框 文件中出现了一些狭长的三角形离散点 为了美化 不显示
for i in range(len(Index)):
    plt.text(x[i],y[i]+0.02,Index[i],ha = 'center',va = 'bottom',fontsize=8)
plt.show()

维诺图

  • 32
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

褑引

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值