寻找满足斜率范围条件的最长连续离散点链
离散点最长连续链求解任务
数据集
以下是待处理的离散点数据集:
| 序号 | y值 | x值 |
|---|---|---|
| 0 | 1.000000 | 1000.000000 |
| 1 | 0.999415 | 1000.000287 |
| 2 | 0.999420 | 1000.000358 |
| 3 | 0.999376 | 1000.000609 |
| 4 | 0.999239 | 1000.000788 |
| 5 | 0.999011 | 1000.000967 |
| 6 | 1.000389 | 1000.001433 |
| 7 | 0.999871 | 1000.001756 |
| 8 | 0.995070 | 1000.002723 |
| 9 | 0.996683 | 1000.003404 |
任务要求
从上述数据集中找出最长的连续点链,链中相邻两点连线的斜率绝对值需小于给定阈值 epsilon=0.4。数据已做归一化处理,用于缩放正切计算结果。
现有代码片段
正切值计算函数
def tangent(df, pt1, pt2): y = df.iloc[pt2]['y'] - df.iloc[pt1]['y'] x = df.iloc[pt2]['x'] - df.iloc[pt1]['x'] return x/y
斜率检测草稿代码
import math index = 1 while index < df.shape[0]: if abs(math.tan(tangent(df, index-1, index) * math.pi)) < epsilon: print("result:", index) index += 1
内容的提问来源于stack exchange,提问作者bugrahaskan
相关产品推荐
相关产品推荐

