Find intersection between line and shape file with python - python

I'm trying to clip lines based on a shape-file in python. I have a script that works, but it is very slow. Are there a faster way to do this? A prerequisite are that I have to do it with python.
from shapely.geometry import LineString, shape, Point
from shapely.affinity import rotate
from itertools import chain
from shapely.ops import cascaded_union
from fiona import open
import numpy as np
# open the shp-file with land geometry
shoreline = open('land_boundary.shp')
shapes = [shape(f['geometry']) for f in shoreline]
mergedshorelines = cascaded_union(shapes)
# create an arbitrary line
x,y = 696346,6601295
x_end, y_end = 746345,6601295
line1 = LineString([(x, y), (x_end, y_end)])
# Creates lines from arbitrary line with 1 degree step
radii = [rotate(line1, deg, (x,y)) for deg in chain(np.mod(np.arange(0,360,1),360))]
mergedradii = cascaded_union(radii)
# the intersection between the two multilines is computed and the intersection point with the smallest distance is choosen
point = Point(x,y)
points_ok = []
#----THIS IS THE SLOW PART-------
# clip lines with land boundary
for line in mergedradii:
if line.intersects(mergedshorelines):
if line.intersection(mergedshorelines).type == "MultiPoint":
# multiple points: select the point with the minimum distance
a = {}
for pt in line.intersection(mergedshorelines):
a[point.distance(pt)] = pt
points_ok.append(a[min(a.keys())])
if line.intersection(mergedshorelines).type == "Point":
# ok, only one intersection
points_ok.append(line.intersection(mergedshorelines))
Shoreline_points = cascaded_union(points_ok) # coordinates of all intersection.
Appreciate any input!
Cheers!
/Björn

You can save a lot of time by calling intersection just once and saving the result, instead of calling it multiple times. I call the intersection method once at the top of the loop and save it as a variable, then refer to the variable in the logic, instead of running it again.
You can also save time by skipping the intersects check entirely, because if a line doesn't intersect with mergedshorelines, it won't be a MultiPoint or Point object, it will be a LineString.
You can also optimize the part of the script that looks for the closest point in a MultiPoint. When trying to find the closest point to the origin of the line, you only need to check the first and last point in a MultiPoint, because the points are sorted. So either the first or last point will be closest to the origin.
for line in mergedradii:
intersection = line.intersection(mergedshorelines)
if intersection.type == "MultiPoint":
# multiple points: select the point with the minimum distance
first_pt = intersection[0]
last_pt = intersection[-1]
if point.distance(first_pt) < point.distance(last_pt):
points_ok.append(first_pt)
else:
points_ok.append(last_pt)
elif intersection.type == "Point":
# ok, only one intersection
points_ok.append(intersection)
Shoreline_points = cascaded_union(points_ok) # coordinates of all intersection.
On my computer, this script runs in about 40 seconds, while the original script took around 190 seconds.

Related

Determine which intersection result further along a line in Shapely

