Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Frame
- import peasy.PeasyCam
- import processing.core._
- import math._
- import scala.Predef._
- object MainApp {
- var wwidth = 0
- var wheight = 0
- def main(args : Array[String]) = {
- val applet = new Applet
- val frame = new javax.swing.JFrame("Applet")
- frame.setExtendedState(Frame.MAXIMIZED_BOTH)
- frame.setUndecorated(true)
- frame.setVisible(true)
- wwidth = frame.getWidth
- wheight = frame.getHeight
- frame.getContentPane.add(applet)
- applet.init()
- }
- }
- class Point(xc: Int, yc: Int) {
- var x: Int = xc
- var y: Int = yc
- }
- object Utils {
- val r = scala.util.Random
- def randomPoint(A:Array[Point]) = {
- val random_index = r.nextInt(A.length)
- A(random_index)
- }
- def dist(x1:Float, y1:Float, x2:Float, y2:Float) : Int = {
- //println("Distance: " + x1 + " " + y1 + " " + x2 + " " + y2)
- sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)).toInt
- }
- }
- class Applet extends PApplet {
- val maxParticles = 30000
- var maxX = MainApp.wwidth/2 + 600
- var maxY = MainApp.wheight/2 + 600
- var centerX = MainApp.wwidth/2
- var centerY = MainApp.wheight/2
- val maxStickDistance = 40
- val zoneSize = 50
- var currentParticles = 0
- var pointsX = new Array[Int](maxParticles)
- var pointsY = new Array[Int](maxParticles)
- var movementsSum = 0
- var nearestX = centerX
- var nearestY = centerY
- var farthestX = centerX
- var farthestY = centerY
- var prevPoint:Point = null
- val maxJumpDistance = 50
- var cam:PeasyCam = null
- var currCamDistance = 200
- override def setup() {
- size(MainApp.wwidth, MainApp.wheight, PConstants.P3D)
- background(20)
- colorMode(PConstants.HSB)
- strokeWeight(2.toFloat)
- smooth()
- cam = new PeasyCam(this, MainApp.wwidth/2, MainApp.wheight/2, 0, currCamDistance)
- cam.setMinimumDistance(0)
- perspective()
- strokeCap(PConstants.ROUND)
- strokeJoin(PConstants.ROUND)
- val f:PFont = createFont("Arial",16,true)
- textFont(f)
- // init again
- maxX = MainApp.wwidth/2 + 600
- maxY = MainApp.wheight/2 + 600
- centerX = MainApp.wwidth/2
- centerY = MainApp.wheight/2
- nearestX = centerX
- nearestY = centerY
- farthestX = centerX
- farthestY = centerY
- }
- override def draw() {
- background(20)
- cam.beginHUD()
- text("Particles: " + currentParticles + "/" + maxParticles, 10, 20)
- if(currentParticles != 0) text("Movements per placement: " + movementsSum/currentParticles.toInt, 10, 40)
- cam.endHUD()
- // redraw points
- for(i <- 0 to currentParticles) {
- val colFactor = i.toFloat / maxParticles
- stroke(colFactor * 255, 255, 255, 255)
- point(pointsX(i), pointsY(i))
- }
- //point(centerX, centerY)
- if(currentParticles == 0) {
- pointsX(0) = centerX
- pointsY(0) = centerY
- currentParticles += 1
- }
- if(currentParticles == maxParticles) noLoop()
- var curJumpDistance = maxJumpDistance
- var closest = -1
- val leftThreshold = if(nearestX - zoneSize > 0) nearestX - zoneSize else 0
- val rightThreshold = if(farthestX + zoneSize > 0) farthestX + zoneSize else 0
- val topThreshold = if(nearestY - zoneSize > 0) nearestY - zoneSize else 0
- val bottomThreshold = if(farthestY + zoneSize > 0) farthestY + zoneSize else 0
- //println("Thresholds: " + leftThreshold + " " + rightThreshold + " " + topThreshold + " " + bottomThreshold)
- var possiblePlaces = Array(
- new Point(random(leftThreshold, nearestX).toInt, random(0, maxY).toInt),
- new Point(random(0, maxX).toInt, random(topThreshold, nearestY).toInt),
- new Point(random(farthestX, rightThreshold).toInt, random(0, maxY).toInt),
- new Point(random(0, maxX).toInt, random(farthestY, bottomThreshold).toInt)
- )
- var newP = Utils.randomPoint(possiblePlaces)
- var placed = false
- var currentMovements = 0
- while(!placed) {
- // drawing point
- /*fill(20)
- noStroke()
- if(prevPoint != null) rect(prevPoint.x-10, prevPoint.y, 20, 20)
- stroke(0, 0, 100, 255)
- point(newP.x, newP.y) */
- // finding closest
- for(i <- 0 until currentParticles) {
- val distance = Utils.dist(newP.x, newP.y, pointsX(i), pointsY(i))
- //println("Closest: " + distance)
- if(distance < curJumpDistance) {
- curJumpDistance = distance
- closest = i
- }
- }
- if(curJumpDistance <= maxStickDistance) {
- // finding free place around
- val stickingTo = new Point(pointsX(closest), pointsY(closest))
- val possiblePlacements = Array(
- new Point(stickingTo.x-1, stickingTo.y),
- new Point(stickingTo.x+1, stickingTo.y),
- new Point(stickingTo.x, stickingTo.y-1),
- new Point(stickingTo.x, stickingTo.y+1)
- )
- val placingTo = Utils.randomPoint(possiblePlacements)
- pointsX(currentParticles) = placingTo.x
- pointsY(currentParticles) = placingTo.y
- currentParticles += 1
- val colFactor = currentParticles.toFloat / maxParticles
- //println("Colfactor: " + colFactor)
- stroke(colFactor * 255, 120, 255, 255)
- //scale(2)
- //point(placingTo.x, placingTo.y)
- //line(pointsX(closest), pointsY(closest), placingTo.x, placingTo.y)
- //println("Placing: " + placingTo.x + " " + placingTo.y)
- if(placingTo.x > farthestX) farthestX = placingTo.x
- if(placingTo.x < nearestX) nearestX = placingTo.x
- if(placingTo.y > farthestY) farthestY = placingTo.y
- if(placingTo.y < nearestY) nearestY = placingTo.y
- placed = true
- movementsSum += currentMovements
- }
- // moving
- possiblePlaces = Array(
- new Point(newP.x-random(maxJumpDistance).toInt, newP.y),
- new Point(newP.x+random(maxJumpDistance).toInt, newP.y),
- new Point(newP.x, newP.y-random(maxJumpDistance).toInt),
- new Point(newP.x, newP.y+random(maxJumpDistance).toInt)
- )
- prevPoint = newP
- val movingTo = Utils.randomPoint(possiblePlaces)
- newP = movingTo
- if(newP.x > maxX || newP.x < 0
- || newP.y > maxY || newP.y < 0) placed = true
- currentMovements += 1
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement