栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Douglas一Peukcer算法: 曲线近似

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Douglas一Peukcer算法: 曲线近似

简介

Douglas一Peukcer算法,用于将曲线表示为点集,并化简点集。

算法流程 步骤1

连接曲线起点A和终点B,得到直线AB,将A、B放入点集。

步骤2

求曲线上距离AB最远的点C,计算C到AB的距离 d C d_C dC​与阈值Th。

步骤3

如果 d C > T h d_{C}>Th dC​>Th,则将C点放入点集中,连接直线AC,BC,并且重复步骤2和3;

如果 d C < T h d_C 实现

从以上算法流程可以看出,Douglas一Peukcer算法使用了递归的思想。以下是我自己的一个算法实现:(python)

import math


def pointDistance(pt1, pt2):
    return ((pt1[0] * pt1[0] - pt2[0] * pt2[0]) + (pt1[1] * pt1[1] - pt2[1] * pt2[1])) ** 0.5


def lineDistance(pt, line_pt1, line_pt2):
    k = (line_pt2[1] - line_pt1[1]) * 1.0 / (line_pt2[0] - line_pt1[0] + 1e-6)
    b = line_pt2[1] - k * line_pt2[0]
    d = abs(k * pt[0] - pt[1] + b) / (k ** 2 + 1) ** 0.5
    return d


def algorithmRamerDouglasPeucker(line_list, threshold=10):
    max_distance = 0.
    idx = None
    target = None
    processed_line = line_list
    for i, pt in enumerate(line_list):
        if len(line_list) < 3:
            break
        distance = lineDistance(pt, line_list[0], line_list[-1])
        print(distance)
        if distance > max_distance:
            idx = i
            target = pt
            max_distance = distance
    # print(idx, target)
    if max_distance >= threshold:
        line_list_split_first = line_list[:idx+1]
        line_list_split_second = line_list[idx:]
        processed_line_first = algorithmRamerDouglasPeucker(line_list_split_first)[:]
        processed_line_second = algorithmRamerDouglasPeucker(line_list_split_second)[1:]
        processed_line_first.extend(processed_line_second)
        processed_line = processed_line_first
    elif idx is not None:
        line_list.pop(idx)
        processed_line = algorithmRamerDouglasPeucker(line_list)
    return processed_line


if __name__ == '__main__':
    A = (0, 0)
    B = (15, 10)
    C = (23, 4)
    D = (19, 7)
    E = (1, 21)
    F = (22, 22)

    line = [A, B, C, D, E, F]

    result = algorithmRamerDouglasPeucker(line)
    print(result)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/268530.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号