Context
(minimum working example code at the end)
I am trying to create a line smoothing algorithm: From the first line vertex, a circle of a specified radius is created and this circle is intersected with the line (LineString). This results into 1 to many intersection results (Points). The farthest intersection point along the line is then taken, a new circle is created, intersected with the line and so on until the line endpoint is approached.
Visualized result with an oddly-passing parameters is in the image below. The blue line is the input, the green line is the smoothed result. The line is in the UTM Projection, but it doesn't matter.
The issue
Now I roughly know how to design most of the algorithm (I can pass it on full here if successful).
What I don't know is how to always choose the farthest intersection point along the line from the current intersection results. Shapely utilizes Point or MultiPoint as an interserciton result in this case. If I reach MultiPoint parts through geoms, they're probably ordered by ascending x coordinate (not sure though).
Attempted solution
I first thought I'd always take the last member from the circle_intersections.geoms. As you probably guess, if I apply this to the algorithm, this wouldn't work once the line changes its direction against the ascending x coordinate. Then, the intersection point I need is usually the first (or other) from the list. And thus, it gets into infinite loop, passing the intersection points between two circles.
In the image above, it worked just because the radius was set such that the searched intersection point was always further in x coordinate.
Secondly, I tried to iterate through the line and filter out the intersection point which splits the line farthest along. Here I must be making some kind of mistake I don't see.
You find both attempts in the find_farthest_intersection function.
Code
Imports:
import shapely
from shapely.geometry import Point, LineString
from shapely.ops import split
import matplotlib.pyplot as plt
The algorithm:
def smooth(point, geometry, r, first_pass=True):
circle = point.buffer(r)
circle_intersections = geometry.intersection(circle.exterior)
# It's always a single point at the first iteration
if isinstance(circle_intersections, Point):
next_point = circle_intersections
else:
next_point = find_farthest_intersection(
geometry,
circle_intersections
)
# If you want to visualize in Jupyter
# plt.plot(
# *point.buffer(6).exterior.xy,
# *circle.exterior.xy,
# *next_point.buffer(5).exterior.xy,
# )
if not first_pass and Point(geometry.coords[-1]).within(circle):
return [geometry.coords[-1]]
return [point.coords[0]] + smooth(
next_point,
geometry,
r,
first_pass=False
)
def find_farthest_intersection(geometry, intersections):
# How to return the right intersection furthest along the line?
# Naive and wrong solution
# When the line heads against X this becomes the opposite point than I want
# return circle_intersections.geoms[-1]
# Other attempted solution
# Not working, results in the same problem as with the solution above.
# Find each intersection point's distance from the
# line beginning
intersection_pnt_dsts_from_line_start = [
split(geometry, point).geoms[0].length
for point in intersections.geoms
]
# Return the intersection point with the maximum
# distance from the line beginning
return max(
zip(
intersection_pnt_dsts_from_line_start,
intersections.geoms
),
key=lambda item: item[0]
)[1]
If you want to visualize in eg. Jupyter (also uncomment pyplots in the smooth function).
# it's in UTM btw
line = shapely.wkt.loads('LINESTRING (478098.5964211893 5442495.543688663, 478229.0423690955 5442525.981076508, 478242.0869638861 5442558.592563484, 478198.6049812507 5442595.552248725, 478129.033809034 5442649.904727018, 478168.1675934059 5442691.212610522, 478209.4754769095 5442665.123420941, 478209.4754769095 5442628.163735701, 478213.8236751731 5442615.11914091, 478231.2164682273 5442599.900446988, 478252.957459545 5442593.378149592, 478305.1358387075 5442602.07454612, 478322.5286317617 5442652.078826151, 478329.0509291569 5442732.520494026, 478383.4034074512 5442762.957881871, 478353.5095443893 5442726.541721413, 478421.4501422573 5442696.37609596, 478471.7261846794 5442718.117087278, 478434.7664994393 5442728.987582936, 478399.9809133308 5442732.248731634, 478458.6815898888 5442757.250871649, 478526.0786629738 5442780.078912533, 478532.6009603692 5442848.563035184, 478579.3440917023 5442883.348621292)')
r = 100 # radius parameter
plt.plot(*line.xy)
result = smooth(
Point(line.coords[0]), # first time it's the first line node
line,
r # smaller number or more complicated lines cause fail
)
# Would then do
# plt.plot(*LineString(result).xy)
So I played around more with geometric operations and with help of #50194077 found the solution is rather simple. I can split the line by each intersection point and measure the distance from the line beginning of the first split result. The longest distance points to the farthest intersection point, i.e. the intersection point I want.
The key to facilitate the split is to create a buffer of the intersection point. Its size should addresses shapely precision issues and be as low as possible (eg 1Oe-9).
Revised function:
def find_farthest_intersection(geometry, intersection_points):
intersection_points_from_geom_distance = []
for intersection_point in intersection_points:
# Create a little buffer
precision_buffer = intersection_point.buffer(10e-9)
# Split line on buffer and take the first line fragment
intersection_points_from_geom_distance.append([
intersection_point,
split(line, precision_buffer)[0].length]
)
# Return the intersection point with the maximum
# distance from the line beginning
return max(
intersection_points_from_geom_distance,
key=lambda item: item[1]
)[0]

How to use values in list from one function in a second function

I am working on coding a precalculous problem. I was able to write one function and get an (x,y) plot in a python list. I want to be able to call list[0] and list[1] of the first function in a second function. How do I setup my functions so that I can use the outputs from the first function as inputs in the second functions?
import numpy as np
import sympy as sy
# find the point of intersection of two points by using a linear function
def intersection(slope1, yintercept1, slope2, yintercept2):
xval = -1*slope1+slope2
recip = np.reciprocal(xval)
xval = yintercept1*recip
yval = slope2*xval
point = [xval, yval]
return point
print(intersection((sy.Rational(-6,7)), 9, sy.Rational(7,6), 0))
#find the distance two points are apart.
def distance(x, y):
#I want to call the point from my intersection function here and put them into the distance formula
to find out how far away the point is from the origin.
print(distance(0,0))
Your function intersection is returning a list, so you save that into a variable, then you call the distance function with this variable that you got from intersection
import math #for numeric sqrt you import math
def distance(p):
x=p[0]
y=p[1]
return math.sqrt(x**2 + y**2) # euclidian distance to (0,0)
point = intersection((sy.Rational(-6,7)), 9, sy.Rational(7,6), 0))
d = distance(point)
I found my problem. I was printing my function after I was finished running it and I needed to save the value of the function into a variable. Once I saved it to a new variable then I could use that variable in the next function. Now I am having a problem dealing with rational numbers and taking the square root using sympy.
from numpy import reciprocal, sqrt
from sympy import Rational
'''Find the area of a right triangle by finding a point
where two perpendicular lines intersect, and by finding the distance
the point is away from the origin'''
# Enter the slope and y intercept for each of the perpendicular lines.
slope_line_one = Rational(-6,7)
y_intercept_line_one = 9
slope_line_two = Rational(7,6)
y_intercept_line_two = 0
perpendicular_lines = [slope_line_one, y_intercept_line_one, slope_line_two,
y_intercept_line_two]
# A function to find the (x,y) value where two perpendicular points intersect
def intersection(slope_one, y_intercept_one, slope_two, y_intercept_two):
xval = -1*slope_one+slope_two
recip = reciprocal(xval)
xval = y_intercept_one*recip
yval = slope_two*xval
point = [xval, yval]
return point
point_of_intersection = (intersection(perpendicular_lines[0], perpendicular_lines[1],
perpendicular_lines[2], perpendicular_lines[3]))
print(point_of_intersection)
# This function is to find how far away the point made by the perpendicular lines is
from the origin
origin = [0, 0]
def distance(x_two, x_one, y_two, y_one):
xdistance = (x_two-x_one)**2
return xdistance
length_leg_one = (distance(origin[0], point_of_intersection[0], origin[1],
point_of_intersection[1]))
print(length_leg_one)

Finding Intersections Region Based Trajectories vs. Line Trajectories

I have two trajectories (i.e. two lists of points) and I am trying to find the intersection points for both these trajectories. However, if I represent these trajectories as lines, I might miss real world intersections (just misses).
What I would like to do is to represent the line as a polygon with certain width around the points and then find where the two polygons intersect with each other.
I am using the python spatial library but I was wondering if anyone has done this before. Here is a picture of the line segments which don't intersect because they just miss each other. Below is the sample data code that represents the trajectory of two objects.
object_trajectory=np.array([[-3370.00427248, 3701.46800775],
[-3363.69164715, 3702.21408203],
[-3356.31277271, 3703.06477984],
[-3347.25951787, 3704.10740164],
[-3336.739511 , 3705.3958357 ],
[-3326.29355823, 3706.78035903],
[-3313.4987339 , 3708.2076586 ],
[-3299.53433345, 3709.72507366],
[-3283.15486406, 3711.47077376],
[-3269.23487255, 3713.05635557]])
target_trajectory=np.array([[-3384.99966703, 3696.41922372],
[-3382.43687562, 3696.6739521 ],
[-3378.22995178, 3697.08802862],
[-3371.98983789, 3697.71490469],
[-3363.5900481 , 3698.62666805],
[-3354.28520354, 3699.67613798],
[-3342.18581931, 3701.04853915],
[-3328.51519511, 3702.57528111],
[-3312.09691577, 3704.41961271],
[-3297.85543763, 3706.00878621]])
plt.plot(object_trajectory[:,0],object_trajectory[:,1],'b',color='b')
plt.plot(vehicle_trajectory[:,0],vehicle_trajectory[:,1],'b',color='r')
Let's say you have two lines defined by numpy arrays x1, y1, x2, and y2.
import numpy as np
You can create an array distances[i, j] containing the distances between the ith point in the first line and the jth point in the second line.
distances = ((x1[:, None] - x2[None, :])**2 + (y1[:, None] - y2[None, :])**2)**0.5
Then you can find indices where distances is less than some threshold you want to define for intersection. If you're thinking of the lines as having some thickness, the threshold would be half of that thickness.
threshold = 0.1
intersections = np.argwhere(distances < threshold)
intersections is now a N by 2 array containing all point pairs that are considered to be "intersecting" (the [i, 0] is the index from the first line, and [i, 1] is the index from the second line). If you want to get the set of all the indices from each line that are intersecting, you can use something like
first_intersection_indices = np.asarray(sorted(set(intersections[:, 0])))
second_intersection_indices = np.asarray(sorted(set(intersections[:, 1])))
From here, you can also determine how many intersections there are by taking only the center value for any consecutive values in each list.
L1 = []
current_intersection = []
for i in range(first_intersection_indices.shape[0]):
if len(current_intersection) == 0:
current_intersection.append(first_intersection_indices[i])
elif first_intersection_indices[i] == current_intersection[-1]:
current_intersection.append(first_intersection_indices[i])
else:
L1.append(int(np.median(current_intersection)))
current_intersection = [first_intersection_indices[i]]
print(len(L1))
You can use these to print the coordinates of each intersection.
for i in L1:
print(x1[i], y1[i])
Turns out that the shapely package already has a ton of convinience functions that get me very far with this.
from shapely.geometry import Point, LineString, MultiPoint
# I assume that self.line is of type LineString (i.e. a line trajectory)
region_polygon = self.line.buffer(self.lane_width)
# line.buffer essentially generates a nice interpolated bounding polygon around the trajectory.
# Now we can identify all the other points in the other trajectory that intersects with the region_polygon that we just generated. You can also use .intersection if you want to simply generate two polygon trajectories and find the intersecting polygon as well.
is_in_region = [region_polygon.intersects(point) for point in points]

Algorithm to close a polygon

I have a part of perimeter of a polygon and need to close it.Please refer this image
As I can see there is only one unique way to close the polygon without dividing the polygon and without the edges intersecting.
And the closing edges would be b->c,d->e,f->g,h->a
Is there any algo to achieve this?
I can think of only one brute force method, try every possible combination and check if it forms a closed polygon(Any good algos to check if it is closed polygon?)
Is there any better way or a known algorithm?
Note: The vertices should be connected by single straight lines only and polygon is not necessarily convex
Also, You can safely assume that these segments always form a polygon because I get these line segments from a polygon and Im trying to recreate the polygon
I think that in "well-behaved" (small gaps, not too irregular shape, etc.) cases, one might get away with following approach. The idea is to assume that the solution (particular permutation of the input line segments which are then assumed to be connected with straight lines) minimizes the length of the resulting MultiLineString defining the boundary of the polygon of interest.
To tackle this problem, the implementation below uses the 2-opt heuristic to the traveling salesman problem. It proceeds in following steps:
the set of vertices is defined as the union of the endpoints of all input line segments
it tries to connect these points in order to minimize the total length of the resulting MultiLineString under the constraint that the points belonging to the same input line segment are always connected (i.e., the 2-opt algorithm is allowed to split only edges connecting different line segments - this is handled by the extra if condition in the main double for-loop).
The result is then:
import logging
import random
import sys
from shapely.geometry import LineString, Polygon
from shapely.ops import polygonize, linemerge
#prevent shapely from showing an error message on is_valid tests
logger = logging.getLogger()
logger.setLevel(logging.ERROR)
#input lines (LineStrings)
lines = [
[(3.15,3.94), (4.06,3.91), (4.27,3.49)],
[(0.84,2.99), (0.97,3.67), (1.68,3.91), (2.68,3.92)],
[(4.46,3.23), (5.12,2.97), (4.60,2.00)],
[(4.13,1.44), (4.41,0.68), (1.14,1.99)]
]
random.shuffle(lines)
N, pnts = 0, []
pnt2line = {}
for line_id, line in enumerate(lines):
#for each line, save its endpoints and remember
#to which line each point belongs
for pnt in [line[0], line[-1]]:
pnt2line[N] = line_id
pnts.append(pnt)
N += 1
#as initial guess, try to connect these points sequentially
route = [i for i in range(0, N)]
def nrm_idx(N, idx):
return (N + idx) % N
def get_polygon(route):
#for given route, attempt to construct the resulting polygon
segments = []
m = len(route)
for idx in range(0, m):
i, j = route[idx], route[nrm_idx(m, idx+1)]
if pnt2line[i] == pnt2line[j]:
#if two consecutive points belong to the same line, consider this line
segments.append(lines[pnt2line[i]])
else:
#otherwise, connect these points with a straight line
segments.append([pnts[i], pnts[j]])
return Polygon(linemerge(segments))
def get_weight(route):
P = get_polygon(route)
return P.length if P.is_valid else sys.maxsize
def edge_is_fixed(pnt_i, pnt_j):
#check if an edge specified by point pnt_i/pnt_j can be dissected or not
#in the latter case, the points belong to the same line/line segment
return (pnt2line[pnt_i] == pnt2line[pnt_j])
def opt_swap(route, i, k):
#perform 2-opt swap
return route[0:i] + route[i:k+1][::-1] + route[k+1:]
flag = True
while flag:
flag = False
best_weight = get_weight(route)
for i in range(0, N-1):
for k in range(i+1, N):
if edge_is_fixed(route[nrm_idx(N, i-1)], route[i]) or edge_is_fixed(route[k], route[nrm_idx(N, k+1)]):
continue
new_route = opt_swap(route, i, k)
weight = get_weight(new_route)
if weight < best_weight:
route = new_route[:]
best_weight = weight
flag = True
P = get_polygon(route)
for x, y in P.exterior.coords:
print(x, y)
For your input (approximated), the result is then indeed:
Here's something that might work:
- Make a set containing only the open points (points that are only on one edge, i.e. the labelled ones in your diagram)
- Run a convex hull algorithm on that set
- Use the edges of the convex hull to complete the polygon with the existing edges. (I.e if the convex hull contains A->B, but A and B are already indirectly connected via adjacent edges in your pre-existing set of edges, discard the edge A->B in the convex hull)
EDIT
I previously suggested co-opting convex hull algorithms but that approach has shortcomings, including the case where the points would not make a convex shape.
Note that, according to your stipulations, there are sets that would not have solutions, such as:
(it's not possible to complete this into a polygon with no crossing lines, using only single straight lines between open points)

Choosing correct distance from a list

This question is somewhat similar to this. I've gone a bit farther than the OP, though, and I'm in Python 2 (not sure what he was using).
I have a Python function that can determine the distance from a point inside a convex polygon to regularly-defined intervals along the polygon's perimeter. The problem is that it returns "extra" distances that I need to eliminate. (Please note--I suspect this will not work for rectangles yet. I'm not finished with it.)
First, the code:
#!/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()
Now, as to how it works:
The code takes 3 inputs: center_point (a point contained in the polygon, given in (x,y) coordinates), Pairs (the vertices of the polygon, also given in (x,y) coordinats), and deg_Increment ( which defines how often to calculate distance).
Let's assume that center_point = (4,5), Pairs = [(1, 4), (3, 8), (7, 2)], and deg_Increment = 20. This means that a polygon is created (sort of) whose vertices are Pairs, and center_point is a point contained inside the polygon.
Now rays are set to radiate from center_point every 20 degrees (which isdeg_Increment). The intersection points of the rays with the perimeter of the polygon are determined, and the distance is calculated using the distance formula.
The only problem is that I'm getting too many distances. :( In my example above, the correct distances are
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 my code is 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 anyone can help me get only the correct distances, I'd greatly appreciate it.
Thanks!
And just for reference, here's what my example looks like with the correct distances only:
You're getting too many values in Intercepts because it's being appended to inside the second for-loop [for d in range(len(Pairs))].
You only want one value in Intercept per step through the outer for-loop [for c in range((360//deg_Increment))], so the append to Intercept needs to be in this loop.
I'm not sure what you're doing with the inner loop, but you seem to be calculating a separate intercept for each of the lines that make up the polygon sides. But you only want the one that you're going to hit "first" when going in that direction.
You'll have to add some code to figure out which of the 3 (in this case) sides of the polygon you're actually going to encounter first.

Categories

Resources