- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我们有 2 个列表(黑色和红色),每个列表包含 3d 空间中的多个点。我们必须将每个黑点移动到一个红点,并以移动的总距离尽可能小的方式进行。列表可以有不同的大小。
如果列表的大小不同,那么我们要么将点堆叠在一起,要么将一个点拆分为多个点。
我们对这个问题的最佳尝试遵循以下一般步骤:
如果红点多于黑点,则选择距离所有红点最远的黑点,并将其与离其位置最近且尚未匹配的红点匹配。
重复步骤 1,直到所有黑点都匹配。
注意:如果黑点多于红点,那么第一步将寻找最远的红点并将其与最近的黑点匹配,然后交换颜色继续进行所有相同的操作。
一些C#代码:
private void SaveFrames(List<List<Vector3>> frameList) {
List<Dictionary<Vector3, List<Vector3>>> resultingPairs = new List<Dictionary<Vector3, List<Vector3>>>();
for (int iFrame = 0; iFrame < frameList.Count+1; iFrame++) {
List<Vector3> currentFrame = frameList[iFrame % frameList.Count];
List<Vector3> nextFrame = frameList[(iFrame + 1) % frameList.Count];
int maxIterations = Mathf.Min(currentFrame.Count, nextFrame.Count);
Dictionary<Vector3, List<Vector3>> pairs = new Dictionary<Vector3, List<Vector3>>();
HashSet<Vector3> takenRed = new HashSet<Vector3>();
HashSet<Vector3> takenBlack = new HashSet<Vector3>();
HashSet<Vector3> takenDestination = new HashSet<Vector3>();
bool moreRed = currentFrame.Count < nextFrame.Count;
if (moreRed) {
for (int i = 0; i < maxIterations; i++) {
// Find furthest black point from any red point
float distance = 0;
Vector3 furthestBlack = Vector3.zero;
foreach (Vector3 black in currentFrame) {
if (takenBlack.Contains(black)) continue;
foreach (var red in nextFrame) {
if (Vector3.Distance(black, red) > distance) {
distance = Vector3.Distance(black, red);
furthestBlack = black;
}
}
}
// Find the closest red point to the furthest black point
distance = float.MaxValue;
Vector3 closestRed = Vector3.zero;
foreach (var red in nextFrame) {
if (takenRed.Contains(red)) continue;
if (Vector3.Distance(furthestBlack, red) < distance) {
distance = Vector3.Distance(furthestBlack, red);
closestRed = red;
}
}
if (!pairs.ContainsKey(furthestBlack)) {
pairs[furthestBlack] = new List<Vector3>();
}
if (!takenDestination.Contains(closestRed)) {
pairs[furthestBlack].Add(closestRed);
takenBlack.Add(furthestBlack);
takenRed.Add(closestRed);
takenDestination.Add(closestRed);
}
// Debug.Log("Pair: " + furthestBlack.ToString() + " to " + closestRed.ToString());
}
} else {
for (int i = 0; i < maxIterations; i++) {
// Find furthest red point from any black point
float distance = 0;
Vector3 furthestRed = Vector3.zero;
foreach (Vector3 red in nextFrame) {
if (takenRed.Contains(red)) continue;
foreach (Vector3 black in currentFrame) {
if (Vector3.Distance(black, red) > distance) {
distance = Vector3.Distance(black, red);
furthestRed = red;
}
}
}
// Find the closest black point to the furthest red point
distance = float.MaxValue;
Vector3 closestBlack = Vector3.zero;
foreach (var black in currentFrame) {
if (takenBlack.Contains(black)) continue;
if (Vector3.Distance(furthestRed, black) < distance) {
distance = Vector3.Distance(furthestRed, black);
closestBlack = black;
}
}
if (!pairs.ContainsKey(closestBlack)) {
pairs[closestBlack] = new List<Vector3>();
}
if (!takenDestination.Contains(furthestRed)) {
pairs[closestBlack].Add(furthestRed);
takenBlack.Add(closestBlack);
takenRed.Add(furthestRed);
takenDestination.Add(furthestRed);
}
// Debug.Log("Pair: " + closestBlack.ToString() + " to " + furthestRed.ToString());
}
}
if (currentFrame.Count < nextFrame.Count) {
// For every nextFrame[i], find the closest black point and pair it.
for (int i = currentFrame.Count; i < nextFrame.Count; i++) {
float distance = float.MaxValue;
Vector3 closestBlack = Vector3.zero;
foreach (var black in currentFrame) {
if (Vector3.Distance(nextFrame[i], black) < distance) {
distance = Vector3.Distance(nextFrame[i], black);
closestBlack = black;
}
}
if (!pairs.ContainsKey(closestBlack)) {
pairs[closestBlack] = new List<Vector3>();
}
if (!takenDestination.Contains(nextFrame[i])) {
pairs[closestBlack].Add(nextFrame[i]);
takenDestination.Add(nextFrame[i]);
}
// Debug.Log("Pair: " + closestBlack.ToString() + " to " + nextFrame[i].ToString());
}
}
if (currentFrame.Count > nextFrame.Count) {
// For every currentFrame[i], find the closest red point and pair it.
for (int i = nextFrame.Count; i < currentFrame.Count; i++) {
float distance = float.MaxValue;
Vector3 closestRed = Vector3.zero;
foreach (var red in nextFrame) {
if (Vector3.Distance(currentFrame[i], red) < distance) {
distance = Vector3.Distance(currentFrame[i], red);
closestRed = red;
}
}
if (!pairs.ContainsKey(currentFrame[i])) {
pairs[currentFrame[i]] = new List<Vector3>();
}
if (!takenDestination.Contains(closestRed)) {
pairs[currentFrame[i]].Add(closestRed);
takenDestination.Add(closestRed);
}
// Debug.Log("Pair: " + currentFrame[i].ToString() + " to " + closestRed.ToString());
}
}
resultingPairs.Add(pairs);
}
}
但是,当立方体位置在 3d 空间中从一组点到另一组点重叠时,它开始起作用。
我不确定为什么会失败,而且我无法想出一个简单的 2D 示例来说明这种方法哪里出了问题。
我们在漫长的 3 天里尝试了 3 种不同的方法,似乎无法找到解决这个看似简单的问题的方法。
最佳答案
您可以将其解释为 the Assignment problem ,其中黑点是“代理”,红点是“任务”(反之亦然),它们之间的距离是成本。
The problem instance has a number of agents and a number of tasks. Any agent can be assigned to perform any task, incurring some cost that may vary depending on the agent-task assignment. It is required to perform all tasks by assigning exactly one agent to each task and exactly one task to each agent in such a way that the total cost of the assignment is minimized.
赋值问题可以使用 The Hungarian algorithm 在多项式时间内解决.问题的变体涉及 more tasks than agents ,您可以将其应用于列表大小不同的特殊情况。
关于c# - 从 3d 空间中的一组点移动到具有最短可能累积距离的另一组点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54700983/
A是不同元素的序列,B是A的子序列,A-B是A中的所有元素,但不是B中的所有元素距离(A) = 总和|a(i)-a(i+1)|从 i=1 到 n-1找到一个子序列 B 使得 Dist(B)+Dist(
我想通过计算每对中所有(多维)点集之间距离的平均值来量化组相似性。 我可以很容易地手动为每对组手动完成此操作,如下所示: library(dplyr) library(tibble) library(
在 OpenXML 中用于指定大小或 X、Y 坐标的度量单位是什么? (介绍)。 将那些与像素匹配是否有意义,如果是这样,那些如何转换为像素? graphicFrame.Transform = new
我想知道是否有人可以帮助我替换过渡层中的值。 如果我尝试: transitionlayer[transitionlayer >= 0.14] = 0.14 : comparison (5) is
我在 firebase 中有一个列表,其中包括地理位置(经度和纬度),并且我想获得距给定坐标最近的 10 个位置。 我正在从 MySQL 过渡,在那里我将计算 SELECT 中的距离, 并在 ORDE
如何在 Python 中根据 2 个 GPS 坐标计算速度、距离和方向(度)?每个点都有纬度、经度和时间。 我在这篇文章中找到了半正矢距离计算: Calculate distance between
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
我只想使用 matplotlib 标记两条曲线之间发生最大偏差的位置。请帮助我。 垂直距离适用于 Kolmogorov–Smirnov test import numpy as np %matplot
我有一个包含数万行重复项的文件。我想根据行号找到重复项之间的平均时间/距离。 例如:(其中第一列是行号) 1 string1 2 string2 3 string2 4 string1 5 strin
用公式speed=distance/time计算时间 但时间总是0我的输入是 distance=10 和 speed=5 我的输出必须 = 2 #include int main() { in
我正在使用 Levenshtein 算法来查找两个字符串之间的相似性。这是我正在制作的程序的一个非常重要的部分,因此它需要有效。问题是该算法没有发现以下示例相似: CONAIR AIRCON 算法给出
对于一个房地产网站,我需要实现一个允许搜索文本和距离的搜索机制。 当 lat 和 lon 记录在单独的列中时,在 MySQL 表上进行距离计算很容易,但房子往往有 LOT true/false 属性。
是否可以在触发前更改 UIPanGestureRecognizer 的距离?目前的实现似乎在触发前有 5-10 像素的距离余量,我想降低它如果可能的话。 原因是我将 UIPanGestureRecog
我试图找到两个网格之间的偏差。例如在 3d 空间中定义的两组点之间的差异,我计划使用一些 3d 可视化工具来可视化距离,例如QT3d 或一些基于开放式 gl 的库。 我有两组网格,基本上是两个 .ST
所以,我有这个函数可以快速返回两个字符串之间的 Levenshtein 距离: Function Levenshtein(ByVal string1 As String, ByVal string2
我正在尝试用字典创建一个光学字符识别系统。 事实上,我还没有实现字典=) 我听说有一些基于 Levenstein 距离的简单指标,这些指标考虑了不同符号之间的不同距离。例如。 'N' 和 'H' 彼此
我在PostGIS数据库(-4326)中使用经纬度/经度SRID。我想以一种有效的方式找到最接近给定点的点。我试图做一个 ORDER BY ST_Distance(point, ST_GeomF
我想从线串的一端开始提取沿线串已知距离处的点的坐标。 例如: library(sf) path % group_by(L1) %>% summarise(do_union =
我已经编写了这些用于聚类基于序列的数据的函数: library(TraMineR) library(cluster) clustering <- function(data){ data <- s
是否可以设置 UILabel 的行之间的距离,因为我有一个 UILabel 包含 3 行,并且换行模式是自动换行? 最佳答案 如果您指的是“前导”,它指的是类型行之间的间隙 - 您无法在 UILabe
我是一名优秀的程序员,十分优秀!