f | import random | f | import random |
| | | |
t | def randsquare(A, B): | t | def randsquare(a, b): |
| a = [] | | |
| d = (abs(A[0] - B[0]) ** 2 + abs(A[1] - B[1]) ** 2) ** (1 / 2) | | diameter = (abs(a[0] - b[0]) ** 2 + abs(a[1] - b[1]) ** 2) ** (1 / 2) |
| a = (d ** 2 / 2) ** (1 / 2) | | border = (diameter ** 2 / 2) ** (1 / 2) |
| ca = abs(A[0] - B[0]) / d | | tmp1 = abs(a[0] - b[0]) / diameter |
| sa = abs(A[1] - B[1]) / d | | tmp2 = abs(a[1] - b[1]) / diameter |
| if B[0] < A[0]: | | if b[0] < a[0]: |
| ca = -ca | | tmp1 = -tmp1 |
| if B[1] < A[1]: | | if b[1] < a[1]: |
| sa = -sa | | tmp2 = -tmp2 |
| x = random.uniform(0, a) | | x = random.uniform(0, border) |
| y = random.uniform(0, a) | | y = random.uniform(0, border) |
| q = 1 / 2 ** (1 / 2) | | normalize = 1 / 2 ** (1 / 2) |
| x1 = x * q + y * q | | x_btw = x * normalize + y * normalize |
| y1 = -x * q + y * q | | y_btw = -x * normalize + y * normalize |
| x3 = x1 * ca - y1 * sa + A[0] | | x_fin = x_btw * tmp1 - y_btw * tmp2 + a[0] |
| y3 = +x1 * sa + y1 * ca + A[1] | | y_fin = +x_btw * tmp2 + y_btw * tmp1 + a[1] |
| return (x3, y3) | | return (x_fin, y_fin) |