我需要找到彼此距离最远的两个点。 正如屏幕截图所示,我有一个包含其他两个数组的数组。一个用于X,一个用于Y坐标。确定数据中最长线路的最佳方法是什么?通过这样说,我需要选择情节中最远的两个点。希望你们能帮忙。下面是一些帮助解释问题的截图。
答案 0 :(得分:10)
您可以通过观察相距最远的两个点将作为凸包中的顶点出现来避免计算成对距离。然后,您可以计算较少点之间的成对距离。
例如,在单位正方形中均匀分布100,000个点,在我的实例中凸包中只有22个点。
import numpy as np
from scipy import spatial
# test points
pts = np.random.rand(100_000, 2)
# two points which are fruthest apart will occur as vertices of the convex hull
candidates = pts[spatial.ConvexHull(pts).vertices]
# get distances between each pair of candidate points
dist_mat = spatial.distance_matrix(candidates, candidates)
# get indices of candidates that are furthest apart
i, j = np.unravel_index(dist_mat.argmax(), dist_mat.shape)
print(candidates[i], candidates[j])
# e.g. [ 1.11251218e-03 5.49583204e-05] [ 0.99989971 0.99924638]
如果数据是二维的,您可以在O(n*log(n))
时n
private void loadPlayMusicSongDetails(Context context, String trackId){
String artistName = null;
String albumName = null;
String trackName = null;
long year = 0;
String[] proj = { MediaStore.Audio.AudioColumns.TITLE,
MediaStore.Audio.AudioColumns.ARTIST,
MediaStore.Audio.AudioColumns.ALBUM,
MediaStore.Audio.AudioColumns.YEAR,
MediaStore.Audio.AudioColumns.ALBUM_ID,
PLAY_MUSIC_COLUMN_ARTWORK_LOCATION,
//"artworkUrl",
//"VThumbnailUrl",
MediaStore.Audio.AudioColumns._ID };
Uri gMusicUri = Uri.parse("content://com.google.android.music.MusicContent/audio");
try {
Uri uri = ContentUris.withAppendedId(gMusicUri, Long.parseLong(trackId));
Cursor cursor = context.getContentResolver().query(uri,
proj, null, null, null);
if(cursor.moveToFirst()){
int titleColumn = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
int artistColumn = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
int albumColumn = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM);
int yearColumn = cursor.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR);
int albumArtColumn = cursor.getColumnIndex(PLAY_MUSIC_COLUMN_ARTWORK_LOCATION);
trackName = cursor.getString(titleColumn);
artistName = cursor.getString(artistColumn);
albumName = cursor.getString(albumColumn);
year = cursor.getLong(yearColumn);
String albumArtLocation = cursor.getString(albumArtColumn);
if(albumArtLocation != null && albumArtLocation.length() > 0){
if(albumArtLocation.startsWith("mediastore:")){
long albumId = Long.parseLong(albumArtLocation.substring(albumArtLocation.indexOf(":")+1));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
setAlbumArtUri(ContentUris.withAppendedId(sArtworkUri, albumId));
}
else {
//TODO: perform http calls if you wanna query for online images
}
}
}
点{{1}}中的凸包进行compute。不幸的是,随着维度的增长,性能提升消失了。