t | import random | t | import random |
| | | |
| def check(x1, y1, x2, y2, x3, y3): | | def check(x1, y1, x2, y2, x3, y3): |
| return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0) | | return abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0) |
| | | |
| def randsquare(A, B): | | def randsquare(A, B): |
| (x_1, y_1) = A | | (x_1, y_1) = A |
| (x_2, y_2) = B | | (x_2, y_2) = B |
| x_center = (x_1 + x_2) / 2 | | x_center = (x_1 + x_2) / 2 |
| y_center = (y_1 + y_2) / 2 | | y_center = (y_1 + y_2) / 2 |
| x_3 = x_center - y_1 + y_center | | x_3 = x_center - y_1 + y_center |
| y_3 = y_center + x_1 - x_center | | y_3 = y_center + x_1 - x_center |
| x_4 = x_center - y_2 + y_center | | x_4 = x_center - y_2 + y_center |
| y_4 = y_center + x_2 - x_center | | y_4 = y_center + x_2 - x_center |
| min_x = min(x_1, x_2, x_3, x_4) | | min_x = min(x_1, x_2, x_3, x_4) |
| max_x = max(x_1, x_2, x_3, x_4) | | max_x = max(x_1, x_2, x_3, x_4) |
| min_y = min(y_1, y_2, y_3, y_4) | | min_y = min(y_1, y_2, y_3, y_4) |
| max_y = max(y_1, y_2, y_3, y_4) | | max_y = max(y_1, y_2, y_3, y_4) |
| all_1 = check(x_1, y_1, x_2, y_2, x_3, y_3) | | all_1 = check(x_1, y_1, x_2, y_2, x_3, y_3) |
| while True: | | while True: |
| (x, y) = (random.uniform(min_x, max_x), random.uniform(min_y, max_y)) | | (x, y) = (random.uniform(min_x, max_x), random.uniform(min_y, max_y)) |
| f_1 = check(x, y, x_2, y_2, x_3, y_3) | | f_1 = check(x, y, x_2, y_2, x_3, y_3) |
| s_1 = check(x_1, y_1, x, y, x_3, y_3) | | s_1 = check(x_1, y_1, x, y, x_3, y_3) |
| t_1 = check(x_1, y_1, x_2, y_2, x, y) | | t_1 = check(x_1, y_1, x_2, y_2, x, y) |
| if all_1 == f_1 + s_1 + t_1: | | if all_1 == f_1 + s_1 + t_1: |
| return (x, y) | | return (x, y) |
| f_2 = check(x, y, x_2, y_2, x_4, y_4) | | f_2 = check(x, y, x_2, y_2, x_4, y_4) |
| s_2 = check(x_1, y_1, x, y, x_4, y_4) | | s_2 = check(x_1, y_1, x, y, x_4, y_4) |
| if all_1 == f_2 + s_2 + t_1: | | if all_1 == f_2 + s_2 + t_1: |
| return (x, y) | | return (x, y) |