Problem Statement
There is a laser cannon at coordinates (0, 0) on the cartesian plane. There are also several targets on the plane. Each target is a vertical line segment, and the endpoints of the i-th target are at coordinates (x[i], y1[i]) and (x[i], y2[i]). A random angle between -Pi/2 and Pi/2, inclusive, is chosen, and a single shot is fired. The angle -Pi/2 is straight down vertically, 0 is straight to the right horizontally, and Pi/2 is straight up vertically. A shot is a straight ray of infinite length starting from the point (0, 0). A shot hits a target if there is a common point between them. Return the expected number of targets that will be hit by the single shot. Hitting a target doesn't change the direction of the laser shot.
Definition
- Class:
- LaserShooting
- Method:
- numberOfHits
- Parameters:
- int[], int[], int[]
- Returns:
- double
- Method signature:
- double numberOfHits(int[] x, int[] y1, int[] y2)
- (be sure your method is public)
Notes
- A return value with either an absolute or relative error of less than 1.0e-9 is considered correct.
Constraints
- x will contain between 1 and 50 elements, inclusive.
- All elements of x will be distinct.
- x, y1 and y2 will contain the same number of elements.
- Each element of x will be between 1 and 1,000, inclusive.
- Each element of y1 and y2 will be between -1,000 and 1,000, inclusive.
- All targets will have positive lengths.
Examples
{1}{-1}{1}Returns: 0.5
The only one target will be hit with probability 1/2.{1,2}{-1,-2}{1,2}Returns: 1.0
Both targets will be hit with probability 1/2.{3,4,7,1}{1,2,3,4}{4,3,2,1}Returns: 0.4623163952488826
{134,298,151,942}{-753,-76,19,568}{440,689,-39,672}Returns: 1.444210260641501