Created
February 10, 2017 16:20
-
-
Save ma2saka/a632570dcb8c26aee0cef1a56305ae5c to your computer and use it in GitHub Desktop.
https://www.openprocessing.org/sketch/149337 を Scala に移植してみたもの。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.core.PConstants._ | |
import processing.core.{PApplet, PFont, PGraphics, PVector} | |
import scala.collection.mutable.ArrayBuffer | |
// Original is : https://www.openprocessing.org/sketch/149337 by Jerome Herr | |
// | |
class App extends PApplet { | |
lazy val letterGraphic: PGraphics = { | |
createGraphics(width, height) | |
} | |
lazy val font: PFont = { | |
createFont("ArialHebrew", FONT_SIZE) | |
} | |
val NUM = 2000 | |
val ballCollection = new ArrayBuffer[Ball](NUM) | |
val FONT_SIZE = 400 | |
var letter = "A" | |
var theta: Float = _ | |
override def settings(): Unit = { | |
size(400, 400, JAVA2D) | |
} | |
override def setup(): Unit = { | |
updateLetter() | |
createBallAndLines() | |
} | |
private def updateLetter(): Unit ={ | |
ballCollection.clear() | |
letterGraphic.beginDraw() | |
letterGraphic.noStroke() | |
letterGraphic.background(255) | |
letterGraphic.fill(0) | |
letterGraphic.textFont(font, FONT_SIZE) | |
letterGraphic.textAlign(CENTER) | |
letterGraphic.text(letter, 200f, 350f) | |
letterGraphic.endDraw() | |
letterGraphic.loadPixels() | |
} | |
private def createBallAndLines(): Unit = { | |
for (i <- 0 to NUM) { | |
val x = random(width).toInt | |
val y = random(height).toInt | |
val c = letterGraphic.pixels(x + y * width) | |
if (brightness(c) < 255) { | |
val org = new PVector(x, y) | |
val radius = random(5, 10) | |
val loc = new PVector(org.x + radius, org.y) | |
val offSet = random(TWO_PI) | |
val dir = if (random(1) > .5) { | |
-1 | |
} else { | |
1 | |
} | |
val myBall = new Ball(org, loc, radius, dir, offSet) | |
ballCollection.append(myBall) | |
} | |
} | |
} | |
override def keyPressed(): Unit = { | |
if (key != CODED) { | |
letter = PApplet.str(key) | |
updateLetter() | |
createBallAndLines() | |
} | |
} | |
override def draw(): Unit = { | |
background(20) | |
textAlign(LEFT, TOP) | |
text("%2.3f fps".format(frameRate), 0, 0) | |
text("current : %s".format(letter), 0 , 15) | |
ballCollection.foreach({ ball => | |
ball.draw() | |
}) | |
theta += .0523f | |
} | |
class Ball(val org: PVector, val loc: PVector, val radius: Float, val dir: Int, val offset: Float) { | |
val SIZE = 2 | |
val MAX_DIST = 20 | |
val OPACITY = 35 | |
val connection = new Array[Boolean](NUM) | |
var countC = 1 | |
def draw(): Unit = { | |
display() | |
move() | |
lineBetween() | |
} | |
private def move(): Unit = { | |
loc.x = org.x + PApplet.sin(theta * dir + offset) * radius | |
loc.y = org.y + PApplet.cos(theta * dir + offset) * radius | |
} | |
private def display(): Unit = { | |
noStroke() | |
fill(255, 100) | |
ellipse(loc.x, loc.y, SIZE, SIZE) | |
} | |
private def lineBetween(): Unit = { | |
var other: Ball = null | |
for (i <- 0 to ballCollection.size - 1) { | |
other = ballCollection(i) | |
val distance = loc.dist(other.loc) | |
if (distance > 0 && distance < MAX_DIST) { | |
stroke(255, OPACITY) | |
line(loc.x, loc.y, other.loc.x, other.loc.y) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment