Loading [MathJax]/jax/output/CommonHTML/jax.js

poj 2002 Squares

题目连接

http://poj.org/problem?id=2002  

Squares

Description

A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degree angles. It is also a polygon such that rotating about its centre by 90 degrees gives the same polygon. It is not the only polygon with the latter property, however, as a regular octagon also has this property. 

So we all know what a square looks like, but can we find all possible squares that can be formed from a set of stars in a night sky? To make the problem easier, we will assume that the night sky is a 2-dimensional plane, and each star is specified by its x and y coordinates. 

Input

The input consists of a number of test cases. Each test case starts with the integer n (1 <= n <= 1000) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (two integers) of each point. You may assume that the points are distinct and the magnitudes of the coordinates are less than 20000. The input is terminated when n = 0.

Output

For each test case, print on a line the number of squares one can form from the given stars.

Sample Input

4
1 0
0 1
1 1
0 0
9
0 0
1 0
2 0
0 2
1 2
2 2
0 1
1 1
2 1
4
-2 5
3 7
0 0
5 2
0

Sample Output

1
6
1

题意:给出一些点的集合求能组成正方形的个数。
思路:直接枚举的话n=1000   O(n4) 会超时。考虑换个思路:已知两个点,由正方形的几何特性
求出另外两个点的坐标,判断是否在点的集合里(哈希,二分,set。。。)都可我用的哈希。
具体先对所有的点按横坐标,纵坐标从小到大排序。统计个数tot,最后的答案即为tot/2(正方形的对称性)
那么问题来了,如何已知两个点,求另外两个点呢?
现在给出公式:记A(x1,y1)   B(x2,y2)  AB=(x2x1,y2y1)
另外两个点C(x3,y3)   D(x4,y4)   
C   x3=y1y2+x1  y3=x2x1+y1
D   x4=y1y2+x2  y4=x2x1+y2
证明其实很简单记X=(a,b)逆时针旋转β度得到Y(x,y)
有:x=acosβbsinβ  y=asinβ+bcosβ (由三角函数的几何意义易得)
那么AC=(x3x1,y3y1)
x3x1=(x2x1)cosβ(y2y1)sinβ
y3y1=(x2x1)sinβ+(y2y1)cosβ 其中β=900
所以x3=y1y2+x1  y3=x2x1+y1
BD同理(注意向量的方向和旋转方向)
原谅我孱弱的语文水平写的太挫了凑合看吧/(ㄒoㄒ)/~~

posted @   GadyPu  阅读(108)  评论(0编辑  收藏  举报
编辑推荐:
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
阅读排行:
· 不到万不得已,千万不要去外包
· C# WebAPI 插件热插拔(持续更新中)
· 会议真的有必要吗?我们产品开发9年了,但从来没开过会
· 【译】我们最喜欢的2024年的 Visual Studio 新功能
· 如何打造一个高并发系统?
点击右上角即可分享
微信分享提示