zoukankan      html  css  js  c++  java
  • 生成凹壳

    http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    ###############
    #
    # Code source modified from
    # http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/
    #
    ###############
    
    
    from shapely import geometry
    from shapely.ops import cascaded_union, polygonize
    from scipy.spatial import Delaunay
    import numpy as np
    import math
    
    
    def alpha_shape(points, alpha):
        """
        Compute the alpha shape (concave hull) of a set
        of points.
    
        @param points: Iterable container of points.
        @param alpha: alpha value to influence the
            gooeyness of the border. Smaller numbers
            don't fall inward as much as larger numbers.
            Too large, and you lose everything!
        """
        if len(points) < 4:
            # When you have a triangle, there is no sense
            # in computing an alpha shape.
            return geometry.MultiPoint(list(points)).convex_hull
    
        def add_edge(edges, edge_points, coords, i, j):
            """
            Add a line between the i-th and j-th points,
            if not in the list already
            """
            if (i, j) in edges or (j, i) in edges:
                # already added
                return
            edges.add((i, j))
            edge_points.append(coords[[i, j]])
    
        coords = np.array([point.coords[0] for point in points])
    
        tri = Delaunay(coords)
        edges = set()
        edge_points = []
        # loop over triangles:
        # ia, ib, ic = indices of corner points of the
        # triangle
        for ia, ib, ic in tri.vertices:
            pa = coords[ia]
            pb = coords[ib]
            pc = coords[ic]
    
            # Lengths of sides of triangle
            a = math.sqrt((pa[0] - pb[0]) ** 2 + (pa[1] - pb[1]) ** 2)
            b = math.sqrt((pb[0] - pc[0]) ** 2 + (pb[1] - pc[1]) ** 2)
            c = math.sqrt((pc[0] - pa[0]) ** 2 + (pc[1] - pa[1]) ** 2)
    
            # Semiperimeter of triangle
            s = (a + b + c) / 2.0
    
            # Area of triangle by Heron's formula
            area = math.sqrt(s * (s - a) * (s - b) * (s - c))
            circum_r = a * b * c / (4.0 * area) #外接圆半径
    
            # Here's the radius filter.
            # print circum_r
            if circum_r < 1.0 / alpha:
                add_edge(edges, edge_points, coords, ia, ib)
                add_edge(edges, edge_points, coords, ib, ic)
                add_edge(edges, edge_points, coords, ic, ia)
    
        m = geometry.MultiLineString(edge_points)
        triangles = list(polygonize(m))
        return cascaded_union(triangles), edge_points

    image

    alpha = .4
    concave_hull, edge_points = alpha_shape(new_points,  alpha=alpha)
    
    plot_polygon(concave_hull)
    _ = pl.plot(x,y,'o', color='#f16824')
    plot_polygon(concave_hull.buffer(1))
    _ = pl.plot(x,y,'o', color='#f16824')

    image

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    ------------------------------------------------------------------------------------------------------------------------------------------------------------

    1、三角形面积=1/2*底*高(三边都可做底)
    2、三角形面积=1/2absinC=1/2acsinB=1/2bcsinA
    3、三角形面积=abc/4R(其中R是三角形外接圆半径)

    a=2RsinA
    b=2RsinB
    c=2RsinC

  • 相关阅读:
    Hibernate对象状态
    Session接口常用方法
    Hibernate 映射文件基本概述
    Hibernate domain对象说明
    Hibernate配置文件说明
    Hiberante可配置参数
    Hibernate基本演示
    使用JSON数据格式模拟股票实时信息
    操作系统实验零——操作系统实验环境准备
    Shell脚本之for循环、while循环,if语句、case语句
  • 原文地址:https://www.cnblogs.com/gispathfinder/p/5791044.html
Copyright © 2011-2022 走看看