Advertisement
Vladislav8653

laba_2_1_java

Nov 16th, 2022 (edited)
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class pointInPolygon {
  3.     public static void main (String[] args) {
  4.         Scanner scanner = new Scanner(System.in);
  5.         System.out.println("The polygon is given by the coordinates of its vertices. Determine if the given point belongs to the area of the polygon.");
  6.         System.out.println("Input number of vertices:");
  7.         int n = 0;
  8.         final int min = 3;
  9.         boolean isIncorrect;
  10.         do {
  11.             isIncorrect = false;
  12.             try {
  13.                 n = Integer.parseInt(scanner.nextLine());
  14.             } catch (Exception e) {
  15.                 isIncorrect = true;
  16.                 System.out.println("Please, enter a positive integer number:");
  17.             }
  18.             if (!isIncorrect && (n < min))  {
  19.                 isIncorrect = true;
  20.                 System.out.println("There must be more than 2 vertices.");
  21.             }
  22.         } while (isIncorrect);
  23.         float[] arrX = new float [n];
  24.         float[] arrY = new float [n];
  25.         int i;
  26.         for (i = 0 ; i < n; i++){
  27.             System.out.println("Enter coordinates for point " + (i + 1) + ":");
  28.             do {
  29.                 isIncorrect = false;
  30.                 try {
  31.                     arrX[i] = Float.parseFloat(scanner.nextLine());
  32.                 } catch (Exception e) {
  33.                     isIncorrect = true;
  34.                     System.out.println("Please, enter a number:");
  35.                 }
  36.             } while (isIncorrect);
  37.             do {
  38.                 isIncorrect = false;
  39.                 try {
  40.                     arrY[i] = Float.parseFloat(scanner.nextLine());
  41.                 } catch (Exception e) {
  42.                     isIncorrect = true;
  43.                     System.out.println("Please, enter a number:");
  44.                 }
  45.             } while (isIncorrect);
  46.         }
  47.         System.out.println("Input point coordinates:");
  48.         float x = 0;
  49.         float y = 0;
  50.         do {
  51.             isIncorrect = false;
  52.             try {
  53.                 x = Float.parseFloat(scanner.nextLine());
  54.             } catch (Exception e) {
  55.                 isIncorrect = true;
  56.                 System.out.println("Please, enter a number:");
  57.             }
  58.         }while (isIncorrect);
  59.         do {
  60.             isIncorrect = false;
  61.             try {
  62.                 y = Float.parseFloat(scanner.nextLine());
  63.             } catch (Exception e) {
  64.                 isIncorrect = true;
  65.                 System.out.println("Please, enter a number:");
  66.             }
  67.         } while (isIncorrect);
  68.         scanner.close();
  69.         boolean gotInto = false;
  70.         int j = n - 1 ;
  71.         for (i = 0; i < n; i++) {
  72.             if ((((arrY[i] <= y) && (y <= arrY[j])) || ((arrY[j] <= y) && (y <= arrY[i]))) && (((arrY[j] - arrY[i]) != 0) && (x >= ((arrX[j] - arrX[i]) * (y - arrY[i]) / (arrY[j] - arrY[i]) + arrX[i]))))
  73.                 gotInto = !gotInto;
  74.             j++;
  75.             if (j == n)
  76.                 j = 0;
  77.         }
  78.         if (gotInto){
  79.             System.out.println("Point belongs to area!");
  80.         }
  81.         else {
  82.             System.out.println("Point doesn't belong to area.");
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement