Skip to content

Instantly share code, notes, and snippets.

@martoo6
Created June 16, 2014 23:25
Show Gist options
  • Save martoo6/c6ac46300e3a07e1408a to your computer and use it in GitHub Desktop.
Save martoo6/c6ac46300e3a07e1408a to your computer and use it in GitHub Desktop.
package main
import processing.core._
/**
* Created by fake.vyral on 13/06/2014.
*/
class Sketch extends PApplet{
val CANTSPHERES= 20
val gridSize = 200
var values = Array.ofDim[Float](gridSize*2,gridSize*2)
var spheres = new Array[Esfera](CANTSPHERES+1)
val stepSize = 10
override def setup={
size(1280,720, PConstants.P3D)
background(0)
noFill()
sphereDetail(10)
stroke(255)
for(i <- 0 to CANTSPHERES){
spheres(i) = new Esfera(new PVector(random(-gridSize,gridSize)*stepSize, -100,random(-gridSize,gridSize)*stepSize), random(10,100))
}
//Distance calc
for(a <- 0 to gridSize*2-1) for(b <- 0 to gridSize*2-1){
values(a)(b) = spheres.foldLeft(0f)((x,y)=> x + y.size*100000f/PApplet.pow(getDist(y.pos,(a-gridSize)*stepSize,(b-gridSize)*stepSize),2))
}
}
def getDist(v:PVector, x:Float, z:Float):Float={
return v.dist(new PVector(x,0,z))
}
override def draw={
background(0)
translate(width/2,height*0.75f,-800)
rotateX(PConstants.PI/8)
rotateY(frameCount*0.05f)
drawGrid
drawSpheres
}
override def keyPressed={
saveFrame("######.jpg")
}
def drawSpheres={
for(s <-spheres){
pushMatrix()
translate(s.pos.x,-100,s.pos.z)
//line(0,-500,0,0,500,0)
sphere(s.size/2)
popMatrix()
}
}
def drawGrid={
drawLines((a:Int,b:Int)=> {vertex((a-gridSize)*stepSize,values(a)(b),(b-gridSize)*stepSize); stroke(255,PApplet.map(values(a)(b),0, 550, 150, 0))})
drawLines((a:Int,b:Int)=> {vertex((b-gridSize)*stepSize,values(b)(a),(a-gridSize)*stepSize); stroke(255,PApplet.map(values(b)(a),0, 550, 150, 0))})
}
def drawLines(f: (Int, Int)=>Unit )={
for(a <- 0 to gridSize*2-1) {
beginShape()
for(b <- 0 to gridSize*2-1) f(a,b)
endShape()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment