欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【python】Draw a straight line

程序员文章站 2022-04-27 13:22:07
...

根据直线方程画直线是一个比较直观且容易实现的方法,那么如何画线段呢? 也即,如何根据两个端点来画线段呢?这就是本篇博客要介绍的内容之一,其次,由线段围成的封闭区域面积应该如何计算呢



1 画线段

参考博客 matplotlib画直线

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

figure, ax = plt.subplots()
# 设置x,y值域
ax.set_xlim(left=0, right=4)
ax.set_ylim(bottom=0, top=4)
# 两条line的数据
line1 = [(1, 1), (2, 2)]
line2 = [(3, 3), (4, 4)]
(line1_xs, line1_ys) = zip(*line1)
(line2_xs, line2_ys) = zip(*line2)
# 创建两条线,并添加
ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, color='red'))
# 展示
plt.plot()
plt.show()

【python】Draw a straight line

2 计算多边形面积

《宫颐癌细胞计算机自动识别系统研究与设计》论文中有如下描述
【python】Draw a straight line
对于 x1x2y1y2\begin{vmatrix} x_1 & x_2\\ y_1 & y_2 \end{vmatrix} 具体的计算方法作者没有进一步阐述,脑海里第一反应是 det (determinant),二阶的容易嘛 ABCD=ADBC\begin{vmatrix} A & B\\ C & D \end{vmatrix} = AD-BC,coding 试了试,并不正确 (づ╥﹏╥)づ

如何计算封闭曲线的面积? [问题点数:40分,结帖人OnlyVB] 这个论坛中找到了解决方案!

【python】Draw a straight line
也即论文中 x1x2y1y2\begin{vmatrix} x_1 & x_2\\ y_1 & y_2 \end{vmatrix} 的计算形式如下:(x1x2)(y1+y2)(x_1-x_2)*(y_1+y_2),coding 后发现 (x2x1)(y1+y2)(x_2-x_1)*(y_1+y_2) 为正确结果(两者差一个负号)

总结公式如下:
S=12i=1n(xi+1xi)(yi+1+yi)S = \frac{1}{2}\sum_{i=1}^{n}\left ( x_{i+1} - x_i \right )\left ( y_{i+1}+y_{i} \right )
i=ni = n时,xi+1=x1,yi+1=y1x_{i+1} = x_1, y_{i+1} = y_1


用第一节的画法来画个图,然后测试下公式
线段比较多,打印下代码,哈哈哈

for i in range(1,13):
    #print("line{a} = [A{a},A{b}]".format(a=i,b=i+1))
    #print("(line{a}_xs, line{a}_ys) = zip(*line{a})".format(a=i))
    print("ax.add_line(Line2D(line{a}_xs, line{a}_ys, linewidth=1, color='blue'))".format(a=i))

设置好端点

A1 = (0,1)
A2 = (1,2)
A3 = (0,3)
A4 = (1,4)
A5 = (2,3)
A6 = (3,4)
A7 = (4,3)
A8 = (3,2)
A9 = (4,1)
A10 = (3,0)
A11 = (2,1)
A12 = (1,0)

画一下图(方法比较笨,仅提供一种配合打印代码的使用方法)

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

figure, ax = plt.subplots()
# 设置x,y值域
ax.set_xlim(left=0, right=4)
ax.set_ylim(bottom=0, top=4)
# 两条line的数据
line1 = [A1,A2]
line2 = [A2,A3]
line3 = [A3,A4]
line4 = [A4,A5]
line5 = [A5,A6]
line6 = [A6,A7]
line7 = [A7,A8]
line8 = [A8,A9]
line9 = [A9,A10]
line10 = [A10,A11]
line11 = [A11,A12]
line12 = [A12,A1]
(line1_xs, line1_ys) = zip(*line1)
(line2_xs, line2_ys) = zip(*line2)
(line3_xs, line3_ys) = zip(*line3)
(line4_xs, line4_ys) = zip(*line4)
(line5_xs, line5_ys) = zip(*line5)
(line6_xs, line6_ys) = zip(*line6)
(line7_xs, line7_ys) = zip(*line7)
(line8_xs, line8_ys) = zip(*line8)
(line9_xs, line9_ys) = zip(*line9)
(line10_xs, line10_ys) = zip(*line10)
(line11_xs, line11_ys) = zip(*line11)
(line12_xs, line12_ys) = zip(*line12)
# 创建两条线,并添加
ax.add_line(Line2D(line1_xs, line1_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line2_xs, line2_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line3_xs, line3_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line4_xs, line4_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line5_xs, line5_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line6_xs, line6_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line7_xs, line7_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line8_xs, line8_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line9_xs, line9_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line10_xs, line10_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line11_xs, line11_ys, linewidth=1, color='blue'))
ax.add_line(Line2D(line12_xs, line12_ys, linewidth=1, color='blue'))
# 展示
plt.plot()
plt.savefig('1.png')
plt.show()

【python】Draw a straight line
我们可以计算出该多边形的面积为 225=10\sqrt{2}*\sqrt{2}*5=10

用公式计算下面积 (x2x1)(y1+y2)(x_2-x_1)*(y_1+y_2)

def det(A,B):
    x1,y1 = A[0],A[1]
    x2,y2 = B[0],B[1]
    return (x2-x1)*(y1+y2)

代入端点

S = 1/2*(det(A1,A2)+det(A2,A3)+det(A3,A4)+det(A4,A5)+det(A5,A6)+det(A6,A7)+
         det(A7,A8)+det(A8,A9)+det(A9,A10)+det(A10,A11)+det(A11,A12)+det(A12,A1))
S

output

10.0

OK,大功告成