V
V
Vladimir2016-11-15 13:15:48
Python
Vladimir, 2016-11-15 13:15:48

How to calculate a point in a rectangle in python?

Good afternoon
There is a rectangle, 4 by 6
further there is a certain point to which the distances from each of the vertices are known.
How to calculate where the given point will be located inside the rectangle (construct graphically by indicating the point)?
Maybe there are already ready-made functions or sets, I did not find it. (how to mathematically go to this is understandable, but cumbersome)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NaName, 2016-11-16
@qDiablo

def find2Points(circle_1, circle_2):
    x_1 = circle_2[0] - circle_1[0]
    y_1 = circle_2[1] - circle_1[1]
    r_0 = circle_1[2]
    r_1 = circle_2[2]
    a = -2 * x_1
    b = -2 * y_1
    c = x_1 ** 2 + y_1 ** 2 + r_0 ** 2 - r_1 ** 2
    x_2 = -(a * c) / (a ** 2 + b ** 2)
    y_2 = -(b * c) / (a ** 2 + b ** 2)
    distance = (x_2 ** 2 + y_2 ** 2) ** (0.5)
    if distance > circle_1[2]:
        coords = ['error']
    elif distance == circle_1[2]:
        coords = [x_2 + circle_1[0], y_2 + circle_1[1]]
    else:
        d = (r_0 ** 2 -(c ** 2) / (a ** 2 + b ** 2)) ** (0.5)
        tmp = ((d ** 2) / (a ** 2 + b ** 2)) ** (0.5)
        x_p1 = x_2 + b * tmp
        y_p1 = y_2 - a * tmp
        x_p2 = x_2 - b * tmp
        y_p2 = y_2 + a * tmp
        coords = , [x_p2 + circle_1[0], y_p2 + circle_1[1]]]
    return coords

def sortRectangle(a, b, c, d):
    tmp = []
    delta = 0.000000001
    coords = find2Points(a, b)
    tmp += coords
    if len(tmp) > 1:
        coords = find2Points(a, c)
        for coord in coords:
            for coord_tmp in tmp:
                if (coord[0] - coord_tmp[0]) ** 2 + (coord[1] - coord_tmp[1]) ** 2 < delta:
                    return coord
    else:
        return tmp

a = [-3, 2, 5]
b = [3, 2, 5]
c = [3, -2, 3]
d = [-3, -2, 3]

print(sortRectangle(a, b, c, d))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question