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