Advertisement
MadCortez

Untitled

Oct 11th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.Scanner;
  2. class MyClass {
  3.     public static Scanner in = new Scanner(System.in);
  4.  
  5.     public static void start(){
  6.         int n = userInput();
  7.         int a[][] = userInputArray(n);
  8.         boolean isPolygon = checkPolygon(a, n);
  9.         print(isPolygon);
  10.     }
  11.  
  12.     public static void print(boolean flag) {
  13.         if (flag)
  14.             System.out.println("Введённый многоугольник выпуклый");
  15.         else
  16.             System.out.println("Введённый многоугольник не выпуклый");
  17.     }
  18.  
  19.     public static int inputValue(int min, int max) {
  20.         int currentValue;
  21.         boolean isNotValid = true;
  22.         do {
  23.             currentValue = in.nextInt();
  24.             if (currentValue >= min && currentValue <= max)
  25.                 isNotValid = false;
  26.             else
  27.                 System.out.println("Введите число в заданном диапазоне");
  28.         } while (isNotValid);
  29.         return currentValue;
  30.     }
  31.  
  32.     public static int[][] userInputArray (int n) {
  33.     final int MIN_VALUE = -500;
  34.     final int MAX_VALUE = 500;
  35.         int a[][] = new int [2][n];
  36.         a[0] = new int[n];
  37.         a[1] = new int[n];
  38.         System.out.print("Введите координаты вершин в порядке обхода в диапазоне " + MIN_VALUE + ".." + MAX_VALUE + ": \n");
  39.         for (int i = 0; i < n; i++) {
  40.             System.out.print("Введите координаты " + (i + 1) + "-й вершины: ");
  41.             a[0][i] = inputValue(MIN_VALUE, MAX_VALUE);
  42.             a[1][i] = inputValue(MIN_VALUE, MAX_VALUE);
  43.         }
  44.         return a;
  45.     }
  46.  
  47.     public static boolean checkPolygon(int[][] a, int n)  {
  48.         int i = 0;
  49.         boolean flag = true;
  50.         n--;
  51.         do {
  52.             int j = (i + 1) % n;
  53.             int k = (i + 2) % n;
  54.             int ans = (a[0][j] - a[0][i]) * (a[1][k] - a[1][j]) - (a[1][j] - a[1][i]) * (a[0][k] - a[0][j]);
  55.             if (ans < 0)
  56.                 flag = false;
  57.             i++;
  58.         } while (flag && i <=n);
  59.         return flag;
  60.     }
  61.  
  62.     public static int userInput() {
  63.         int n;
  64.         final int MIN_SIZE = 3;
  65.         final int MAX_SIZE = 20;
  66.         System.out.print("Данная программа определяет, является ли данный многоугольник выпуклым\n");
  67.         System.out.print("Введите количество вершин в диапазоне " + MIN_SIZE + ".." + MAX_SIZE + ": ");
  68.         n = inputValue(MIN_SIZE, MAX_SIZE);
  69.         return n;
  70.     }
  71.  
  72.     public static void main(String[] args){
  73.         start();
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement