多边形填充-活动边表法

参考文档:
参考1:https://blog.youkuaiyun.com/u013044116/article/details/49737585
参考2:https://blog.youkuaiyun.com/keneyr/article/details/83747501

算法思想:
对多边形沿y轴从0开始遍历,建立边表NET。只记录顶点的x, dx, ymax。
根据NET构建活动边表AET(activate edge table)。射线与多边形的交点坐标为(x + △x, y + 1), △x是斜率的倒数(dx)。详解见 参考1

实现代码:

点击查看代码
# encoding=utf8
import math

import cv2
import numpy as np


class EdgeTable:
    def __init__(self, x, dx, ymax):
        self.x = x
        self.dx = dx
        self.ymax = ymax
        self.next = None


def fill_polys(polys):
    poly_x_max = float('-inf')
    poly_y_max = float('-inf')
    for poly in polys:
        poly_x_max = max(poly_x_max, poly[0])
        poly_y_max = max(poly_y_max, poly[1])

    poly_x_max += 1
    poly_y_max += 1
    point_count = len(polys)

    # build edge table
    net = [None for _ in range(poly_y_max)]
    for i in range(poly_y_max):
        for j in range(point_count):
            # 一个点跟前面的点形成线段 跟后边的点也形成线段
            if polys[j][1] == i:
                if polys[(j - 1 + point_count) % point_count][1] > polys[j][1]:
                    x_ = polys[j][0]
                    dx_ = (
                        polys[(j - 1 + point_count) % point_count][0] - polys[j][0]
                    ) / (polys[(j - 1 + point_count) % point_count][1] - polys[j][1])
                    ymax_ = polys[(j - 1 + point_count) % point_count][1]
                    pnt = EdgeTable(x_, dx_, ymax_)
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值