- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有 2 个点列表:
var listA = new List<Point>();
listA.Add(new Point(10, 1));
listA.Add(new Point(5, 5));
listA.Add(new Point(15, 35));
var listB = new List<Point>();
listB.Add(new Point(1, 1));
listB.Add(new Point(5, 4));
listB.Add(new Point(35, 15));
现在我想做这样的事情:
getClosesPoints(listA,listB, Out pointA, Out pointB);
我现在可以再次检查列表 1 点
private int NearestPoint(Point srcPt, List<Point> lookIn)
{
if (lookIn.Count == 1)
return 0;
KeyValuePair<double, int> smallestDistance = new KeyValuePair<double, int>();
for (int i = 0; i < lookIn.Count; i++)
{
double distance = Math.Sqrt(Math.Pow(srcPt.X - lookIn[i].X, 2) + Math.Pow(srcPt.Y - lookIn[i].Y, 2));
if (i == 0)
smallestDistance = new KeyValuePair<double, int>(distance, i);
else if (distance < smallestDistance.Key)
smallestDistance = new KeyValuePair<double, int>(distance, i);
}
Console.WriteLine("smallest Distance:" + smallestDistance.Key);
return smallestDistance.Value;
}
但我不确定如何扩展此代码以检查 2 个列表。
最佳答案
我假设您想要在两个列表中找到最近的两个点,其中一个点在一个列表中,另一个点在另一个列表中。
您可以使用内部循环和外部循环来解决这个问题。外层循环遍历第一个列表中的所有点;对于这些点中的每一个,您都使用内部循环将其与第二个列表中的所有点进行比较。
这样你就不需要记住所有的距离;您只需要记住到目前为止找到的两个最近点之间的距离,以及到目前为止最近的点本身。
您还可以创建一个简单的小类来返回最近的两个点,而不是通过 out
返回结果参数。为方便起见,您可以添加 Distance()
对此类起作用,因此您无需存储实际距离。 (但是,如果您经常使用此值,那么您可能希望将距离缓存在本地字段中,而不是每次都计算它。)
最后,参数给FindClosestPoints()
方法可以是IEnumerable<Point>
而不是 List<Point>
这允许您将它与更多集合类型一起使用。
将所有这些放在一起会得到类似这样的东西(一个可编译的控制台应用程序):
using System;
using System.Collections.Generic;
using System.Drawing;
namespace Demo
{
// Class just used to return the result from FindClosestPoints()
public sealed class ClosestPoints
{
public ClosestPoints(Point p1, Point p2)
{
_p1 = p1;
_p2 = p2;
}
public Point P1 { get { return _p1; } }
public Point P2 { get { return _p2; } }
public double Distance()
{
double dx = P1.X - P2.X;
double dy = P1.Y - P2.Y;
return Math.Sqrt(dx*dx + dy*dy);
}
private readonly Point _p1;
private readonly Point _p2;
}
public static class Program
{
public static void Main()
{
var listA = new List<Point>();
listA.Add(new Point(10, 1));
listA.Add(new Point(5, 5));
listA.Add(new Point(15, 35));
var listB = new List<Point>();
listB.Add(new Point(1, 1));
listB.Add(new Point(5, 4));
listB.Add(new Point(35, 15));
var answer = FindClosestPoints(listA, listB);
Console.WriteLine("Closest points are {0} and {1}", answer.P1, answer.P2);
}
public static ClosestPoints FindClosestPoints(IEnumerable<Point> seq1, IEnumerable<Point> seq2)
{
double closest = double.MaxValue;
ClosestPoints result = null;
foreach (var p1 in seq1)
{
foreach (var p2 in seq2)
{
double dx = p1.X - p2.X;
double dy = p1.Y - p2.Y;
double distance = dx*dx + dy*dy;
if (distance >= closest)
continue;
result = new ClosestPoints(p1, p2);
closest = distance;
}
}
return result;
}
}
}
关于c# - 如何从 2 List<Point> 中获取最近的 2 Points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28694699/
我需要获取过去 24 小时的记录,但不能像这样按小时分组: SELECT HOUR(CompDate) AS hour, COUNT(1) AS action FROM mytable WHERE (
我们有一个自动完成列表,当您向某人发送电子邮件时会填充该列表,这一切都很好,直到列表变得非常大,您需要输入越来越多的地址才能找到您想要的地址, 这违背了自动完成的目的 我在想应该添加一些逻辑,以便自动
我在 android 的锁屏上工作我们如何禁用导航软按钮,已经尝试了所有方法,systemoverlay但它不起作用,在按下主页按钮时它会终止服务和 Activity 。 最佳答案 后退按钮可以通过覆
我有一个报告创建时间为 2016-05-30,现在我需要从报告时间开始的最后 7 天。我怎样才能使用时刻? report_create_time = moment('2016-05-30').form
我想找出向量中最接近的三个数字。 就像是 v = c(10,23,25,26,38,50) c = findClosest(v,3) c 23 25 26 我试过 sort(colSums(as.ma
考虑以下表结构: id speed 1 100 2 200 3 300 4 400 5 500 考虑以下查询:"SELECT * FROM records WHERE
我正在开发一个依赖 YouTube 直播和实时聊天(也来自 YouTube)的网络应用。事情进展顺利,突然嵌入的聊天功能无法在移动设备上运行。 我试图在我这边找到一个错误或一些无效的配置,但我找不到。
我正在制作一个 React Native 应用程序,它有一个安全部分,用户必须在其中输入密码才能解锁 protected 内容。问题是,当用户在锁定该部分之前切换到另一个应用程序时,将生成屏幕截图以及
我有一条 SQL 语句 (SQL Server Management Studio),我通过仪表板软件将数据传递到 where 语句中。用户可以选择年份(2013 年或现在的 2014 年)和月份(作
我有一个脚本可以添加一组行,使您能够在 SharePoint 列表表单中捕获其他访问者的信息。我有两个 anchor 标记,一个用于添加,另一个用于删除。当我添加一个新的访问者时它有效,当我删除访问者
我正在学习斯坦福 iOS 类(class),我有一个问题,我认为与最近的更新有关。这是代码部分: func evaluate(ops: [Op]) -> (result: Double?,remain
我注意到我的应用有一个奇怪的行为。每当我按下主页按钮时,我的应用程序就会被杀死。我没有在应用程序堆栈中看到该应用程序。我可以看到之前启动的其他应用程序。最初我怀疑 android:launchMode
我需要获取过去 7 天内的所有付费和临时条目,但我总是收到所有退回的内容。我不确定我做错了什么,我已经阅读了这里的很多帖子,但无法理解它是什么。 MySQL 5.6(如果它与我一直在做的事情有什么不同
我的表有一列以 mysql time() 格式格式化。 当它是一个值分配给名为 $preRemainOt 的 php 变量时我想重新安排到最近的 15 分钟 function roundTime($w
我想获取过去 7 天内每天每个产品的最后时间戳。数据库中有数千条记录。我怎样才能通过查询来做到这一点。大约有 25 种不同的产品,每种产品每天大约有 50 个时间戳。 表:构建数据 'Timesta
我现在的代码正在从 SQL 中获取移动应用程序中的数据,首先添加显示,我需要将其设置为在我的 Android 应用程序中显示最后添加的第一个。我有如下所示的 api 代码,最新的是根据我的要求显示的,
我有一张 table ,说 table 的描述为: | ID | SNO | c1 | c2 | c3 | ___________________________________ |
我有两个大小相等的向量,例如 A=[2.29 2.56 2.77 2.90 2.05] and B=[2.34 2.62 2.67 2.44 2.52]. 我有兴趣在两个相同大小的向量 A 和 B 中
之前,我在这里发布了一个问题,询问有关如何从驱动器读取和写入数据的建议,而不是通过像“aaa.txt”这样的文件标签,而只是扇区..我被建议尝试阅读和写作....但新问题出现了……毛茸茸的参数 int
我想删除在给定时间段内未登录的用户,但我稍后会根据结果选择时间段。 所以我需要报告,其中我将收到过去 1 个月、2 个月...... n 个月内未登录的用户数量。 我不太清楚如何在单个 mysql 查
我是一名优秀的程序员,十分优秀!