We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
A Circle and a Square
A Circle and a Square
Sort by
recency
|
6 Discussions
|
Please Login in order to post a comment
Here is a solution with integer numbers. using distance between points and cross product.
Alternatively, for the square part, one can rotate the coordinate system so as to have the square aligned with the horizontal and vertical directions. Then it is enough to check each coordinate separately i.e. given a point (x, y) whether x is between x1 and x3 (all values after rotation), and similarly for y. In this case we operate with floating point numbers, and need to be careful with precision errors (use small epsilon when comparing numbers).
Checking if the points lies inside a square can be done without any floating point operations. Two hints (References almost contain the solution to this problem. Do not open them if you still want to figure your own way out just by using the hints) :
Calculating the boundaries of the square is the nub of this question. I had considered a square to be enclosed by two pairs of parallel lines at right angles and sides of equal length which it is. The output from my code for the square somtimes differed a little from HR output. Changing from int to float types helped, but the answer seems to need the hacker to consider the square as 2 triangles.
New learning.. Thank you for the question. Converted the editorial version to Java