Help us understand the problem. What is going on with this article?

pythonのnp.array配列に格納した2次元座標から、入力した座標に最も近い座標を取得した際の備忘録です

More than 1 year has passed since last update.

目的

pythonのnp.array配列に格納した2次元座標から、入力した座標に最も近い座標を取得した際の備忘録です

コード

sample.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import numpy as np

PI = 3.1415
coordinate = np.array([
        # x, y, th
        [0.5, 0, PI],
        [0.9, 0, PI],
        [0.9, -0.5, PI],
        [0.9, 0.5, PI],
        [0.9, 0, PI],
        [0, 0.5, -1*PI/2],
        [0, 0.5, 0],
        [0, 0.5, PI],
        [-0.5, 0, 0],
        [-0.9, 0, 0],
        [-0.9, 0.5, 0],
        [-0.9, -0.5, 0],
        [-0.9, 0, 0],
        [0, -0.5, PI/2],
        [0, -0.5, 0],
        [0, -0.5, PI],
        [0, -0.5, PI/4]
])

# 点p0に一番近い点を取得
def func_search_neighbourhood(p0, ps):
    L = np.array([])
    for i in range(ps.shape[0]):
        norm = np.sqrt( (ps[i][0] - p0[0])*(ps[i][0] - p0[0]) +
                        (ps[i][1] - p0[1])*(ps[i][1] - p0[1]) )
        L = np.append(L, norm)
    return np.argmin(L) ,ps[np.argmin(L)]


def main():
    # target coordinate
    _x = 0
    _y = -0.7

    target_coor = np.array([_x, _y])
    idx, nearrest_coor = func_search_neighbourhood(target_coor, coordinate)
    print( len( coordinate ) )
    print( idx, nearrest_coor[0], nearrest_coor[1], nearrest_coor[2] ) # idx, (x, y, th)

if __name__ == '__main__':
    main()

実行結果

$ python sample.py
17
13 0.0 -0.5 1.57075

参考

Python/NumPyで2点間の距離を計算
辞書内のPythonの最も近い座標
Python/Numpyで最も近い点を探索
Finding the closest point to a list of points

seigot
# 投稿内容は私個人の意見であり、所属企業・部門見解を代表するものではありません。 # 投稿内容は執筆時点の情報であり、必ずしも最新情報であるとはかぎりません。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした