f | import random | f | import random |
| from decimal import * | | from decimal import * |
| | | |
| | | |
| def randsquare(t1, t2): | | def randsquare(t1, t2): |
| x1, y1 = t1 | | x1, y1 = t1 |
| x2, y2 = t2 | | x2, y2 = t2 |
| | | |
| if (x1 - x2) == (y2 - y1): | | if (x1 - x2) == (y2 - y1): |
| return (random.uniform(x1, x2), random.uniform(y1, y2)) | | return (random.uniform(x1, x2), random.uniform(y1, y2)) |
| | | |
| cent_x, cent_y = ((x2+x1)/2, (y1+y2)/2) | | cent_x, cent_y = ((x2+x1)/2, (y1+y2)/2) |
| x3, y3 = (cent_x - (cent_y - y1), cent_y + (cent_x - x1)) | | x3, y3 = (cent_x - (cent_y - y1), cent_y + (cent_x - x1)) |
| x4, y4 = (cent_x + (cent_y - y1), cent_y - (cent_x - x1)) | | x4, y4 = (cent_x + (cent_y - y1), cent_y - (cent_x - x1)) |
| lx = [x1, x2, x3, x4] | | lx = [x1, x2, x3, x4] |
| l_x = sorted([x1, x2, x3, x4]) | | l_x = sorted([x1, x2, x3, x4]) |
| x = random.uniform(l_x[0], l_x[3]) | | x = random.uniform(l_x[0], l_x[3]) |
| ly = [y1, y2, y3, y4] | | ly = [y1, y2, y3, y4] |
| | | |
| if x > l_x[2]: | | if x > l_x[2]: |
| g1 = lx.index(l_x[3]) | | g1 = lx.index(l_x[3]) |
| if l_x[3] == l_x[2]: | | if l_x[3] == l_x[2]: |
| g2 = lx.index(l_x[2], g1+1) | | g2 = lx.index(l_x[2], g1+1) |
| else: | | else: |
| g2 = lx.index(l_x[2]) | | g2 = lx.index(l_x[2]) |
| if l_x[1] == l_x[3]: | | if l_x[1] == l_x[3]: |
| g3 = lx.index(l_x[1], g1+1) | | g3 = lx.index(l_x[1], g1+1) |
| elif l_x[1] == l_x[2]: | | elif l_x[1] == l_x[2]: |
| g3 = lx.index(l_x[1], g2+1) | | g3 = lx.index(l_x[1], g2+1) |
| else: | | else: |
| g3 = lx.index(l_x[1]) | | g3 = lx.index(l_x[1]) |
n | #print(lx[g1], ly[g1], lx[g2], ly[g2], lx[g3], ly[g3]) | n | |
| if lx[g1] != lx[g2]: | | if lx[g1] != lx[g2]: |
| k = (ly[g1] - ly[g2])/(lx[g1] - lx[g2]) | | k = (ly[g1] - ly[g2])/(lx[g1] - lx[g2]) |
| b = ly[g1] - k * lx[g1] | | b = ly[g1] - k * lx[g1] |
| y_top = k * x + b | | y_top = k * x + b |
| else: | | else: |
| y_top = y1 | | y_top = y1 |
| | | |
| if lx[g1] != lx[g3]: | | if lx[g1] != lx[g3]: |
| k = (ly[g1] - ly[g3])/(lx[g1] - lx[g3]) | | k = (ly[g1] - ly[g3])/(lx[g1] - lx[g3]) |
| b = ly[g1] - k * lx[g1] | | b = ly[g1] - k * lx[g1] |
| y_bot = k * x + b | | y_bot = k * x + b |
| else: | | else: |
| y_bot = y2 | | y_bot = y2 |
| elif x < l_x[1]: | | elif x < l_x[1]: |
| g1 = lx.index(l_x[0]) | | g1 = lx.index(l_x[0]) |
| if l_x[0] == l_x[2]: | | if l_x[0] == l_x[2]: |
| g2 = lx.index(l_x[2], g1+1) | | g2 = lx.index(l_x[2], g1+1) |
| else: | | else: |
| g2 = lx.index(l_x[2]) | | g2 = lx.index(l_x[2]) |
| if l_x[1] == l_x[0]: | | if l_x[1] == l_x[0]: |
| g3 = lx.index(l_x[1], g1+1) | | g3 = lx.index(l_x[1], g1+1) |
| elif l_x[1] == l_x[2]: | | elif l_x[1] == l_x[2]: |
| g3 = lx.index(l_x[1], g2+1) | | g3 = lx.index(l_x[1], g2+1) |
| else: | | else: |
| g3 = lx.index(l_x[1]) | | g3 = lx.index(l_x[1]) |
| if lx[g1] != lx[g2]: | | if lx[g1] != lx[g2]: |
| k = (ly[g1] - ly[g2])/(lx[g1] - lx[g2]) | | k = (ly[g1] - ly[g2])/(lx[g1] - lx[g2]) |
| b = ly[g1] - k * lx[g1] | | b = ly[g1] - k * lx[g1] |
| y_top = k * x + b | | y_top = k * x + b |
| else: | | else: |
| y_top = y1 | | y_top = y1 |
| if lx[g1] != lx[g3]: | | if lx[g1] != lx[g3]: |
| k = (ly[g1] - ly[g3])/(lx[g1] - lx[g3]) | | k = (ly[g1] - ly[g3])/(lx[g1] - lx[g3]) |
| b = ly[g1] - k * lx[g1] | | b = ly[g1] - k * lx[g1] |
| y_bot = k * x + b | | y_bot = k * x + b |
| else: | | else: |
| y_bot = y2 | | y_bot = y2 |
| else: | | else: |
n | #print("мы тут") | n | |
| g1 = lx.index(l_x[3]) | | g1 = lx.index(l_x[3]) |
| if l_x[3] == l_x[1]: | | if l_x[3] == l_x[1]: |
| g2 = lx.index(l_x[1], g1+1) | | g2 = lx.index(l_x[1], g1+1) |
| else: | | else: |
| g2 = lx.index(l_x[1]) | | g2 = lx.index(l_x[1]) |
| if l_x[2] == l_x[3]: | | if l_x[2] == l_x[3]: |
| g3 = lx.index(l_x[2], g1+1) | | g3 = lx.index(l_x[2], g1+1) |
| elif l_x[2] == l_x[1]: | | elif l_x[2] == l_x[1]: |
| g3 = lx.index(l_x[2], g2+1) | | g3 = lx.index(l_x[2], g2+1) |
| else: | | else: |
| g3 = lx.index(l_x[2]) | | g3 = lx.index(l_x[2]) |
| if (g1 in [0, 1, 2]) and (g2 in [0, 1, 2]) and (g3 in [1, 2, 0]): | | if (g1 in [0, 1, 2]) and (g2 in [0, 1, 2]) and (g3 in [1, 2, 0]): |
| g4 = 3 | | g4 = 3 |
| elif (g1 in [0, 3, 2]) and (g2 in [0, 3, 2]) and (g3 in [0, 2, 3]): | | elif (g1 in [0, 3, 2]) and (g2 in [0, 3, 2]) and (g3 in [0, 2, 3]): |
| g4 = 1 | | g4 = 1 |
| elif (g1 in [3, 1, 2]) and (g2 in [1, 2, 3]) and (g3 in [1, 2, 3]): | | elif (g1 in [3, 1, 2]) and (g2 in [1, 2, 3]) and (g3 in [1, 2, 3]): |
| g4 = 0 | | g4 = 0 |
| else: | | else: |
| g4 = 2 | | g4 = 2 |
n | #print(lx[g1], lx[g2], lx[g3], lx[g4]) | n | |
| if lx[g1] != lx[g2]: | | if lx[g1] != lx[g2]: |
| k = Decimal(ly[g1] - ly[g2])/Decimal(lx[g1] - lx[g2]) | | k = Decimal(ly[g1] - ly[g2])/Decimal(lx[g1] - lx[g2]) |
| b = Decimal(ly[g1]) - k * Decimal(lx[g1]) | | b = Decimal(ly[g1]) - k * Decimal(lx[g1]) |
| y_top = k * Decimal(x) + b | | y_top = k * Decimal(x) + b |
| else: | | else: |
| y_top = y1 | | y_top = y1 |
| if lx[g3] != lx[g4]: | | if lx[g3] != lx[g4]: |
n | #print("точки ", lx[g3], lx[g4], ly[g3], ly[g4]) | n | |
| #print(ly[g3] - ly[g4]) | | |
| #print(lx[g3] - lx[g4]) | | |
| k = Decimal(ly[g3] - ly[g4])/Decimal(lx[g3] - lx[g4]) | | k = Decimal(ly[g3] - ly[g4])/Decimal(lx[g3] - lx[g4]) |
| b = Decimal(ly[g3]) - k * Decimal(lx[g3]) | | b = Decimal(ly[g3]) - k * Decimal(lx[g3]) |
| y_bot = k * Decimal(x) + b | | y_bot = k * Decimal(x) + b |
n | #print("k =, b =", k, b) | n | |
| else: | | else: |
| y_bot = y2 | | y_bot = y2 |
| | | |
t | #print("y top = ", y_top, y_bot) | t | |
| y = random.uniform(float(y_top), float(y_bot)) | | y = random.uniform(float(y_top), float(y_bot)) |
| return (x, y) | | return (x, y) |
| | | |
| #print(randsquare((0, 0), (100, 101))) | | #print(randsquare((0, 0), (100, 101))) |
| | | |