Jump to content
新域网络技术论坛
Sign in to follow this  
Jamers

Python中的简单示例代码(三点是否在一条直线上)

Recommended Posts

由于系统没字体,所以没使用中文的title

# -*- coding: UTF-8 -*-

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

x = [[0], [1], [2]]
y = [[0], [1], [2]]

if ((x[0][0] == x[1][0] and x[0][0] == x[2][0]) or (y[0][0] == y[1][0] and y[0][0] == y[2][0]) or ((x[0][0]-x[1][0])/(y[0][0]-y[1][0]) == (x[0][0]-x[2][0])/(y[0][0]-y[2][0]))):
    online = True
else:
    online = False
#title = u'三点是否在一条直线上?:'+str(online)
title = 'Is the three point in a straight line? '+str(online)
plt.figure()
plt.title(title)
plt.xlabel('x')
plt.ylabel('y')
plt.axis([-2, 10, -2, 10])
plt.grid(True)
plt.plot(x, y, 'k.')
plt.plot(x, y, 'g-')
plt.show()

Share this post


Link to post
Share on other sites

数学算法原理:

三点一线判定:

1. 三个点x轴或y轴一致,True

2. 任意两条线的斜率一致,True, (x1-x2)/(y1-y2) == (x1-x3)/(y1-y3)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×
×
  • Create New...