Among all quadrilaterals , Chris likes rectangles the most, especially the ones which have their sides parallel to the axes. This file contains information of 1000 quadrilaterals. How many of them are rectangles that meet this criteria?
Input Format
Each quadrilateral is described in two lines. The first line contains 4 integers x 1 , x 2 , x 3 , x 4 and the second line contains 4 integers y 1 , y 2 , y 3 , y 4 . This indicates a quadrilateral with vertex ( x 1 , y 1 ) , ( x 2 , y 2 ) , ( x 3 , y 3 ) , ( x 4 , y 4 ) (not necessary in clockwise / anticlockwise order).
Details and Assumptions :
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Hm, are you assuming that the rectangle must have sides parallel to the axis? IE ( 1 , 0 ) , ( 0 , 1 ) , ( − 1 , 0 ) , ( 0 , − 1 ) also forms a rectangle.
So it would be better to replace with a more general rectangle equation.
Log in to reply
You are right. I hadn't considered that. Strangely, the test case contained no such rectangle.
Log in to reply
@Christopher Boo You should have a question where the cases involve a tilted rectangle :)
Log in to reply
@Calvin Lin – Actually for this problem I want to show a neat trick (that only works on non-tilted rectangle) to verify if the four points form a rectangle. I have added the solution. For the non-tilted one, the only solution I have in mind now is heavy case check. I'll post one once I figured out an elegant solution.
Noticed that if the four points form a rectangle that is parallel to the axes, there will be 2 pairs of x with the same value, same goes for y . For example, ( 0 , 0 ) , ( 0 , 1 ) , ( 1 , 0 ) , ( 1 , 1 ) , x has 2 0 's and 2 1 's, y has 2 0 's and 2 1 's. Since it is guaranteed that no points will be in the same spot, the inverse is also true. To check if the 4 numbers form 2 pairs, simply XOR the 4 value and they should equal to 0 since a ⊕ a = 0 .
1 2 3 |
|
Problem Loading...
Note Loading...
Set Loading...
The key to avoid the tedious work is to sort the points first by the x-coordinates, and then by the y-coordinates. In python, the
sorted
function does that automatically for 2-tuples.Here is my solution:
That prints 590