Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: None | Size: 2.10 KB | Hits: 38 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package punkbuster.scraper.pattern;
  7.  
  8. import java.awt.Color;
  9. import java.util.ArrayList;
  10. import java.util.Collections;
  11. import java.util.List;
  12.  
  13. /**
  14.  *
  15.  * @author Andreas Böck
  16.  */
  17. public class RectangleMatcher {
  18.  
  19.     public Rectangle match(List<Rectangle> m1, Rectangle m2) throws Exception{
  20.  
  21.         //List<Integer> score = new ArrayList<Integer>();
  22. //        System.out.println(m1.size());
  23.         for (int i=0; i<m1.size();i++){
  24.             Integer tempScore = new Integer(0);
  25.             if (m1.get(i).getHeight() != m2.getHeight() || m1.get(i).getWidth()!= m2.getWidth()){
  26.                 throw new Exception("Scraping error! Compared Rectangles are not of same size.");
  27.             }
  28.             if (m1.get(i).getHeight() <=0 || m1.get(i).getWidth() <=0 ){
  29.                 throw new Exception("Scraping error! Rectangles must be of size greater 0.");
  30.             }
  31.             else{
  32.                 for ( int x=0; x<m2.getWidth();x++){
  33.                     for (int y=0; y<m2.getHeight();y++){
  34.                         tempScore +=(calcPoints(m1.get(i).getPixel(x, y),m2.getPixel(x, y)));
  35.                     }
  36.                 }
  37.             }
  38.             m1.get(i).setScore(tempScore);
  39. //            System.out.println("Score von rect "+i+ " ist "+tempScore);
  40.         }
  41.  
  42.         Collections.sort(m1);
  43. //        System.out.println("Score+ "+m1.get(0).getScore());
  44.         if (m1.size()>0) {
  45.             return m1.get(0);
  46.         }         //Return Colour with best (lowest) score.
  47.         else {
  48.             return null;
  49.         }
  50.     }
  51.     private Integer calcPoints(Color c1, Color c2){
  52.         Integer pts = new Integer(0);
  53.         pts += new Integer(absoluteValue(c1.getRed() - c2.getRed()));
  54.         pts += new Integer(absoluteValue(c1.getBlue() - c2.getBlue()));
  55.         pts += new Integer(absoluteValue(c1.getGreen() - c2.getGreen()));
  56.         return pts;
  57.     }
  58.     private int absoluteValue(int e){
  59.         if (e<0) {return -e;}
  60.         else{
  61.             return e;
  62.         }
  63.     }
  64. }