Water Falls
Water Falls |
Consider the set of line segments P1, P2 and P3 of figure 1,representing the side view of a set of planes. What happens ifsome water falls (in the vertical direction and ignoringhorizontal deviations created by kinetics) from source point Sa?It flows over the plane P3, to P1, finally falling on the groundat the point Ga. It is easy to see that, if the water is fallingfrom source point Sb, then it hits the ground at the point Gb.

Given a list of lines segments and a list of source points, theproposed problem is to determine the corresponding falling pointson the ground. To simplify the problem, it is assumed that neitherhorizontal lines nor crossing lines are given. Also nocoincidences exist in the vertical projection of all points (the xcoordinates of the end points and of the source points are alldifferent).
Input
The input begins with a single positive integer on a line by itself indicatingthe number of the cases following, each of them as described below.This line is followed by a blank line, and there is also a blank line betweentwo consecutive inputs.
The input is a text file, containing several lines as follows.
Thefirst line of the input contains the number NP (integer format) ofline segments. It is followed by NP lines containing, each one,the coordinates of the two end points of a segment, in thesequence x1 y1 x2 y2, separated by single spaces. No order issupposed, for this case, between point 1 and point 2 and numbersare written in the integer format.
The next line is the number NS(integer format) of source points. It is followed by NS linescontaining, each one, a pair of integer values x y, separated by asingle space, which are the coordinates of the correspondingsource point.
Output
For each test case, the output must follow the description below.The outputs of two consecutive cases will be separated by a blank line.
NS lines of text containing, each one, the coordinate
x (integerformat) of the corresponding falling point
G. The output valuesmust keep the input order.
Sample Input
1 4 14 7 3 4 11 13 16 11 1 10 6 7 2 1 4 3 3 10 4 14 14 2 13
Sample Output
10 16 2
Note: The picture below corresponds to the sample input.

import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class Main
{
public static final boolean DEBUG = false;
public StreamTokenizer tokenizer;
public BufferedReader cin;
public PrintWriter cout;
public int np, ns;
public Line[] line;
public Point[] spoint;
public double crossy;
static class Point
{
int x, y;
}
static class Line
{
Point a, b;
}
public void init()
{
try {
if (DEBUG) {
cin = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\OJ\\uva_in.txt")));
} else {
cin = new BufferedReader(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
tokenizer = new StreamTokenizer(cin);
} catch (Exception e) {
e.printStackTrace();
}
}
public String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tokenizer.ttype == StreamTokenizer.TT_WORD)
return tokenizer.sval;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER)
return String.valueOf((int) tokenizer.nval);
else
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public boolean input()
{
np = Integer.parseInt(next());
line = new Line[np];
for (int i = 0; i < np; i++) {
line[i] = new Line();
line[i].a = new Point();
Point a = new Point();
a.x = Integer.parseInt(next());
a.y = Integer.parseInt(next());
Point b = new Point();
b.x = Integer.parseInt(next());
b.y = Integer.parseInt(next());
if (a.x > b.x) {
int tmp = a.x;
a.x = b.x;
b.x = tmp;
tmp = a.y;
a.y = b.y;
b.y = tmp;
}
line[i].a = a;
line[i].b = b;
}
ns = Integer.parseInt(next());
spoint = new Point[ns];
for (int i = 0; i < ns; i++) {
spoint[i] = new Point();
spoint[i].x = Integer.parseInt(next());
spoint[i].y = Integer.parseInt(next());
}
return true;
}
public int cross(int x1, int y1, int x2, int y2)
{
return x1 * y2 - x2 * y1;
}
public boolean intersection(Point p1, Point p2, Point p3, Point p4)
{
double c1 = cross(p3.x - p1.x, p3.y - p1.y, p4.x - p3.x, p4.y - p3.y);
double c2 = cross(p2.x - p1.x, p2.y - p1.y, p4.x - p3.x, p4.y - p3.y);
double c3 = cross(p1.x - p3.x, p1.y - p3.y, p2.x - p1.x, p2.y - p1.y);
double c4 = cross(p4.x - p3.x, p4.y - p3.y, p2.x - p1.x, p2.y - p1.y);
if (c2 < 0) {
c1 = -c1;
c2 = -c2;
}
if (c4 < 0) {
c3 = -c3;
c4 = -c4;
}
if (c2 == 0)
return false;
if (c1 >= 0 && c1 <= c2 && c3 >= 0 && c3 <= c4) {
crossy = p1.y + (p2.y - p1.y) * (c1 * 1.0/ c2);
return true;
} else
return false;
}
public Point cal(Point x)
{
boolean[] vis = new boolean[np];
Arrays.fill(vis, false);
int cur = -1;
Point tmp = new Point();
while (true) {
double max = -1;
boolean flag = true;
for (int i = 0; i < np; i++) {
if (!vis[i]) {
tmp.x = x.x;
tmp.y = 0;
if (intersection(line[i].a, line[i].b, x, tmp)) {
if (crossy > max) {
max = crossy;
cur = i;
flag = false;
}
}
}
}
if (!flag) {
vis[cur] = true;
if ((line[cur].b.y - line[cur].a.y) * 1.0 / (line[cur].b.x - line[cur].a.x) > 0) x = line[cur].a;
else x = line[cur].b;
} else break;
}
return x;
}
public void solve(int cas)
{
for (int i = 0; i < ns; i++) {
Point ans = cal(spoint[i]);
cout.println(ans.x);
}
if (cas != 0) cout.println();
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
int t = Integer.parseInt(solver.next());
while (t-- > 0) {
solver.input();
solver.solve(t);
}
}
}