Advertisement
Guest User

intersecting_lines.py

a guest
May 25th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. import sys
  2. import matplotlib.pyplot as plt
  3. import random
  4.  
  5. def in_range(a,b,c):
  6.     '''
  7.    Returns true if b <= a <= c
  8.    '''
  9.     return a >= b and a <= c
  10.    
  11. def is_equal(a,b):
  12.     '''
  13.    Returns True if floats are almost equal, or if both are None
  14.    '''
  15.     if a == None and b == None:
  16.         return True
  17.     elif a == None or b == None:
  18.         return False
  19.     epsilon = 1e-10
  20.     return abs(a-b) < epsilon
  21.    
  22. def random_segments(x,y,n):
  23.     '''
  24.    Makes n random segments in x,y grid
  25.    Prints them to file 'input.txt'
  26.    '''
  27.     with open('input.txt','w') as FH:
  28.         for i in xrange(n):
  29.             x1 = random.random()*x*2 - x
  30.             x2 = random.random()*x*2 - x
  31.             y1 = random.random()*y*2 - y
  32.             y2 = random.random()*y*2 - y
  33.             if x1 > x2:
  34.                 x2,x1 = x1,x2
  35.             l = [i,x1,y1,x2,y2]
  36.             l = map(str,l)
  37.             l.append('\n')
  38.             s = ' '.join(l)
  39.             FH.write(s)
  40.     return True
  41.        
  42. #filename = sys.argv[1]
  43. filename = 'input.txt'
  44. x_range = 10
  45. y_range = 10
  46. n_segments = 20
  47. random_segments(x_range,y_range,n_segments)
  48. pltdata = []
  49. x_intersect = []
  50. y_intersect = []
  51.  
  52. with open(filename) as FH:
  53.     data = []
  54.     for line in FH:
  55.         l = line.rstrip().split()
  56.         label = l.pop(0)
  57.         l = map(float,l)
  58.         pltdata.append((l[0],l[2]))
  59.         pltdata.append((l[1],l[3]))
  60.         pltdata.append('g')
  61.         if l[0] > l[2]:
  62.             print 'input error - x1 should always be less than x2'
  63.         try:
  64.             m = (l[1] - l[3]) / (l[0] - l[2])
  65.             b = l[1] - m * l[0]
  66.         except ZeroDivisionError:
  67.             m = None
  68.             b = None
  69.         data.append((label,m,b,l[0],l[2],min(l[1],l[3]),max(l[1],l[3])))
  70.  
  71.     for counter,i in enumerate(data):
  72.         label,m,b,x_min,x_max,y_min,y_max = i
  73.        
  74.         for j in range(counter+1,len(data)):
  75.             label2,m2,b2,x_min2,x_max2,y_min2,y_max2 = data[j]
  76.             intersect = ()
  77.                
  78.             if is_equal(m,m2) and not is_equal(b,b2):
  79.                 pass
  80.             elif m == None and m2 == 0:
  81.                 # line1 vertical, line2 horizontal
  82.                 if in_range(x_min,x_min2,x_max2) and in_range(y_min2,y_min,y_max):
  83.                     intersect = (x_min,y_min2)
  84.             elif m == 0 and m2 == None:
  85.                 # line1 horizontal, line2 vertical
  86.                 if in_range(x_min2,x_min,x_max) and in_range(y_min,y_min2,y_max2):
  87.                     intersect = (x_min2,y_min)
  88.             elif m == None and m2 == None and is_equal(x_min,x_min2):
  89.                 # two vertical segments on same line
  90.                 if is_equal(y_max,y_min2):
  91.                     intersect = (x_min,y_max)
  92.                 elif is_equal(y_min,y_max2):
  93.                     intersect = (x_min,y_min)
  94.                 elif in_range(y_min,y_min2,y_max2) and in_range(y_max,y_min2,y_max2):
  95.                     # first line subset of second line
  96.                     intersect = ((x_min,y_min),(x_max,y_max))
  97.                 elif in_range(y_min2,y_min,y_max) and in_range(y_max2,y_min,y_max):
  98.                     # second line subset of first line
  99.                     intersect = ((x_min2,y_min2),(x_max2,y_max2))
  100.                 elif in_range(y_max,y_min2,y_max2):
  101.                     # second line overlaps top of first line
  102.                     intersect = ((x_min,y_min2),(x_min,y_max))
  103.                 elif in_range(y_min,y_min2,y_max2):
  104.                     # second line overlaps bottom of first line
  105.                     intersect = ((x_min,y_min),(x_min,y_max2))
  106.             elif is_equal(m,m2) and is_equal(b,b2):
  107.                 # same line, see if they intersect at a single point
  108.                 if is_equal(x_min,x_max2):
  109.                     intersect = (x_min,m*x_min+b)
  110.                 elif is_equal(x_max,x_min2):
  111.                     intersect = (x_max,m*x_max+b)
  112.                 elif in_range(x_min,x_min2,x_max2) and in_range(x_max,x_min2,x_max2):
  113.                     # first line subset of second line
  114.                     intersect = ((x_min,m*x_min+b),(x_max,m*x_max+b))
  115.                 elif in_range(x_min2,x_min,x_max) and in_range(x_max2,x_min,x_max):
  116.                     # second line subset of first line
  117.                     intersect = ((x_min2,m2*x_min2+b2),(x_max2,m2*x_max2+b2))
  118.                 elif in_range(x_min2,x_min,x_max):
  119.                     # second line overlaps right side of first line
  120.                     intersect = ((x_min2,m2*x_min2+b2),(x_max,m*x_max+b))
  121.                 elif in_range(x_max2,x_min,x_max):
  122.                     # second line overlaps left side of first line
  123.                     intersect = ((x_min,m*x_min+b),(x_max2,m2*x_max2+b))
  124.             elif m == None:
  125.                 # first line is vertical, second is not vert or hor
  126.                 if in_range(x_min,x_min2,x_max2) and in_range(m2*x_min+b2,y_min2,y_max2):
  127.                     intersect = (x_min,m2*x_min+b2)
  128.             elif m2 == None:
  129.                 # second line is vertical, first is not vert or hor
  130.                 if in_range(x_min2,x_min,x_max) and in_range(m*x_min2+b,y_min,y_max):
  131.                     intersect = (x_min2,m*x_min2+b)
  132.             else:
  133.                 try:
  134.                     x = (b2 - b) / (m - m2)
  135.                 except ZeroDivisionError:
  136.                     print m,m2,i,data[j]
  137.                 if in_range(x,x_min,x_max) and in_range(x,x_min2,x_max2):
  138.                     intersect = (x,m*x+b)
  139.             if intersect:
  140.                 if len(intersect) == 2:
  141.                     x_intersect.append(intersect[0])
  142.                     y_intersect.append(intersect[1])
  143.                
  144. plt.axis([-x_range,x_range,-y_range,y_range])                
  145. plt.plot(*pltdata)
  146. plt.plot([x_intersect],[y_intersect],'ro')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement