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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...