python - Choosing correct distance from a list -


this question similar this. i've gone bit farther op, though, , i'm in python 2 (not sure using).

i have python function can determine distance point inside convex polygon regularly-defined intervals along polygon's perimeter. problem returns "extra" distances need eliminate. (please note--i suspect not work rectangles yet. i'm not finished it.) first, code:

#!/usr/bin/env python # -*- coding: utf-8 -*- # #  t1.py #  #  copyright 2015 fred <fred@matthew24-25> #  #  testing code only. moved correct module #  upon completion. #  __future__ import division import math import matplotlib.pyplot plt def dist(center_point, pairs, deg_increment):  # want set empty lists store values of m_lnsgmnt , b_lnsgmnts # every iteration of loop.     m_linesegments = []     b_linesegments = []  # scream , die if pairs[0] same last element of pairs--i.e. # has been run once. #if pairs[0] == pairs[len(pairs)-1]:   ##print "the vertices contain duplicate points!"  ## creates new list containing original list plus first element. did because, due ## way loop set up, last iteration of loop subtracts value of ## last value of pairs first value. therefore duplicated first value. #elif:     new_pairs = pairs + [pairs[0]]      # calculate slopes , y-intercepts of linesegments of polygon.     in range(len(pairs)):          # calculates slope of each line segment , appends 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)          # calculates y-intercept of each line segment , appends b_linesegments.         b_lnsgmnt = (pairs[a][4]) - (m_lnsgmnt * pairs[a][0])         b_linesegments.append(b_lnsgmnt)      # these temporary testing codes.     print "m_linesegments =", m_linesegments     print "b_linesegments =", b_linesegments      # want set empty lists store value of m_rys , b_rys every     # iteration of loop.     m_rays = []     b_rays = []      # need set range of degrees intercepts calculated for.     theta = range(0, 360, deg_increment)      # temporary testing line.     print "theta =", theta      # calculate slope , y-intercepts of rays radiating center_point.     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 intercepts.     intercepts = []     angle = []      # calculate intersections of rays line segments.     c in range((360//deg_increment)):          d in range(len(pairs)):              # calculate x-coordinates , 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 angle of ray. rounding necessary              # compensate binary-decimal errors.              a_ngle = round(math.degrees(math.atan2((y_int - center_point[1]), (x_int - center_point[0]))))              # substitutes positive equivalent every negative angle,              # i.e. -270 degrees equals 90 degrees.             if a_ngle < 0:                 a_ngle = a_ngle + 360              # selects angles correspond theta             if a_ngle == theta[c]:                 angle.append(a_ngle)       print "int1=", intercepts     print "angle=", angle      dist = []      # calculates distance.     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()   

now, how works: code takes 3 inputs: center_point (a point contained in polygon, given in (x,y) coordinates), pairs (the vertices of polygon, given in (x,y) coordinats), , deg_increment ( defines how calculate distance). let's assume center_point = (4,5), pairs = [(1, 4), (3, 8), (7, 2)], , deg_increment = 20. means polygon created (sort of) vertices pairs, , center_point point contained inside polygon. rays set radiate center_point every 20 degrees (which isdeg_increment). intersection points of rays perimeter of polygon determined, , distance calculated using distance formula. problem i'm getting many distances. :( in example above, correct distances 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

but code returning 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]

if can me correct distances, i'd appreciate it. thanks!

and reference, here's example looks with correct distances only:distance

you're getting many values in intercepts because it's being appended inside second for-loop [for d in range(len(pairs))].

you want 1 value in intercept per step through outer for-loop [for c in range((360//deg_increment))], append intercept needs in loop.

i'm not sure you're doing inner loop, seem calculating separate intercept each of lines make polygon sides. want 1 you're going hit "first" when going in direction.

you'll have add code figure out of 3 (in case) sides of polygon you're going encounter first.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -