绘制力场
void onMouseDrag() {
PVector direc = new PVector(mouseX-pmouseX, mouseY-pmouseY).normalize();
drawField(pmouseX, pmouseY, direc.mult(force));
}
void drawField(float x, float y, PVector v) {
int column = int(constrain(x/resolution, 0, cols-1));
int row = int(constrain(y/resolution, 0, rows-1));
for (int i=-affectRadius; i<=affectRadius; i++) {
for (int j=-affectRadius; j<=affectRadius; j++) {
if (i*i+j*j<affectRadius*affectRadius) {
try {
field[column+i][row+j].add(v).mult(0.9);
}
private void showField() {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
PVector v = field[x][y];
if (v != null) {
stroke(255, v.mag()*100);
strokeWeight(1);
pushMatrix();
translate(x * resolution, y * resolution);
rotate(v.heading());
line(0, 0, resolution*0.5, 0);
popMatrix();
粒子
class myVector extends PVector {
myVector (float p_x, float p_y) {
super(p_x, p_y);
}
PVector pre;
int count;
color myColor;
}
private void setParticle(int i) {
tabParticles[i] = new myVector((int)random(width), (int)random(height));
tabParticles[i].pre = tabParticles[i].copy();
tabParticles[i].count = (int)random(MIN_LIFE_TIME, MAX_LIFE_TIME);
tabParticles[i].myColor = color(255, 0, 0);
}
void update() {
for (int i = 0; i < NB_PARTICLES; i++) {
myVector t = tabParticles[i];
t.pre = t.copy();
PVector v = ff.lookup(t.x, t.y);
v.limit(MAX_PARTICLE_SPEED);
t.add(v);
t.count--;
void cheak() {
for (int i = 0; i < NB_PARTICLES; i++) {
myVector t = tabParticles[i];
if ( (t.x < 0) || (t.x > width-1)
|| (t.y < 0) || (t.y > height-1)
|| t.count < 0) {
setParticle(i);
使用图片
oid initializeImage() {
myImage = loadImage(IMAGE_PATH);
myImage.resize(width, 0);
imageW = myImage.width;
imageH = myImage.height;
myPixels = new color[imageW * imageH];
myImage.loadPixels();
myPixels = myImage.pixels;
image(myImage, 0, 0);
}
tabParticles[i].myColor = myPixels[(int)(tabParticles[i].y)*imageW + (int)(tabParticles[i].x)];
void setup() {
cp5.setColorBackground(0x141414);
sliderR = cp5.addSlider("Radius")
.setPosition(width/4*3, height-150)
.setRange(1, 20)
.setValue(5).setSize(150, 25);
sliderF = cp5.addSlider("Force")
.setPosition(width/4*3, height-100)
.setRange(0.1, 0.5)
.setValue(0.2).setSize(150, 25);
sliderS = cp5.addSlider("Particle Size")
.setPosition(width/4*3, height-50)
.setRange(0.8, 2)
.setValue(0.5).setSize(150, 25);
}
一流
持久化
void saveField() {
try {
FileWriter out = new FileWriter(file);
for (int i=0; i<cols; i++) {
for (int j=0; j<rows; j++) {
out.write(field[i][j].x+","+field[i][j].y+"\t");
}
out.write("\r\n");
}
out.close();
}
catch(Exception e) {
}
}
void readField() throws IOException {
try {
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
for (int i = 0; (line = in.readLine()) != null; i++) {
String[] temp = line.split("\t");
for (int j=0; j<temp.length; j++) {
String[] xy = temp[j].split(",");
float x = Float.parseFloat(xy[0]);
float y = Float.parseFloat(xy[1]);
field[i][j] = new PVector(x, y);
}
}
in.close();
}
catch(Exception e) {
throw new IOException("no field.txt");
}
}
- 附上近期采风的一个作品,也是用一样的方法做的哦
- 当然场的种类也更丰富了 引力场,螺旋场,噪声场等