Advertisement
Guest User

Untitled

a guest
Mar 20th, 2014
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 6.20 KB | None | 0 0
  1. import java.awt.Frame
  2. import peasy.PeasyCam
  3. import processing.core._
  4. import math._
  5. import scala.Predef._
  6.  
  7. object MainApp {
  8.  
  9.   var wwidth = 0
  10.   var wheight = 0
  11.  
  12.   def main(args : Array[String]) = {
  13.     val applet = new Applet
  14.     val frame = new javax.swing.JFrame("Applet")
  15.  
  16.     frame.setExtendedState(Frame.MAXIMIZED_BOTH)
  17.     frame.setUndecorated(true)
  18.     frame.setVisible(true)
  19.     wwidth = frame.getWidth
  20.     wheight = frame.getHeight
  21.  
  22.     frame.getContentPane.add(applet)
  23.     applet.init()
  24.   }
  25. }
  26.  
  27. class Point(xc: Int, yc: Int) {
  28.   var x: Int = xc
  29.   var y: Int = yc
  30. }
  31.  
  32. object Utils {
  33.   val r = scala.util.Random
  34.  
  35.   def randomPoint(A:Array[Point]) = {
  36.     val random_index = r.nextInt(A.length)
  37.     A(random_index)
  38.   }
  39.  
  40.   def dist(x1:Float, y1:Float, x2:Float, y2:Float) : Int = {
  41.     //println("Distance: " + x1 + " " + y1 + " " + x2 + " " + y2)
  42.     sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)).toInt
  43.   }
  44. }
  45.  
  46. class Applet extends PApplet {
  47.  
  48.   val maxParticles = 30000
  49.   var maxX = MainApp.wwidth/2 + 600
  50.   var maxY = MainApp.wheight/2 + 600
  51.   var centerX = MainApp.wwidth/2
  52.   var centerY = MainApp.wheight/2
  53.   val maxStickDistance = 40
  54.   val zoneSize = 50
  55.  
  56.   var currentParticles = 0
  57.   var pointsX = new Array[Int](maxParticles)
  58.   var pointsY = new Array[Int](maxParticles)
  59.   var movementsSum = 0
  60.  
  61.   var nearestX = centerX
  62.   var nearestY = centerY
  63.   var farthestX = centerX
  64.   var farthestY = centerY
  65.  
  66.   var prevPoint:Point = null
  67.  
  68.   val maxJumpDistance = 50
  69.  
  70.   var cam:PeasyCam = null
  71.   var currCamDistance = 200
  72.  
  73.   override def setup() {
  74.     size(MainApp.wwidth, MainApp.wheight, PConstants.P3D)
  75.     background(20)
  76.     colorMode(PConstants.HSB)
  77.     strokeWeight(2.toFloat)
  78.     smooth()
  79.  
  80.     cam = new PeasyCam(this, MainApp.wwidth/2, MainApp.wheight/2, 0, currCamDistance)
  81.     cam.setMinimumDistance(0)
  82.     perspective()
  83.     strokeCap(PConstants.ROUND)
  84.     strokeJoin(PConstants.ROUND)
  85.  
  86.     val f:PFont = createFont("Arial",16,true)
  87.     textFont(f)
  88.  
  89.     // init again
  90.     maxX = MainApp.wwidth/2 + 600
  91.     maxY = MainApp.wheight/2 + 600
  92.     centerX = MainApp.wwidth/2
  93.     centerY = MainApp.wheight/2
  94.     nearestX = centerX
  95.     nearestY = centerY
  96.     farthestX = centerX
  97.     farthestY = centerY
  98.   }
  99.  
  100.   override def draw() {
  101.     background(20)
  102.     cam.beginHUD()
  103.     text("Particles: " + currentParticles + "/" + maxParticles, 10, 20)
  104.     if(currentParticles != 0) text("Movements per placement: " + movementsSum/currentParticles.toInt, 10, 40)
  105.     cam.endHUD()
  106.  
  107.     // redraw points
  108.     for(i <- 0 to currentParticles) {
  109.       val colFactor = i.toFloat / maxParticles
  110.       stroke(colFactor * 255, 255, 255, 255)
  111.       point(pointsX(i), pointsY(i))
  112.     }
  113.  
  114.     //point(centerX, centerY)
  115.     if(currentParticles == 0) {
  116.       pointsX(0) = centerX
  117.       pointsY(0) = centerY
  118.       currentParticles += 1
  119.     }
  120.     if(currentParticles == maxParticles) noLoop()
  121.  
  122.     var curJumpDistance = maxJumpDistance
  123.     var closest = -1
  124.  
  125.     val leftThreshold = if(nearestX - zoneSize > 0) nearestX - zoneSize else 0
  126.     val rightThreshold = if(farthestX + zoneSize > 0) farthestX + zoneSize else 0
  127.     val topThreshold = if(nearestY - zoneSize > 0) nearestY - zoneSize else 0
  128.     val bottomThreshold = if(farthestY + zoneSize > 0) farthestY + zoneSize else 0
  129.  
  130.     //println("Thresholds: " + leftThreshold + " " + rightThreshold + " " + topThreshold + " " + bottomThreshold)
  131.  
  132.     var possiblePlaces = Array(
  133.       new Point(random(leftThreshold, nearestX).toInt, random(0, maxY).toInt),
  134.       new Point(random(0, maxX).toInt, random(topThreshold, nearestY).toInt),
  135.       new Point(random(farthestX, rightThreshold).toInt, random(0, maxY).toInt),
  136.       new Point(random(0, maxX).toInt, random(farthestY, bottomThreshold).toInt)
  137.     )
  138.  
  139.     var newP = Utils.randomPoint(possiblePlaces)
  140.     var placed = false
  141.     var currentMovements = 0
  142.     while(!placed) {
  143.       // drawing point
  144.       /*fill(20)
  145.       noStroke()
  146.       if(prevPoint != null) rect(prevPoint.x-10, prevPoint.y, 20, 20)
  147.       stroke(0, 0, 100, 255)
  148.       point(newP.x, newP.y) */
  149.  
  150.       // finding closest
  151.       for(i <- 0 until currentParticles) {
  152.         val distance = Utils.dist(newP.x, newP.y, pointsX(i), pointsY(i))
  153.         //println("Closest: " + distance)
  154.         if(distance < curJumpDistance) {
  155.           curJumpDistance = distance
  156.           closest = i
  157.         }
  158.       }
  159.  
  160.       if(curJumpDistance <= maxStickDistance) {
  161.         // finding free place around
  162.         val stickingTo = new Point(pointsX(closest), pointsY(closest))
  163.         val possiblePlacements = Array(
  164.           new Point(stickingTo.x-1, stickingTo.y),
  165.           new Point(stickingTo.x+1, stickingTo.y),
  166.           new Point(stickingTo.x, stickingTo.y-1),
  167.           new Point(stickingTo.x, stickingTo.y+1)
  168.         )
  169.  
  170.         val placingTo = Utils.randomPoint(possiblePlacements)
  171.         pointsX(currentParticles) = placingTo.x
  172.         pointsY(currentParticles) = placingTo.y
  173.         currentParticles += 1
  174.  
  175.         val colFactor = currentParticles.toFloat / maxParticles
  176.         //println("Colfactor: " + colFactor)
  177.         stroke(colFactor * 255, 120, 255, 255)
  178.         //scale(2)
  179.         //point(placingTo.x, placingTo.y)
  180.         //line(pointsX(closest), pointsY(closest), placingTo.x, placingTo.y)
  181.  
  182.         //println("Placing: " + placingTo.x + " " + placingTo.y)
  183.  
  184.         if(placingTo.x > farthestX) farthestX = placingTo.x
  185.         if(placingTo.x < nearestX) nearestX = placingTo.x
  186.         if(placingTo.y > farthestY) farthestY = placingTo.y
  187.         if(placingTo.y < nearestY) nearestY = placingTo.y
  188.  
  189.         placed = true
  190.         movementsSum += currentMovements
  191.       }
  192.  
  193.       // moving
  194.       possiblePlaces = Array(
  195.         new Point(newP.x-random(maxJumpDistance).toInt, newP.y),
  196.         new Point(newP.x+random(maxJumpDistance).toInt, newP.y),
  197.         new Point(newP.x, newP.y-random(maxJumpDistance).toInt),
  198.         new Point(newP.x, newP.y+random(maxJumpDistance).toInt)
  199.       )
  200.  
  201.       prevPoint = newP
  202.       val movingTo = Utils.randomPoint(possiblePlaces)
  203.       newP = movingTo
  204.       if(newP.x > maxX || newP.x < 0
  205.         || newP.y > maxY || newP.y < 0) placed = true
  206.  
  207.       currentMovements += 1
  208.     }
  209.   }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement