Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random as rd
- import math as mt
- import matplotlib.pyplot as plt
- ## set this to whatever
- distance = 2
- x = rd.random()*10000
- y = rd.random()*10000
- start_pt = [x,y]
- angle = rd.uniform(0, 360)
- x_1 = x + distance * mt.cos(angle)
- y_1 = y + distance * mt.sin(angle)
- end_pt = [x_1,y_1]
- distance_check = mt.sqrt(((start_pt[0]-end_pt[0])**2)+((start_pt[1]-end_pt[1])**2) )
- ## check
- print(start_pt)
- print(distance_check)
- print(end_pt)
- print(angle)
- ## count how many lines you've crossed
- def find_int_between(x,y):
- if y > x:
- return max(0,mt.ceil(y) - mt.floor(x)-1)
- else:
- return max(0, mt.ceil(x) - mt.floor(y)-1)
- x_axis_cross = find_int_between(end_pt[0],(start_pt[0]))
- y_axis_cross = find_int_between(end_pt[1],(start_pt[1]))
- ## number of lines crossed
- total_cross = abs(x_axis_cross) + abs(y_axis_cross)
- ## check to visualize what you're doing
- x = [start_pt[0],end_pt[0]]
- y = [start_pt[1],end_pt[1]]
- plt.plot(x, y)
- plt.grid(True)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement