简体   繁体   English

给定2-D平面中n个点的坐标,找出形成直角三角形的三元组的数量

[英]Given the co-ordinates of n points in 2-D plane, find the number of triplets that form right triangle

I can obviously do this by brute force, by selecting all the triplets one by one and checking if they form a right triangle. 我可以通过蛮力,通过逐个选择所有三胞胎并检查它们是否形成直角三角形来做到这一点。 But what could be a more optimal way? 但是什么可能是更优化的方式? I couldn't think of any. 我什么都想不到。

edit: Since some of you pointed out another question seemingly similar to this one, I went through the answers and I still couldn't figure out how do I solve this one using those answers. 编辑:既然你们中的一些人指出了另一个看似与此问题类似的问题,我会仔细阅读答案,但我仍然无法弄清楚如何使用这些答案来解决这个问题。

This will work in O(n^2Log(n)) . 这将在O(n^2Log(n))

Algo : Algo

Create a 2D array Slope , where Slope[i,j] is slope vector between coordinate[i] and coordinate[j] . 创建2D阵列 Slope ,其中Slope[i,j]coordinate[i]coordinate[j]之间的斜率矢量。

Now, for coordinate[i] to be the joint (see note 2) of the right angled triangle , there should be two slopes in Slope[i,....] which are orthogonal to each other, ie their dot products are 0. 现在,为了使coordinate[i]成为直角三角形关节 (见注2), Slope[i,....]应该有两个相互正交的Slope[i,....] ,即它们的点积为0 。

If Slope[i,j] = (a,b) (in vector form) , then you have to look for (b,-a) or (-b,a) in Slope[i,....] . 如果Slope[i,j] = (a,b) (in vector form) ,那么你必须在Slope[i,....]寻找(b,-a)(-b,a)

To make search optimized, keep the array Slope[i,...] sorted on a or b . 要使搜索优化,请将数组Slope[i,...]排序在ab Sorting will take O(nlogn) time. 排序将花费O(nlogn)时间。

Now, for any Slope[i,j] , do a binary search to look for (b,-a) and (-b,a) in array. 现在,对于任何Slope[i,j] ,进行二进制搜索以在数组中查找(b,-a)(-b,a)

Binary Search for all n slopes will take O(nlogn) time. 所有n斜率的二进制搜索将花费O(nlogn)时间。

So, total Time Complexity : 所以,总时间复杂度

= Calculate slope between all points + Sort the slopes + BinarySearch 
= O(n*(n+nlogn+nlogn)) = O(n^2log(n)) 

Note : 注意

  1. Make sure to normalize the Slope Vector, ie : 确保标准化斜率向量,即:

    coordinate_a = (p,q) coordinate_b = (r,s) slope_vector(a,b) = (pr,qs)/distance(a,b)

  2. Joint of Right Angled Triangle : Common coordinate between two orthogonal edges. 直角三角形的关节:两个正交边之间的公共坐标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 二维最接近坐标的算法 - Algorithm for 2-D closest co-ordinates 如何找到在二维平面上连接一组坐标的最小生成树? - How to find minimum spanning tree connecting a set of co-ordinates in the 2d plane? 给定2-D平面中的n个点,我们必须在它们之间找到每个点的k个最近邻居 - Given n points in a 2-D plane we have to find k nearest neighbours of each point among themselves 如何从可以平移或旋转的四个点找到一个点的坐标? 所有这些点形成一个僵硬的身体 - How to find co-ordinates of a point from four points that can translate or rotate? All these points form a rigid body 如何从坐标列表中计算到给定点的最近坐标 - How to calculate the closest co-ordinates to a given point from a list of co-ordinates 如何在给定时间在大表中查找GPS坐标的接近值 - How to find close GPS co-ordinates in large table at a given time 给定随机纬度/经度,从给定隐含行进方向的坐标列表中找到最接近的坐标(纬度/经度) - Given a random lat/lon, find nearest co-ordinate(lat/lon) from given the list of co-ordinates implying the direction of traveling 在2D平面中的给定点集中找到两个点,且距离小于O(n ^ 2) - Find two points in a given set of points in 2D plane with least distance in less than O(n^2) time 使用x和y坐标的2d单元位置 - Postion of 2d cell using x and y co-ordinates 给定 N 个相等的圆(可能重叠)和平面上的 M 个点。 找到包含最大点数的圆 - Given N equal circles (possibly overlapping) and M points on a plane. Find a circle which contains maximum number of points
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM