1. <legend id='GCS3K'><style id='GCS3K'><dir id='GCS3K'><q id='GCS3K'></q></dir></style></legend>

    <small id='GCS3K'></small><noframes id='GCS3K'>

    • <bdo id='GCS3K'></bdo><ul id='GCS3K'></ul>
    <i id='GCS3K'><tr id='GCS3K'><dt id='GCS3K'><q id='GCS3K'><span id='GCS3K'><b id='GCS3K'><form id='GCS3K'><ins id='GCS3K'></ins><ul id='GCS3K'></ul><sub id='GCS3K'></sub></form><legend id='GCS3K'></legend><bdo id='GCS3K'><pre id='GCS3K'><center id='GCS3K'></center></pre></bdo></b><th id='GCS3K'></th></span></q></dt></tr></i><div id='GCS3K'><tfoot id='GCS3K'></tfoot><dl id='GCS3K'><fieldset id='GCS3K'></fieldset></dl></div>
  2. <tfoot id='GCS3K'></tfoot>
    1. 如何在Python中获取多边形内网格点的坐标?

      时间:2023-07-23

      <legend id='bu1af'><style id='bu1af'><dir id='bu1af'><q id='bu1af'></q></dir></style></legend>

        1. <small id='bu1af'></small><noframes id='bu1af'>

              <bdo id='bu1af'></bdo><ul id='bu1af'></ul>
              1. <i id='bu1af'><tr id='bu1af'><dt id='bu1af'><q id='bu1af'><span id='bu1af'><b id='bu1af'><form id='bu1af'><ins id='bu1af'></ins><ul id='bu1af'></ul><sub id='bu1af'></sub></form><legend id='bu1af'></legend><bdo id='bu1af'><pre id='bu1af'><center id='bu1af'></center></pre></bdo></b><th id='bu1af'></th></span></q></dt></tr></i><div id='bu1af'><tfoot id='bu1af'></tfoot><dl id='bu1af'><fieldset id='bu1af'></fieldset></dl></div>
                  <tbody id='bu1af'></tbody>
                <tfoot id='bu1af'></tfoot>
                本文介绍了如何在Python中获取多边形内网格点的坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想输入一个多边形的4个顶点的坐标,指定点的数量来划分多边形的边(等分),目标是生成一个矩阵,该矩阵带有多边形内部的网格点的坐标.

                I want to input the coordinates of 4 vertices of a polygon, specify the number of points to divide the polygon's edges (in equal segments), and the goal is to generate a matrix with the coordinates of the grid points inside the polygon.

                下图可能更好地解释了我的目标:

                The following picture might explain better my goal:

                所以在这种情况下,我将输入点 (P) 的坐标并指定我希望网格为 3 x 2.输出将是一个 3 x 2 矩阵,其坐标为网格的坐标 (x,y)点 (N).

                So in that case I would input the coordinates of the points (P) and specify that I want grid to be 3 by 2. The output would be a 3 by 2 matrix with the coordinates (x,y) of the grid points (N).

                我进行了很多搜索,但仍然找不到这样做的方法,老实说,我对 Python 一点经验都没有.我发现使用 numpy 的 meshgrid 结合 matplotlib.path 的 contains_points 在多边形内创建网格但我不知道如何获取网格点的坐标.我看到 shapely 在此类问题中被大量使用,但同样,我没有这方面的经验,因此不胜感激!

                I have searched a lot but still couldn't find a way to do this, and honestly I am not at all experienced with Python. I found something using numpy's meshgrid in combination with matplotlib.path's contains_points to create a grid inside a polygon but I have no idea how to get the grid point's coordinates. I saw shapely being used a lot in problems like this but again, I'm not experience in this so some help would be appreciated!

                提前谢谢大家!

                推荐答案

                为了解释这个方法,我们分为三个步骤:

                To explain the approach, we have three steps:

                1. 找出 4 边形每一边的刻度
                2. 找到网格线
                3. 找到网格线的交点

                def det(a, b):
                    return a[0] * b[1] - a[1] * b[0]
                
                ticks = []
                A = (1, 2)  # wlog., suppose the polygon is ABCD;
                B = (3, 2)
                C = (3, 4)
                D = (1, 4)
                polygon = [A, B, C, D]
                n = 3  # number of parts on each side of the grid
                
                # we first find ticks on each side of the polygon
                for j in range(4):  # because we are talking about 4-gons
                    temp_ticks = []
                    for i in range(n-1):
                        t = (i+1)*(1/n)
                        Ex = polygon[j][0] * (1-t) + polygon[(j+1) % 4][0] * t
                        Ey = polygon[j][1] * (1-t) + polygon[(j+1) % 4][1] * t
                        temp_ticks.append((Ex, Ey))
                    if j < 2:
                        ticks.append(temp_ticks)
                    else: # because you are moving backward in this part
                        temp_ticks.reverse()
                        ticks.append(temp_ticks)
                
                # then we find lines of the grid
                h_lines = []
                v_lines = []
                for i in range(n-1):
                    h_lines.append((ticks[0][i], ticks[2][i]))
                    v_lines.append((ticks[1][i], ticks[3][i]))
                        
                # then we find the intersection of grid lines
                for i in range(len(h_lines)):
                    for j in range(len(v_lines)):
                        line1 = h_lines[i]
                        line2 = v_lines[j]
                        xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
                        ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
                        div = det(xdiff, ydiff)
                        if div == 0:
                            raise Exception('lines do not intersect')
                
                        d = (det(*line1), det(*line2))
                        x = det(d, xdiff) / div
                        y = det(d, ydiff) / div
                        print(x,y) # this is an intersection point that you want
                

                注意:查找线交点的部分代码来自 这里

                Note: the part of the code for finding the intersection of lines are from here

                这篇关于如何在Python中获取多边形内网格点的坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Jupyter-Notebook 中使用循环的图像网格.如何? 下一篇:Windows 上的 Pyusb - 没有可用的后端

                相关文章

                <small id='nxzZt'></small><noframes id='nxzZt'>

                  <tfoot id='nxzZt'></tfoot>
                  <legend id='nxzZt'><style id='nxzZt'><dir id='nxzZt'><q id='nxzZt'></q></dir></style></legend>

                  <i id='nxzZt'><tr id='nxzZt'><dt id='nxzZt'><q id='nxzZt'><span id='nxzZt'><b id='nxzZt'><form id='nxzZt'><ins id='nxzZt'></ins><ul id='nxzZt'></ul><sub id='nxzZt'></sub></form><legend id='nxzZt'></legend><bdo id='nxzZt'><pre id='nxzZt'><center id='nxzZt'></center></pre></bdo></b><th id='nxzZt'></th></span></q></dt></tr></i><div id='nxzZt'><tfoot id='nxzZt'></tfoot><dl id='nxzZt'><fieldset id='nxzZt'></fieldset></dl></div>
                    • <bdo id='nxzZt'></bdo><ul id='nxzZt'></ul>