Advertisement
Guest User

Untitled

a guest
Feb 6th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.70 KB | None | 0 0
  1. package com.aiko.kontrol.geotechnical.interpolation;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14. public class Interpolation {
  15.  
  16.     private List<WeightedPoint> weightedPoints = new ArrayList<WeightedPoint>();
  17.     private List<ColorRange> colorRanges = new ArrayList<ColorRange>();
  18.     private Point origin = new Point(0, 0);
  19.  
  20.     int power = 4;
  21.     Random random = new Random();
  22.  
  23.     public static void main(String[] args) {
  24.         // TODO Auto-generated method stub
  25.  
  26.         try {
  27.             File imageFile = new File("newImage.png");
  28.             ImageIO.write(new Interpolation().getImage(1366, 768), "png",
  29.                     imageFile);
  30.         } catch (IOException ex) {
  31.  
  32.         }
  33.     }
  34.  
  35.     public BufferedImage getImage(int width, int height) {
  36.  
  37.         BufferedImage bufferedImage = new BufferedImage(width, height,
  38.                 BufferedImage.TYPE_INT_RGB);
  39.  
  40.         initialState();
  41.         interpolateImage(bufferedImage);
  42.  
  43.         return bufferedImage;
  44.     }
  45.  
  46.     public void addWeightedPoint(WeightedPoint p) {
  47.         weightedPoints.add(p);
  48.     }
  49.  
  50.     public void addColorRange(ColorRange c) {
  51.         colorRanges.add(c);
  52.     }
  53.  
  54.     public Point getOrigin() {
  55.         return origin;
  56.     }
  57.  
  58.     public void setOrigin(Point origin) {
  59.         this.origin = origin;
  60.     }
  61.  
  62.     private void initialState() {
  63.  
  64.         float r = 0.34f;
  65.         int n = 10;
  66.         int max = 101;
  67.  
  68.         for (int i = 0; i < n; i++) {
  69.             r -= 0.3333f / n;
  70.  
  71.             colorRanges.add(new ColorRange(i * (max / n), i * (max / n)
  72.                     + (max / n), Color.getHSBColor(r, 1f, 1f)));
  73.         }
  74.  
  75.         /* Agregar puntos */
  76.         weightedPoints.add(new WeightedPoint(100, 100, 100));
  77.         weightedPoints.add(new WeightedPoint(300, 200, 0));
  78.         weightedPoints.add(new WeightedPoint(800, 550, 10));
  79.         weightedPoints.add(new WeightedPoint(100, 500, 50));
  80.         weightedPoints.add(new WeightedPoint(1200, 400, 80));
  81.         weightedPoints.add(new WeightedPoint(1100, 700, 70));
  82.         weightedPoints.add(new WeightedPoint(800, 550, 10));
  83.  
  84.     }
  85.  
  86.     private void interpolateImage(BufferedImage bufferedImage) {
  87.  
  88.         for (int i = 0; i < bufferedImage.getWidth(); i++) {
  89.             for (int j = 0; j < bufferedImage.getHeight(); j++) {
  90.                 bufferedImage.setRGB(i, j, getValueShepard(i, j));
  91.             }
  92.         }
  93.     }
  94.  
  95.     private int getValueShepard(int i, int j) {
  96.  
  97.         double dTotal = 0.0;
  98.         double result = 0.0;
  99.  
  100.         for (WeightedPoint p : weightedPoints) {
  101.  
  102.             double d = distance(p.getX() - origin.getX(),
  103.                     p.getY() - origin.getY(), i, j);
  104.             if (power != 1) {
  105.                 d = Math.pow(d, power);
  106.             }
  107.             d = Math.sqrt(d);
  108.             if (d > 0.0) {
  109.                 d = 1.0 / d;
  110.             } else { // if d is real small set the inverse to a large number
  111.                      // to avoid INF
  112.                 d = 1.e20;
  113.             }
  114.             result += p.getValue() * d;
  115.             dTotal += d;
  116.         }
  117.  
  118.         if (dTotal > 0) {
  119.             return getColor(result / dTotal);
  120.         } else {
  121.             return getColor(0);
  122.         }
  123.  
  124.     }
  125.  
  126.     private int getColor(double val) {
  127.         for (ColorRange r : colorRanges) {
  128.             if (val >= r.min && val < r.max) {
  129.                 return r.color.getRGB();
  130.             }
  131.         }
  132.         return 0;
  133.     }
  134.  
  135.     /**
  136.      * Calculates the distance between two points.
  137.      *
  138.      * @param xDataPt
  139.      *            the x coordinate.
  140.      * @param yDataPt
  141.      *            the y coordinate.
  142.      * @param xGrdPt
  143.      *            the x grid coordinate.
  144.      * @param yGrdPt
  145.      *            the y grid coordinate.
  146.      *
  147.      * @return The distance between two points.
  148.      */
  149.     private double distance(double xDataPt, double yDataPt, double xGrdPt,
  150.             double yGrdPt) {
  151.         double dx = xDataPt - xGrdPt;
  152.         double dy = yDataPt - yGrdPt;
  153.         return Math.sqrt(dx * dx + dy * dy);
  154.     }
  155.  
  156.     // bufferedImage.setRGB(i, j, new Random(100).nextInt());
  157.  
  158.     public class WeightedPoint {
  159.         int x;
  160.         int y;
  161.         int value;
  162.  
  163.         public WeightedPoint(int x, int y, int value) {
  164.  
  165.             this.x = x;
  166.             this.y = y;
  167.             this.value = value;
  168.         }
  169.  
  170.         public int getX() {
  171.             return x;
  172.         }
  173.  
  174.         public void setX(int x) {
  175.             this.x = x;
  176.         }
  177.  
  178.         public int getY() {
  179.             return y;
  180.         }
  181.  
  182.         public void setY(int y) {
  183.             this.y = y;
  184.         }
  185.  
  186.         public int getValue() {
  187.             return value;
  188.         }
  189.  
  190.         public void setValue(int value) {
  191.             this.value = value;
  192.         }
  193.     }
  194.  
  195.     public class ColorRange {
  196.         int min;
  197.         int max;
  198.         Color color;
  199.  
  200.         public ColorRange(int min, int max, Color color) {
  201.  
  202.             this.min = min;
  203.             this.max = max;
  204.             this.color = color;
  205.         }
  206.  
  207.         public int getMin() {
  208.             return min;
  209.         }
  210.  
  211.         public void setMin(int min) {
  212.             this.min = min;
  213.         }
  214.  
  215.         public int getMax() {
  216.             return max;
  217.         }
  218.  
  219.         public void setMax(int max) {
  220.             this.max = max;
  221.         }
  222.  
  223.         public Color getColor() {
  224.             return color;
  225.         }
  226.  
  227.         public void setColor(Color color) {
  228.             this.color = color;
  229.         }
  230.  
  231.     }
  232.  
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement