Advertisement
MadCortez

Untitled

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