aditya_sheth's blog

By aditya_sheth, history, 4 years ago, In English

The problem asks to check if it is possible to rotate a square(of side length R*sqrt(2) in the problem) with fixed center(origin on 2-D plane) such that two points p1(x,y) and p2(x,y) lies on the border of square.

I have coded a solution using floating point operations, but it is failing on test #10.

It would be great help if anyone can help with a method using integer operations.

  • Vote: I like it
  • +2
  • Vote: I do not like it

»
4 years ago, # |
Rev. 7   Vote: I like it +19 Vote: I do not like it

Rotate one point 4 times by 90 degrees and each time just check if two points $$$A, B$$$ could lie on the same segment of the square. To do that, draw a chord through the two points and compute the area of green triangle $$$(S, C_1, C_2)$$$, more details below. Print YES if the area is exactly $$$R^2 / 2$$$. No other triangle could have this area because any such pizza-shape triangle has area $$$\frac{1}{2} R^2 \cdot \sin \alpha$$$ so we have $$$\sin \alpha = 1$$$.

How to compute the area of the green triangle? Use cross product to compute area of triangle $$$ABS$$$ and then divide by length $$$|AB|$$$ to get height $$$d$$$ (in red). Express the result as $$$\frac{u}{\sqrt{t}}$$$ and keep two variables $$$u$$$ and $$$t$$$. Then multiply this by the length $$$|C_1 C_2|$$$ to get the area of green triangle. To compare the computed value $$$\frac{u \sqrt{w}}{\sqrt{t}}$$$ with $$$R^2 / 2$$$, square both sides to get just integers.

All that being said, the constraints aren't huge so maybe you can solve this problem with real values and carefully chosen epsilon values.