Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- /**
- * @author sci4me
- */
- public final class SortViz
- {
- public static final int WIDTH = 300;
- public static final int HEIGHT = 400;
- public static void main(final String[] args) throws Throwable
- {
- new SortViz().start();
- }
- private SortViz()
- {
- }
- private GifMaker gif;
- private int[] dataset;
- private int highlightedIndex;
- private void start() throws Throwable
- {
- this.gif = new GifMaker(new FileOutputStream("sort.gif"), 50, WIDTH, HEIGHT);
- this.dataset = new int[14];
- final List<Integer> data = new ArrayList<>();
- for (int i = 0; i < this.dataset.length; i++)
- data.add(i + 1);
- Collections.shuffle(data);
- for (int i = 0; i < this.dataset.length; i++)
- this.dataset[i] = data.get(i);
- this.highlightedIndex = -1;
- this.drawFrame();
- this.drawFrame();
- int pos = 1;
- while (pos < this.dataset.length)
- {
- if (this.dataset[pos] >= this.dataset[pos - 1])
- {
- pos++;
- }
- else
- {
- final int temp = this.dataset[pos];
- this.dataset[pos] = this.dataset[pos - 1];
- this.dataset[pos - 1] = temp;
- if (pos > 1)
- {
- pos--;
- }
- }
- this.highlightedIndex = pos;
- this.drawFrame();
- this.drawFrame();
- }
- for(int i = 0; i < 100; i++)
- this.drawFrame();
- this.gif.write();
- }
- private void drawFrame() throws IOException
- {
- for (int x = 0; x < WIDTH; x++)
- {
- for (int y = 0; y < HEIGHT; y++)
- {
- this.gif.setPixel(x, y, 0xFFFFFFFF);
- }
- }
- for (int i = 0; i < this.dataset.length; i++)
- {
- final int height = (int) (((double) this.dataset[i] / this.dataset.length) * HEIGHT - 20);
- final int xStart = 10 + i * 20;
- for (int x = xStart; x < xStart + 10; x++)
- {
- final int yStart = 10;
- for (int y = yStart; y < yStart + height; y++)
- {
- this.gif.setPixel(x, HEIGHT - y, i == this.highlightedIndex ? 0xFFFF0000 : 0xFFAA0000);
- }
- }
- }
- this.gif.nextFrame();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement