繁体   English   中英

从列表中选择正确的距离

[英]Choosing correct distance from a list

这个问题与类似 不过,我比OP走得更远,而且我使用的是Python 2(不确定他在使用什么)。

我有一个Python函数,该函数可以确定从凸多边形内部的点到沿多边形外围的规则定义的间隔的距离。 问题是它返回我需要消除的“额外”距离。 (请注意,我怀疑这对矩形尚不适用。我还没有完成。)首先,代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#  t1.py
# 
#  Copyright 2015 FRED <fred@matthew24-25>
# 
#  THIS IS TESTING CODE ONLY. IT WILL BE MOVED INTO THE CORRECT MODULE
#  UPON COMPLETION.
# 
from __future__ import division
import math
import matplotlib.pyplot as plt
def Dist(center_point, Pairs, deg_Increment):

# I want to set empty lists to store the values of m_lnsgmnt and b_lnsgmnts
# for every iteration of the for loop.
    m_linesegments = []
    b_linesegments = []

# Scream and die if Pairs[0] is the same as the last element of Pairs--i.e.
# it has already been run once.
#if Pairs[0] == Pairs[len(Pairs)-1]:
  ##print "The vertices contain duplicate points!"

## Creates a new list containing the original list plus the first element. I did this because, due
## to the way the for loop is set up, the last iteration of the loop subtracts the value of the
## last value of Pairs from the first value. I therefore duplicated the first value.
#elif:
    new_Pairs = Pairs + [Pairs[0]]

    # This will calculate the slopes and y-intercepts of the linesegments of the polygon.
    for a in range(len(Pairs)):

        # This calculates the slope of each line segment and appends it to m_linesegments.
        m_lnsgmnt = (new_Pairs[a+1][2] - new_Pairs[a][3]) / (new_Pairs[a+1][0] - new_Pairs[a][0])
        m_linesegments.append(m_lnsgmnt)

        # This calculates the y-intercept of each line segment and appends it to b_linesegments.
        b_lnsgmnt = (Pairs[a][4]) - (m_lnsgmnt * Pairs[a][0])
        b_linesegments.append(b_lnsgmnt)

    # These are temporary testing codes.
    print "m_linesegments =", m_linesegments
    print "b_linesegments =", b_linesegments

    # I want to set empty lists to store the value of m_rys and b_rys for every
    # iteration of the for loop.
    m_rays = []
    b_rays = []

    # I need to set a range of degrees the intercepts will be calculated for.
    theta = range(0, 360, deg_Increment)

    # Temporary testing line.
    print "theta =", theta

    # Calculate the slope and y-intercepts of the rays radiating from the center_point.
    for b in range(len(theta)):

        m_rys = math.tan(math.radians(theta[b]))
        m_rays.append(m_rys)

        b_rys = center_point[1] - (m_rys * center_point[0])
        b_rays.append(b_rys)

    # Temporary testing lines.   
    print "m_rays =", m_rays
    print "b_rays =", b_rays


    # Set empty matrix for Intercepts.
    Intercepts = []
    angle = []

    # Calculate the intersections of the rays with the line segments.
    for c in range((360//deg_Increment)):

        for d in range(len(Pairs)):

            # Calculate the x-coordinates and the y-coordinates of each
            # intersection
            x_Int = (b_rays[c] - b_linesegments[d]) / (m_linesegments[d] - m_rays[c])
            y_Int = ((m_linesegments[d] * x_Int) + b_linesegments[d])


            Intercepts.append((x_Int, y_Int))

            # Calculates the angle of the ray. Rounding is necessary to 
            # compensate for binary-decimal errors. 
            a_ngle = round(math.degrees(math.atan2((y_Int - center_point[1]), (x_Int - center_point[0]))))

            # Substitutes positive equivalent for every negative angle, 
            # i.e. -270 degrees equals 90 degrees.
            if a_ngle < 0:
                a_ngle = a_ngle + 360

            # Selects the angles that correspond to theta
            if a_ngle == theta[c]:
                angle.append(a_ngle)


    print "INT1=", Intercepts
    print "angle=", angle

    dist = []

    # Calculates distance.
    for e in range(len(Intercepts) - 1):
        distA = math.sqrt(((Intercepts[e][0] - center_point[0])**2) +  ((Intercepts[e][5]- center_point[1])**2))
        dist.append(distA)

    print "dist=", dist

if __name__ == "__main__":
    main()  

现在,关于它的工作方式:该代码需要3个输入:center_point(多边形中包含的点,以(x,y)坐标给出),Pairs(多边形的顶点,也以(x,y)坐标给出) )和deg_Increment(定义计算距离的频率)。 让我们假设center_point = (4,5)Pairs = [(1, 4), (3, 8), (7, 2)] deg_Increment = 20 Pairs = [(1, 4), (3, 8), (7, 2)]deg_Increment = 20 这意味着将创建一个顶点为Pairs的多边形(作为其一部分),而center_point是该多边形内部包含的点。 现在射线从设置以辐射center_point每20度(这是deg_Increment )。 确定射线与多边形周长的交点,并使用距离公式计算距离。 唯一的问题是我距离太远。 :(在上面的示例中,正确的距离是1.00000 0.85638 0.83712 0.92820 1.20455 2.07086 2.67949 2.29898 2.25083 2.50000 3.05227 2.22683 1.93669 1.91811 2.15767 2.85976 2.96279 1.40513

但我的代码被返回dist= [2.5, 1.0, 6.000000000000001, 3.2523178818773006, 0.8563799085248148, 3.0522653889161626, 5.622391569468206, 0.8371216462519347, 2.226834844885431, 37.320508075688686, 0.9282032302755089, 1.9366857335569072, 7.8429970322236064, 1.2045483557883576, 1.9181147622136665, 3.753460385470896, 2.070863609380179, 2.157671808913309, 2.6794919243112276, 12.92820323027545, 2.85976265663383, 2.298981118867903, 2.962792920643178, 5.162096782237789, 2.250827351906659, 1.4051274947736863, 69.47032761621092, 2.4999999999999996, 1.0, 6.000000000000004, 3.2523178818773006, 0.8563799085248148, 3.0522653889161626, 5.622391569468206, 0.8371216462519347, 2.226834844885431, 37.32050807568848, 0.9282032302755087, 1.9366857335569074, 7.842997032223602, 1.2045483557883576, 1.9181147622136665, 3.7534603854708997, 2.0708636093801767, 2.1576718089133085, 2.679491924311227, 12.928203230275532, 2.85976265663383, 2.298981118867903, 2.9627929206431776, 5.162096782237789, 2.250827351906659, 1.4051274947736847] dist= [2.5, 1.0, 6.000000000000001, 3.2523178818773006, 0.8563799085248148, 3.0522653889161626, 5.622391569468206, 0.8371216462519347, 2.226834844885431, 37.320508075688686, 0.9282032302755089, 1.9366857335569072, 7.8429970322236064, 1.2045483557883576, 1.9181147622136665, 3.753460385470896, 2.070863609380179, 2.157671808913309, 2.6794919243112276, 12.92820323027545, 2.85976265663383, 2.298981118867903, 2.962792920643178, 5.162096782237789, 2.250827351906659, 1.4051274947736863, 69.47032761621092, 2.4999999999999996, 1.0, 6.000000000000004, 3.2523178818773006, 0.8563799085248148, 3.0522653889161626, 5.622391569468206, 0.8371216462519347, 2.226834844885431, 37.32050807568848, 0.9282032302755087, 1.9366857335569074, 7.842997032223602, 1.2045483557883576, 1.9181147622136665, 3.7534603854708997, 2.0708636093801767, 2.1576718089133085, 2.679491924311227, 12.928203230275532, 2.85976265663383, 2.298981118867903, 2.9627929206431776, 5.162096782237789, 2.250827351906659, 1.4051274947736847]

如果有人能帮助我获得正确的距离,我将不胜感激。 谢谢!

仅供参考,这是我的示例仅具有正确距离时的样子: 距离

您在Intercepts中获得了太多的值,因为它被附加到第二个for循环内[for d in range(len(Pairs))]

您只希望通过外部for循环[for c in range((360//deg_Increment))]中的每一步,在Intercept中需要一个值,因此Intercept的附加项需要在此循环中。

我不确定您对内部循环的处理方式,但是您似乎正在为构成多边形边的每条线计算一个单独的截距。 但是,您只希望在朝那个方向前进时将其打到“第一个”。

您必须添加一些代码来确定您实际上首先要遇到的多边形的3个边(在这种情况下)中的哪一个。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM