Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 KB | None | 0 0
  1. //is a point inside a convex polygon? | https://informatics.mccme.ru/mod/statements/view3.php?id=41131&chapterid=451
  2. //Made by @maximihajlov | PhTS2020a | 30/05/2019
  3.  
  4.  
  5. import java.util.Scanner;
  6.  
  7. public class TaskD {
  8.     public static class Point {
  9.         public Point(double x0, double y0) {
  10.             x = x0;
  11.             y = y0;
  12.         }
  13.  
  14.         double x;
  15.         double y;
  16.     }
  17.  
  18.     public static int N;
  19.     public static Point Poly[];
  20.  
  21.     public static boolean solve(Point spot) {
  22.         int i;
  23.         int j;
  24.         boolean ans = false;
  25.         for (i = 0, j = Poly.length - 1; i < Poly.length; j = i++) {
  26.             if ((Poly[i].y > spot.y) != (Poly[j].y > spot.y) &&
  27.                     (spot.x < (Poly[j].x - Poly[i].x) * (spot.y - Poly[i].y) / (Poly[j].y - Poly[i].y) + Poly[i].x)) {
  28.                 ans = !ans;
  29.             }
  30.         }
  31.         return ans;
  32.     }
  33.  
  34.     public static void main(String[] args) {
  35.         Scanner sc = new Scanner(System.in);
  36.         N = sc.nextInt();
  37.  
  38.         Poly = new Point[N];
  39.  
  40.         for (int i = 0; i < N; i++) Poly[i] = new Point(sc.nextDouble(), sc.nextDouble()); //Polygon input
  41.  
  42.         Point spot = new Point(sc.nextDouble(), sc.nextDouble());//xPoint
  43.  
  44.         if (solve(spot)) System.out.println("YES");
  45.         else System.out.println("NO");
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement