// First assume you have a line - find the slope and y-intercept
slope = (point[0].y - point[1].y) / (point[0].x - point[1].x);
y_intercept = point[0].y - (slope * point[0].x);
// Using slope-intercept (y = mx + b), see if the other points are on the same line
for (n = 0 to numPoints)
{
if ((slope * point[n].x + y_intercept) != point[n].y)
{
// You've found a point that's not on the line - as soon as you
// find a point that's not on the line, you know that the contour
// is not a straight line
}
}
请注意,您必须在 if 条件中考虑到这一点 - 您可以't directly compare floating point numbers for equality, so you' ll需要将它们四舍五入到一定程度的准确度 . 我把它留下来保持伪代码简单 .
1 回答
首先假设你有一条线,并应用一些基本的代数 .
首先找到直线的斜率和y轴截距 . 线的斜率定义为y的变化除以x的变化 . 给定两点(x0,y0),(x1,y1):
使用斜率截距方程
(y=mx+b)
找到y截距并求解b:所以
一旦得到斜率和y轴截距,您只需要遍历轮廓的点并查看是否所有点都落在同一条线上 . 如果他们这样做,你就有了一条线;如果他们不这样做,你就会有一条曲线 .
由于我不知道你正在使用什么语言,这里是伪代码的整个过程:
请注意,您必须在
if
条件中考虑到这一点 - 您可以't directly compare floating point numbers for equality, so you' ll需要将它们四舍五入到一定程度的准确度 . 我把它留下来保持伪代码简单 .