Problem A
Triangle Fun
Input: Standard Input
Output: Standard Output
In the picture below you can seea triangle ABC. Point D, E and F divides the sides BC, CA and AB into ratio 1:2respectively. That is CD=2BD, AE=2CE and BF=2AF. A, D; B, E and C, F areconnected. AD and BE intersects at P, BE and CF intersects at Q and CF and ADintersects at R.
So now a new triangle PQR isformed. Given triangle ABC your job is to find the area of triangle PQR.
Input
First line of the input filecontains an integer N (0<N<1001) which denotes how many sets of inputsare there. Input for each set contains six floating-point numberAx, Ay, Bx, By, Cx, Cy. (0≤Ax, Ay, Bx, By,Cx,Cy≤10000) in one line line. These six numbersdenote that the coordinate of points A, B and C are (Ax, Ay), (Bx, By) and (Cx, Cy) respectively. A, B and C willnever be collinear.
Output
For each setof input produce one line of output. This one line contains an integer AREA.Here AREA is the area of triangle PQR, rounded to the nearest integer.
Sample Input
2 3994.707 9251.677 4152.916 7157.810 5156.835 2551.972 6903.233 3540.932 5171.382 3708.015 213.959 2519.852 |
Output forSample Input
98099 206144
|
将AD和BE的交点P,BE和CF的交点Q、AD与CF的交点R求出来,用叉乘即可求得面积
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
public class Main
{
private static final boolean DEBUG = false;
private BufferedReader cin;
private PrintWriter cout;
private StreamTokenizer tokenizer;
Point a, b, c;
class Point
{
double x, y;
}
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));
}
tokenizer = new StreamTokenizer(cin);
cout = new PrintWriter(new OutputStreamWriter(System.out));
} catch (Exception e) {
e.printStackTrace();
}
}
private String next()
{
try {
tokenizer.nextToken();
if (tokenizer.ttype == StreamTokenizer.TT_EOF)
return null;
else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) {
return String.valueOf(tokenizer.nval);
} else
return tokenizer.sval;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void input()
{
a = new Point();
b = new Point();
c = new Point();
a.x = Double.parseDouble(next());
a.y = Double.parseDouble(next());
b.x = Double.parseDouble(next());
b.y = Double.parseDouble(next());
c.x = Double.parseDouble(next());
c.y = Double.parseDouble(next());
}
private double cross(Point a, Point b, Point c, Point d)
{
double x1 = b.x - a.x, y1 = b.y - a.y;
double x2 = d.x - c.x, y2 = d.y - c.y;
return x1 * y2 - x2 * y1;
}
private Point intersection(Point a, Point b, Point c, Point d)
{
double c1 = cross(a, c, c, d);
double c2 = cross(a, b, c, d);
Point ans = new Point();
ans.x = a.x + (b.x - a.x) * c1 / c2;
ans.y = a.y + (b.y - a.y) * c1 / c2;
return ans;
}
public void solve()
{
Point d = new Point();
Point e = new Point();
Point f = new Point();
d.x = (c.x + 2 * b.x) / 3;
d.y = (c.y + 2 * b.y) / 3;
e.x = (a.x + 2 * c.x) / 3;
e.y = (a.y + 2 * c.y) / 3;
f.x = (b.x + 2 * a.x) / 3;
f.y = (b.y + 2 * a.y) / 3;
Point p = intersection(a, d, b, e);
Point q = intersection(c, f, b, e);
Point r = intersection(a, d, c, f);
double ans = cross(p, q, p, r) / 2;
cout.println( Math.round(Math.abs(ans)));
cout.flush();
}
public static void main(String[] args)
{
Main solver = new Main();
solver.init();
int t = (int)Double.parseDouble(solver.next());
while (t-- > 0) {
solver.input();
solver.solve();
}
}
}