Advertisement
sci4me

SortViz

Feb 13th, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.List;
  6.  
  7. /**
  8.  * @author sci4me
  9.  */
  10. public final class SortViz
  11. {
  12.     public static final int WIDTH = 300;
  13.     public static final int HEIGHT = 400;
  14.  
  15.     public static void main(final String[] args) throws Throwable
  16.     {
  17.         new SortViz().start();
  18.     }
  19.  
  20.     private SortViz()
  21.     {
  22.     }
  23.  
  24.     private GifMaker gif;
  25.     private int[] dataset;
  26.  
  27.     private int highlightedIndex;
  28.  
  29.     private void start() throws Throwable
  30.     {
  31.         this.gif = new GifMaker(new FileOutputStream("sort.gif"), 50, WIDTH, HEIGHT);
  32.         this.dataset = new int[14];
  33.  
  34.         final List<Integer> data = new ArrayList<>();
  35.         for (int i = 0; i < this.dataset.length; i++)
  36.             data.add(i + 1);
  37.         Collections.shuffle(data);
  38.         for (int i = 0; i < this.dataset.length; i++)
  39.             this.dataset[i] = data.get(i);
  40.  
  41.         this.highlightedIndex = -1;
  42.  
  43.         this.drawFrame();
  44.         this.drawFrame();
  45.  
  46.         int pos = 1;
  47.         while (pos < this.dataset.length)
  48.         {
  49.             if (this.dataset[pos] >= this.dataset[pos - 1])
  50.             {
  51.                 pos++;
  52.             }
  53.             else
  54.             {
  55.                 final int temp = this.dataset[pos];
  56.                 this.dataset[pos] = this.dataset[pos - 1];
  57.                 this.dataset[pos - 1] = temp;
  58.  
  59.                 if (pos > 1)
  60.                 {
  61.                     pos--;
  62.                 }
  63.             }
  64.  
  65.             this.highlightedIndex = pos;
  66.  
  67.             this.drawFrame();
  68.             this.drawFrame();
  69.         }
  70.  
  71.         for(int i = 0; i < 100; i++)
  72.             this.drawFrame();
  73.  
  74.         this.gif.write();
  75.     }
  76.  
  77.     private void drawFrame() throws IOException
  78.     {
  79.         for (int x = 0; x < WIDTH; x++)
  80.         {
  81.             for (int y = 0; y < HEIGHT; y++)
  82.             {
  83.                 this.gif.setPixel(x, y, 0xFFFFFFFF);
  84.             }
  85.         }
  86.  
  87.         for (int i = 0; i < this.dataset.length; i++)
  88.         {
  89.             final int height = (int) (((double) this.dataset[i] / this.dataset.length) * HEIGHT - 20);
  90.  
  91.             final int xStart = 10 + i * 20;
  92.             for (int x = xStart; x < xStart + 10; x++)
  93.             {
  94.                 final int yStart = 10;
  95.                 for (int y = yStart; y < yStart + height; y++)
  96.                 {
  97.                     this.gif.setPixel(x, HEIGHT - y, i == this.highlightedIndex ? 0xFFFF0000 : 0xFFAA0000);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         this.gif.nextFrame();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement