Pois bem, hoje vamos usar mais uma fórmula muito interessante que tomei conhecimento de mais um vídeo do mestre Shiffman, veja:
Filotaxia é o nome em português dessa área de estudo e você pode ler mais sobre ela na wikipédia. No Phyllotaxis você pode ter acesso a um documento e ao algoritmo que o Shiffman e eu usamos.
Mais uma vez usamos o canvas em uma aplicação que vai criar "plantas" aleatóriamente na tela. Para isso, ao contrário do que fez o Shiffman, vamos criar uma classe que vai desenhar a nossa plantinha.
Mas como funciona o algoritmo? A ideia é muito simples, temos um ângulo mágico e aplicamos uma fórmula sobre ele usando um número que é incrementado a cada frame de uma animação, por fim, aplicamos trigonometria para conseguir os pontos X e Y que iremos desenhar na tela. Em seguida, desenhamos os pontos gerados.
A ideia geral é essa! Usamos uma classe genérica com os elementos que mostramos no post anterior e o código ficou bastante limpo. Em breve falo sobre o DrawingFX...
Veja em seguida um screenshot da aplicação, o código, e um vídeo onde eu mostro partes do código e executo a aplicação.
This file contains hidden or 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 org.fxapps.drawingfx.DrawingApp; | |
import static java.lang.Math.*; | |
import javafx.scene.paint.Color; | |
public class FlowersApp extends DrawingApp { | |
Flower[] flowers = new Flower[50]; | |
public static void main(String[] args) { | |
launch(); | |
} | |
public void setup() { | |
title = "Flowers App"; | |
width = 1200; | |
height = 800; | |
for (int i = 0; i < flowers.length; i++) { | |
flowers[i] = new Flower(random.nextInt(width), | |
random.nextInt(height)); | |
} | |
frames = 300; | |
background = Color.LIGHTYELLOW; | |
} | |
public void draw() { | |
for (int i = 0; i < flowers.length; i++) { | |
Flower p = flowers[i]; | |
p.update(); | |
p.show(); | |
if (p.grown) { | |
flowers[i] = new Flower(random.nextInt(width), | |
random.nextInt(height)); | |
} | |
} | |
} | |
public class Flower { | |
int posX, posY, n, duration, size, c = 1; | |
double angle = 137.5, x, y; | |
Color color, stroke; | |
boolean grown = false; | |
public Flower(int posX, int posY) { | |
this.posX = posX; | |
this.posY = posY; | |
this.color = Color.rgb(random.nextInt(255), | |
random.nextInt(255), | |
random.nextInt(255)); | |
this.stroke = Color.rgb(random.nextInt(255), | |
random.nextInt(255), | |
random.nextInt(255)); | |
size = random.nextInt(10) + 5; | |
duration = random.nextInt(1500) + 100; | |
} | |
public void update() { | |
double a = n * angle; | |
double r = c * sqrt(n); | |
x = r * cos(a) + posX; | |
y = r * sin(a) + posY; | |
if (!grown) { | |
n++; | |
} | |
if (n > duration) { | |
grown = true; | |
} | |
} | |
public void show() { | |
ctx.setFill(color); | |
ctx.setStroke(stroke); | |
ctx.strokeOval(x, y, size, size); | |
ctx.fillOval(x, y, size, size); | |
} | |
} | |
} |
Por fim vejam esse vídeo rápido que fiz para demonstar a APP e falar um pouco do código.
Muito bom. Parabéns.
ResponderExcluir