【问题标题】:How to find minimal and maximal angle determined by three points selected from n points?如何找到由从n个点中选择的三个点确定的最小和最大角度?
【发布时间】:2017-04-06 22:34:39
【问题描述】:

给定 n 个二维平面上的点,例如 (0,0),(1,1), ... 我们可以从中选择任意三个点来构造角度。比如我们选择A(0, 0), B(1, 1), C(1, 0),那么我们得到角度 ABC = 45 度,ACB = 90 度,CAB = 45 度。

我的问题是如何计算由从 n 个点中选择的三个点确定的最大或最小角度。

显然,我们可以使用蛮力算法 - 计算所有角度并找到最大值和最小值,使用余弦定律计算角度,使用勾股定理计算距离。但是高效算法存在吗?

【问题讨论】:

    标签: algorithm math geometry mathematical-optimization


    【解决方案1】:

    如果我是正确的,蛮力在 O(n^3) 中运行:你基本上取每个三元组,计算 3 个角度,并存储整体最大值。

    你可以稍微提高到 O(n^2 * log(n)) 但它更棘手:

    best_angle = 0
    for each point p1:
        for each point p2:
            compute vector (p1, p2), and the signed angle it makes with the X-axis
            store it in  an array A
        sort array A    # O(n * log(n))
        # Traverse A to find the best choice:
        for each alpha in A:
             look for the element beta in A closest to alpha+Pi 
             # Takes O(log n) because it's sorted. Don't forget to take into account the fact that A represents a circle: both ends touch...
            best_angle = max(best_angle, abs(beta - alpha))
    

    复杂度是O(n * (n + nlog(n) + n * log(n))) = O(n^2 * log(n))

    当然你也可以检索在循环过程中获得最佳角度的pt1、pt2。

    可能还有更好的方法,这感觉总体上做的工作太多了,即使您将以前的 pt1 计算重新用于 pt2、...、ptn ...

    【讨论】:

    • 哇,很好的解决方案!挺棘手的。反正pt指点?
    • “在 A 中寻找最接近 alpha+Pi 的元素 beta”应该在摊销常数时间内是可行的(但这并不重要,因为复杂性是由排序决定的)。
    • 是的 pt 表示点,对不起。 Marc 是对的,您可以改进对最佳 beta 的搜索,方法是像往常一样查找第一个,然后使用前一个查找下一个,但您不会获得太多,因为它仍然在 O(n^ 2 log(n))
    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    相关资源
    最近更新 更多