Home > front end >  Best way to find a point near all four points known coordinates
Best way to find a point near all four points known coordinates

Time:01-12

I have the coordinates of four points. Can anyone help me find the coordinates of one point that satisfies the condition: the distances from the finding point to four input points are in the range of 1.9 and 2.5?

import numpy as np

dist_min = 1.9
dist_max = 2.5
# this show no points satisfied
input_points1 = [[ 7.57447956,  6.67658376, 10.79921475],
                [ 8.98026868,  7.69010703, 12.89377068],
                [ 6.22242062,  7.73362942, 12.87947421],
                [ 10.0000000,  9.00000000, 8.500000000]]
#this has
input_points2 = [[ 7.57447956,  6.67658376, 10.79921475],
                 [ 8.98026868,  7.69010703, 12.89377068],
                 [ 6.22242062,  7.73362942, 12.87947421],
                 [ 6.22473072,  4.74175054, 12.96455411]]

def Distance(point1, point2):
    return np.linalg.norm(point1 - point2)

CodePudding user response:

Here a method that finds a random point:

import numpy as np


dist_min = 1.9
dist_max = 2.5
# this show no points satisfied
input_points1 = [[ 7.57447956,  6.67658376, 10.79921475],
                [ 8.98026868,  7.69010703, 12.89377068],
                [ 6.22242062,  7.73362942, 12.87947421],
                [ 10.0000000,  9.00000000, 8.500000000]]
#this has
input_points2 = [[ 7.57447956,  6.67658376, 10.79921475],
                 [ 8.98026868,  7.69010703, 12.89377068],
                 [ 6.22242062,  7.73362942, 12.87947421],
                 [ 6.22473072,  4.74175054, 12.96455411]]

def Distance(point1, point2):
    return np.linalg.norm(np.array(point1) - np.array(point2))


def find_point(input_points):
    dmax = max([Distance(input_points[i], input_points[j]) 
     for i in range(len(input_points)-1) 
     for j in range(i 1, len(input_points))]) 

    if dmax > 2 * dist_max:
        return None

    found = False
    while not found:
        ip = np.random.choice(len(input_points))
        p = np.random.normal(size=3)
        r = np.random.uniform(dist_min, dist_max)
        x = p / np.linalg.norm(p) * r   np.array(input_points[ip])
        found = True
        for i in input_points:
            d = Distance(i, x)
            if d <= dist_min or d >= dist_max:
                found = False
                continue

    return(x)

a = find_point(input_points1)
print(a)  
# NONE

b = find_point(input_points2)
print([Distance(i, b) for i in input_points2])
# [2.4877643881304805, 2.1439232926982417, 2.2860134633791795, 1.9466840567560841]

CodePudding user response:

This looks like something you could use a K-Means (link to Wikipedia) function for with just 1 centroid and then check that the point's distance is the right distance away from all the points in the data. Perhaps not the most elegant or efficient solution, but it should work.

K-Means Code adapted from this tutorial on K-Means:

import pandas as pd
from sklearn.cluster import KMeans

# data is whatever set of points you have
df = pd.DataFrame(data)

# fit k means with 1 centroid lol
k_means = KMeans(n_clusters=1)
labels=k_means.fit_predict(df)
centroid = k_means.cluster_centers_[:,0]

# compare to all points
for point in data:
    assert distance(point, centroid) >= 1.9
    assert distance(point, centroid) <= 2.5
  • Related