【问题标题】:How to draw a grid on top of an image in Processing3 while using updatePixels()如何在处理 3 中使用 update Pixels() 在图像顶部绘制网格
【发布时间】:2021-09-24 09:47:31
【问题描述】:

我正在尝试在图像上绘制网格,而“与朋友和敌人共舞”算法正在运行并更新像素。然而,我的网格在消失之前只显示了一秒钟。我猜这是由于像素更新,但是网格没有随像素更新。如何使用其余像素更新网格?

import java.util.Arrays;

int activeFollowers, totalFollowers;

Follower[] followers;
int[] blurFilter;
boolean saveNextFrame;
PImage img;
int x = 0; // Gaan dit gebruik vir die vertikale lyne
int y = 0; // Gaan dit gebruik vir die horisontale lyne
int spacing = 50; // Gaan dit gebruik om die grid groter en kleiner te maak


void setup() {
  size(1280, 720);
  frameRate(60);
  activeFollowers = 2000;
  totalFollowers = 10000;
  initUI();
  initFollowers(totalFollowers);
  activeSlider.val = activeFollowers/(float)followers.length;
  background(0);
  

  loadPixels();

  blurFilter = new int[width*height];

  saveNextFrame = false;
  noSmooth();
  
}

final static int wf = 4;
final static int hf = 3;
void draw() {
  //VERANDER BACKGROUND KAART
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  PImage img = loadImage("map3.jpg");
  img.resize(1280, 720);
  image(img,0,0);
  int fullImage = img.width * img.height;
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  //**********************************************************************************************************************************************
  
  
  loadPixels();
  int maxCount = 0;
  swarm(followers, activeFollowers);
  if (!blurToggle.set) {
    Arrays.fill(blurFilter, 0);
  }

  // We want to set the brightness according to Follower density per pixel
  // and we can have thousands of Followers overlapping in one pixel.
  // Using FPM, we first we add 0x100 per Follower to every pixel
  for (int i = 0; i < activeFollowers; i++) {
    Follower f = followers[i];
    int x = (int)f.x;
    int y = (int)f.y;
    if (x >= 0 && x < width && y >= 0 && y < height) {
      int c = blurFilter[x + width*y] + 0x100;
      blurFilter[x + width*y] = c;

      //also keep count maximum value of the blurField, to normalise later on.
      if (c > maxCount) {
        maxCount = c;
      }
    }
  }
  

  // Linear scaling of brightness doensn't work - we want to be able to distinguish from 1 to 100000 Followers
  float mv = 1/sqrt(sqrt(maxCount));
  for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
      int v = blurFilter[x + y*width];
      if (v > 0) {
        v = (int)(0xFF*sqrt(sqrt(v))*mv);
        pixels[x + y*width] = 0xFF000000 + v*0x10101;
      }
    }
  }
  
  for (int i = 0; i < fullImage; i++) {
    pixels[i] = pixels[i];
}

  updatePixels();
  
  //Make the GRID
  stroke(255); 
  strokeWeight(1); 
  
  while(x < width){
    line(x, 0, x, height);
    x = x + spacing;
  }
  
  while(y < height){
    line(0, y, width, y);
    y = y + spacing;
  }

  // By blurring over time, both the path and the density of the Followers
  // is easier to distinguish
  if (blurToggle.set) {
    blur(blurFilter, pixels, width, height);
    fade(pixels, blurFilter, 15, 4);
    //arrayCopy(pixels, blurFilter);
  }
  Arrays.fill(pixels, 0xFF000000);

  drawUI();
  
}

【问题讨论】:

    标签: java processing


    【解决方案1】:

    看起来您从未重置用于循环网格线的 xy 变量。

    所以在第一次通过后,x 总是大于 width 并且 y 总是大于 height,所以你的循环不会运行。

      x = 0;
      while(x < width){
        line(x, 0, x, height);
        x = x + spacing;
      }
      
      y = 0;
      while(y < height){
        line(0, y, width, y);
        y = y + spacing;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 2010-11-26
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      相关资源
      最近更新 更多