Poj2002--squares (n points to find the number of squares)

Source: Internet
Author: User

Squares
Time Limit: 3500MS Memory Limit: 65536K
Total Submissions: 16615 Accepted: 6320

Description

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

So we are know what's a square looks like, but can we find all possible squares that can is formed from a set of stars in a Night sky? To make the problem easier, we'll assume that the night sky was a 2-dimensional plane, and each star was specified by its X and Y coordinates. 

Input

The input consists of a number of test cases. Each test case is starts with the integer n (1 <= n <=) indicating the number of points to follow. Each of the next n lines specify the x and y coordinates (both integers) of each point. Assume that the points is distinct and the magnitudes of the coordinates is 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

41 00 11 10 090 01 02 00 21 22 20 11 12 14-2 53 70 05 20

Sample Output

161

Give n points to find out how many squares can be formed?

Enumerate the two points diagonally, then solve the other two points, take the two points into the N points to find, you can hash or two points.

The point of the known diagonal (x1,y1) (X3,Y3) is calculated by the midpoint ((X1+X3)/2, (Y1+Y3)/2) (x, y) minus the midpoint with one point on the diagonal (x, y), then the other two points are (x1-y,y1+x) (x1+y,y1-x)

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <vector >using namespace std; #define EQS 1e-9struct node{double x, y;} P[1100]; bool CMP (node A,node b) {return (a.x < b.x | | (a.x = = b.x && a.y < B.Y)) ;}    BOOL Judge (double x,double y,int n) {int low = 0, Mid, high = n-1;        while (low <= high) {mid = (low + high)/2;        if (Fabs (p[mid].x-x) < EQs && Fabs (p[mid].y-y) < EQS) return true; else if (p[mid].x-x > EQs | |            (Fabs (P[mid].x-x) < EQs && p[mid].y-y > EQs))        High = mid-1;    else Low = mid + 1; } return false;}    int main () {int n, I, J, num;    Double x, y, xx, yy;        while (scanf ("%d", &n) && n) {num = 0;        for (i = 0; i < n; i++) {scanf ("%lf%lf", &p[i].x, &AMP;P[I].Y);        } sort (p,p+n,cmp); for (i= 0; I < n;                i++) {for (j = i+1; J < N; j + +) {if (i = = j) Continue;                x = (p[i].x+p[j].x)/2;                y = (p[i].y+p[j].y)/2;                xx = P[i].x-x;                yy = P[i].y-y; if (judge (X+yy,y-xx,n) && judge (X-yy,y+xx,n)) {//printf (%.1LF,%.1LF) (%.1LF,                    %.1LF) (%.1LF,%.1LF) (%.1LF,%.1LF) \ n ", p[i].x, P[i].y, p[j].x, p[j].y,x+yy,y-xx,x-yy,y+xx);                num++;    }}} printf ("%d\n", NUM/2); } return 0;}


Poj2002--squares (n points to find the number of squares)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.