Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2015
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.01 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics2D;
  3. import java.awt.Point;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.Collections;
  9. import java.util.Comparator;
  10. import java.util.List;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14. public class UnobstructedRectangles {
  15.    
  16.     public class Rectangle {
  17.        
  18.         public Color colour;
  19.         public int zIndex;
  20.         public Point topLeft;
  21.         public Point topRight;
  22.         public Point bottomLeft;
  23.         public Point bottomRight;
  24.        
  25.         public Rectangle() {
  26.             colour = new Color(255, 255, 255);
  27.         }
  28.     }
  29.    
  30.     private BufferedImage image;
  31.     private List<Rectangle> rectangles;
  32.    
  33.     public static void main(String[] args) throws IOException {
  34.         new UnobstructedRectangles();
  35.     }
  36.    
  37.     public UnobstructedRectangles() throws IOException {
  38.         rectangles = new ArrayList<Rectangle>();
  39.         mapRectangles();
  40.         traceRectangles();
  41.         drawSolution();
  42.     }
  43.    
  44.     public void mapRectangles() throws IOException {
  45.         image = ImageIO.read(new File("input.png"));
  46.        
  47.         for (int y = 0; y < image.getHeight(); y++) {
  48.             for (int x = 0; x < image.getWidth(); x++) {
  49.                 Color c = new Color(image.getRGB(x, y));
  50.                
  51.                 if (c.getRGB() == -1) {
  52.                     continue;
  53.                 }
  54.                
  55.                 Rectangle rect = null;
  56.                 for (Rectangle re : rectangles) {
  57.                     if (re.colour.equals(c)) {
  58.                         rect = re;
  59.                         break;
  60.                     }
  61.                 }
  62.                
  63.                 if (rect == null) {
  64.                     rect = new Rectangle();
  65.                     rectangles.add(rect);
  66.                 }
  67.                
  68.                 if (rect.topLeft == null) {
  69.                     rect.topLeft = new Point(x, y);
  70.                     rect.topRight = new Point(x, y);
  71.                     rect.bottomLeft = new Point(x, y);
  72.                     rect.bottomRight = new Point(x, y);
  73.                     rect.colour = new Color(c.getRGB());
  74.                 }
  75.                
  76.                 if (x < rect.topLeft.x) {
  77.                     rect.topLeft.x = x;
  78.                     rect.bottomLeft.x = x;
  79.                 }
  80.                
  81.                 if (x > rect.topRight.x) {
  82.                     rect.topRight.x = x;
  83.                     rect.bottomRight.x = x;
  84.                 }
  85.                
  86.                 rect.bottomLeft.y = y;
  87.                 rect.bottomRight.y = y;
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void traceRectangles() {
  93.        
  94.         List<Color> invadingColours = null;
  95.         for (Rectangle r : rectangles) {
  96.             invadingColours = new ArrayList<Color>();
  97.             for (int y = r.topLeft.y; y <= r.bottomLeft.y; y++) {
  98.                 for (int x = r.topLeft.x; x <= r.topRight.x; x++) {
  99.                     if (image.getRGB(x, y) != r.colour.getRGB()) {
  100.                        
  101.                         Color overlappingColour = new Color(image.getRGB(x, y));
  102.                         boolean found = false;
  103.                         for (Color invCol : invadingColours) {
  104.                             if (invCol.equals(overlappingColour)) {
  105.                                 found = true;
  106.                                 break;
  107.                             }
  108.                         }                      
  109.                         if (!found) {
  110.                             invadingColours.add(overlappingColour);
  111.                         } else {
  112.                             continue;
  113.                         }
  114.                        
  115.                         Rectangle overlappingRectangle = null;                     
  116.                         for (Rectangle re : rectangles) {
  117.                             if (re.colour.equals(overlappingColour)) {
  118.                                 overlappingRectangle = re;
  119.                                 break;
  120.                             }
  121.                         }
  122.                        
  123.                         if (overlappingRectangle != null) {
  124.                             overlappingRectangle.zIndex = r.zIndex + 1;
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.            
  130.         }
  131.     }
  132.    
  133.     public void drawSolution() throws IOException {
  134.        
  135.         Collections.sort(rectangles, new RectangleZIndexComparator());
  136.         for (int ii = rectangles.size(); ii > 0; ii--) {
  137.             drawRectangles(rectangles.subList(0, ii), rectangles.size() - ii + 1);
  138.         }
  139.     }
  140.    
  141.     public void drawRectangles(List<Rectangle> rects, int ii) throws IOException {
  142.        
  143.         BufferedImage outputImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
  144.         Graphics2D g = (Graphics2D) outputImage.getGraphics();
  145.        
  146.         for (Rectangle r : rects) {
  147.            
  148.             g.setColor(r.colour);
  149.            
  150.             int x = r.topLeft.x;
  151.             int y = r.topLeft.y;
  152.             int width = r.topRight.x - r.topLeft.x;
  153.             int height = r.bottomLeft.y - r.topLeft.y;
  154.            
  155.             g.fillRect(x, y, width, height);
  156.         }
  157.        
  158.         ImageIO.write(outputImage, "png", new File("output_" + ii + ".png"));
  159.     }
  160.    
  161.     private void log() {
  162.         StringBuilder sb = new StringBuilder();
  163.        
  164.         for (Rectangle r : rectangles) {
  165.             sb.append("[");
  166.             sb.append(String.format("%03d", r.colour.getRed()));
  167.             sb.append(",");
  168.             sb.append(String.format("%03d", r.colour.getGreen()));
  169.             sb.append(",");
  170.             sb.append(String.format("%03d", r.colour.getBlue()));
  171.             sb.append(String.format("]\tZ-index: %d", r.zIndex));
  172.             sb.append("\tTop left: ");
  173.             sb.append(r.topLeft.x);
  174.             sb.append(",");
  175.             sb.append(r.topLeft.y);
  176.             sb.append(";\tTop right: ");
  177.             sb.append(r.topRight.x);
  178.             sb.append(",");
  179.             sb.append(r.topRight.y);
  180.             sb.append(";\tBottom left: ");
  181.             sb.append(r.bottomLeft.x);
  182.             sb.append(",");
  183.             sb.append(r.bottomLeft.y);
  184.             sb.append(";\tBottom right: ");
  185.             sb.append(r.bottomRight.x);
  186.             sb.append(",");
  187.             sb.append(r.bottomRight.y);
  188.             System.out.println(sb.toString());
  189.             sb.setLength(0);
  190.         }
  191.     }
  192.    
  193.     public class RectangleZIndexComparator implements Comparator<Rectangle> {
  194.  
  195.         @Override
  196.         public int compare(Rectangle r1, Rectangle r2) {
  197.             return r1.zIndex > r2.zIndex ? 1 : (r1.zIndex == r2.zIndex ? 0 : -1);
  198.         }
  199.     }
  200.    
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement