Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import isclose
- from matplotlib import pyplot as plt
- from random import choice
- FILE_NAME = 'dane.txt'
- def load_segments(file_name):
- segments = []
- with open(file_name) as file:
- for line in file:
- s = line.split()
- x1 = float(s[0])
- y1 = float(s[1])
- x2 = float(s[2])
- y2 = float(s[3])
- segment = (x1, y1, x2, y2)
- segments.append(segment)
- return segments
- def is_vertical(segment):
- x1, y1, x2, y2 = segment
- return x1 == x2
- def is_horizontal(segment):
- x1, y1, x2, y2 = segment
- return y1 == y2
- def is_connected(a,b):
- x1a, y1a, x2a, y2a = a
- x1b, y1b, x2b, y2b = b
- connected11 = isclose(x1a, x1b) and isclose(y1a, y1b)
- connected22 = isclose(x2a, x2b) and isclose(y2a, y2b)
- connected12 = isclose(x1a, x2b) and isclose(y1a, y2b)
- connected21 = isclose(x2a, x1b) and isclose(y2a, y2b)
- return connected11 or connected22 or connected12 or connected21
- def plot_segment(segment, code='k-'):
- x1, y1, x2, y2 = segment
- plt.plot([x1, x2], [y1, y2], code)
- if __name__ == "__main__":
- codes = ['r-', 'g-', 'b-', 'm-', 'c-']
- segments = load_segments(FILE_NAME)
- plt.figure(1)
- for s in segments:
- plot_segment(s, 'y:')
- counter = 0
- for s1 in segments:
- if is_vertical(s1):
- for s2 in segments:
- if is_horizontal(s2):
- if is_connected(s1, s2):
- counter += 1
- code = choice(codes)
- plot_segment(s1, code)
- plot_segment(s2, code)
- print('W pliku jest łącznie', len(segments), 'odcinków')
- print('Jest', counter, 'par odcinków spełniających kryterium')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement