Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Arrays;
- public class Points {
- private static ArrayList<ArrayList<Double>> shapes = new ArrayList<>();
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String brIn;
- while ((brIn = br.readLine()) != null && !brIn.equals("*")) {
- String[] tokens = brIn.split(" ");
- ArrayList<Double> curShape = new ArrayList<>();
- Arrays.stream(tokens).skip(1).forEach(x -> curShape.add(Double.parseDouble(x)));
- shapes.add(curShape);
- }
- int curPoint = 1;
- String checkIn;
- while ((checkIn = br.readLine()) != null && !checkIn.equals("9999.9 9999.9")) {
- int curIndex = 1;
- ArrayList<Double> tokens = new ArrayList<>();
- Arrays.stream(checkIn.split(" ")).forEach(x -> tokens.add(Double.parseDouble(x)));
- boolean hasFoundOnce = false;
- for (ArrayList<Double> cur : shapes) {
- boolean isWithin;
- switch (cur.size()) {
- case 4:
- isWithin = withinRectangle(cur.get(0), cur.get(1), cur.get(2), cur.get(3), tokens.get(0), tokens.get(1));
- break;
- case 3:
- isWithin = withinCircle(cur.get(0), cur.get(1), cur.get(2), tokens.get(0), tokens.get(1));
- break;
- default:
- isWithin = withinTriangle(cur.get(0), cur.get(1), cur.get(2), cur.get(3), cur.get(4), cur.get(5), tokens.get(0), tokens.get(1));
- break;
- }
- if (isWithin) {
- System.out.println(String.format("Point %d is contained in figure %d", curPoint, curIndex));
- hasFoundOnce = true;
- }
- curIndex++;
- }
- if (!hasFoundOnce) {
- System.out.println(String.format("Point %d is not contained in any figure", curPoint));
- }
- curPoint++;
- }
- }
- public static boolean withinTriangle(double x1, double y1, double x2, double y2, double x3, double y3, double xP, double yP) {
- double initialArea = Math.abs((x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2);
- // x1 y1
- double firstSmallTriangle = Math.abs((xP * (y2 - y3) + x2 * (y3 - yP) + x3 * (yP - y2)) / 2);
- // x2 y2
- double secondSmallTriangle = Math.abs((x1 * (yP - y3) + xP * (y3 - y1) + x3 * (y1 - yP)) / 2);
- // x3 y3
- double thirdSmallTriangle = Math.abs((x1 * (y2 - yP) + x2 * (yP - y1) + xP * (y1 - y2)) / 2);
- return firstSmallTriangle != 0.0 && secondSmallTriangle != 0.0 && thirdSmallTriangle != 0.0 && (initialArea * 1.000001 > firstSmallTriangle + secondSmallTriangle + thirdSmallTriangle && initialArea * 0.999999 < firstSmallTriangle + secondSmallTriangle + thirdSmallTriangle);
- }
- public static boolean withinCircle(double x1, double y1, double r, double xP, double yP) {
- return Math.pow(Math.pow((xP - x1), 2) + Math.pow((yP - y1), 2), 0.5) < r;
- }
- public static boolean withinRectangle(double x1, double y1, double x2, double y2, double xP, double yP) {
- return xP > x1 && xP < x2 && yP < y1 && yP > y2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement