borkins

Exam Jan-2016 - 12. Generate Rectangles + Visualisation

May 11th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner console = new Scanner(System.in);
  8.        
  9.         int n = Integer.parseInt(console.nextLine());
  10.         int m = Integer.parseInt(console.nextLine());
  11.        
  12.         int count = 0;
  13.        
  14.         for (int x1 = -n; x1 < n; x1++)
  15.         {
  16.             for (int y1 = -n; y1 < n; y1++)
  17.             {
  18.                 for (int x2 = x1 + 1; x2 <= n; x2++)
  19.                 {
  20.                     for (int y2 = y1 + 1; y2 <= n; y2++)
  21.                     {
  22.                         int a = Math.abs(x2 - x1);
  23.                         int b = Math.abs(y2 - y1);
  24.                        
  25.                         if (a * b >= m) {
  26.                             System.out.printf("(%d, %d) (%d, %d) -> %d%n", x1, y1, x2, y2, (a * b));
  27.                             count++;
  28.                             // Drawing the rectangles on the coordinate system
  29.                             drawRectangle(x1, y1, x2, y2, n);
  30.                         }
  31.                     }
  32.                 }
  33.             }
  34.         }
  35.         if (count == 0) System.out.println("No");
  36.     }
  37.    
  38.     public static void drawRectangle(int x1, int y1, int x2, int y2, int gridSize)
  39.     {
  40.         int size = gridSize + 3;
  41.        
  42.         System.out.print("\n ");
  43.        
  44.         for (int col = -size; col <= size; col++)
  45.         {
  46.             System.out.printf(" %2d", col);
  47.         }
  48.         System.out.println();
  49.        
  50.         for (int row = -size; row <= size; row++)
  51.         {
  52.             System.out.printf("%2d", row);
  53.             for (int col = -size; col <= size; col++)
  54.             {
  55.                 System.out.print(((col == x1 || col == x2) &&
  56.                                   (row == y1 || row == y2)) ? " * " : " | ");
  57.             }
  58.             System.out.println();
  59.         }
  60.         System.out.println();
  61.     }
  62. }
Add Comment
Please, Sign In to add comment