SHARE
TWEET

Untitled

a guest Nov 19th, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import isclose
  2. from matplotlib import pyplot as plt
  3. from random import choice
  4.  
  5. FILE_NAME = 'dane.txt'
  6. def load_segments(file_name):
  7.     segments = []
  8.     with open(file_name) as file:
  9.         for line in file:
  10.             s = line.split()
  11.             x1 = float(s[0])
  12.             y1 = float(s[1])
  13.             x2 = float(s[2])
  14.             y2 = float(s[3])
  15.             segment = (x1, y1, x2, y2)
  16.             segments.append(segment)
  17.     return segments
  18.  
  19. def is_vertical(segment):
  20.     x1, y1, x2, y2 = segment
  21.     return x1 == x2
  22.  
  23. def is_horizontal(segment):
  24.     x1, y1, x2, y2 = segment
  25.     return y1 == y2
  26.  
  27. def is_connected(a,b):
  28.     x1a, y1a, x2a, y2a = a
  29.     x1b, y1b, x2b, y2b = b
  30.     connected11 = isclose(x1a, x1b) and isclose(y1a, y1b)
  31.     connected22 = isclose(x2a, x2b) and isclose(y2a, y2b)
  32.     connected12 = isclose(x1a, x2b) and isclose(y1a, y2b)
  33.     connected21 = isclose(x2a, x1b) and isclose(y2a, y2b)
  34.     return connected11 or connected22 or connected12 or connected21
  35.  
  36. def plot_segment(segment, code='k-'):
  37.     x1, y1, x2, y2 = segment
  38.     plt.plot([x1, x2], [y1, y2], code)
  39.    
  40. if __name__ == "__main__":
  41.      
  42.       codes = ['r-', 'g-', 'b-', 'm-', 'c-']
  43.      
  44.       segments = load_segments(FILE_NAME)
  45.      
  46.       plt.figure(1)
  47.       for s in segments:
  48.           plot_segment(s, 'y:')
  49.      
  50.       counter = 0
  51.       for s1 in segments:
  52.           if is_vertical(s1):
  53.               for s2 in segments:
  54.                   if is_horizontal(s2):
  55.                       if is_connected(s1, s2):
  56.                           counter += 1
  57.                           code = choice(codes)
  58.                           plot_segment(s1, code)
  59.                           plot_segment(s2, code)
  60.                          
  61.       print('W pliku jest łącznie', len(segments), 'odcinków')
  62.       print('Jest', counter, 'par odcinków spełniających kryterium')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top