Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 7th, 2012  |  syntax: None  |  size: 1.69 KB  |  views: 36  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.awt.Point;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class Lightning {
  6.    
  7.     private static final double VAR_FACTOR = 0.40;
  8.     private static final double VAR_DECREASE = 0.55;
  9.     private static final int MIN_LENGTH = 50;
  10.  
  11.     @SuppressWarnings("unused")
  12.     public static void main(String[] args) {
  13.         Point start = new Point(0, 0);
  14.         Point end = new Point(0, 400);
  15.         ArrayList<Point> bolt = buildBolt(start, end);
  16.     }
  17.    
  18.     public static ArrayList<Point> buildBolt(Point start, Point end) {
  19.         ArrayList<Point> bolt = new ArrayList<Point>();
  20.         double dx = start.getX() - end.getX();
  21.         double dy = start.getY() - end.getY();
  22.         double length = Math.sqrt(dx*dx + dy*dy);
  23.         double variance = length * VAR_FACTOR;
  24.         bolt.add(start);
  25.         buildBolt(start, end, bolt, variance);
  26.         return bolt;
  27.     }
  28.    
  29.     private static void buildBolt(Point start, Point end,
  30.                                   List<Point> bolt, double variance) {
  31.         double dx = start.getX() - end.getX();
  32.         double dy = start.getY() - end.getY();
  33.         double length = Math.sqrt(dx*dx + dy*dy);
  34.        
  35.         if (length > MIN_LENGTH) {        
  36.             int midX = (start.x + end.x)/2;
  37.             int midY = (start.y + end.y)/2;
  38.             int varX = (int) ((Math.random() * variance * 2) - variance);
  39.             midX = midX + varX;
  40.             Point mid = new Point(midX, midY);
  41.             buildBolt(start, mid, bolt, variance * VAR_DECREASE);
  42.             buildBolt(mid, end, bolt, variance * VAR_DECREASE);
  43.         } else {
  44.             bolt.add(end);
  45.         }
  46.         return;      
  47.     }
  48.  
  49. }