如何重新启动正在处理的草图项目? [英] How to restart a sketch project in processing?

查看:88
本文介绍了如何重新启动正在处理的草图项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个 Processing 项目,但我不知道如何在项目结束后重新启动该项目.我已经搜索并发现 setup() 方法会成功.但它不起作用.谁能帮我.我希望草图在完成后自行重新启动.

I am working on a Processing project, but I don´t know how to restart the project once it is over. I have searched and found that the setup() method will make it. But it's not working. Can anyone help me. I would like the sketch to restart by itself once it is finished.

    /* OpenProcessing Tweak of *@*http://www.openprocessing.org/sketch/59807*@* */
/* !do not delete the line above, required for linking your tweak if you upload again */
//
// outline: takes an image (image.jpg) and creates a sketch version
//
// procsilas (procsilas@hotmail.com / http://procsilas.net)
//

String iName="image.jpeg";

void setup() {
  llegeixImatge("./"+iName);
  size(img.width, img.height);
}

// parameters
// NO real control, so be careful

int NP=6000; // 1000 for line art, 10000 for complex images, O(N^2) so be patient!!!
int B=1; // try 2 or 3
float THR=28; // range 5-50
float MD=6; // range 0-10
int NMP=6; // range 1-15

float[][] punts;
color[] cpunts;
int [] usat;
int [] NmP=new int[NMP];
float [] NdmP=new float[NMP];

int inici=0;

PImage img;

void llegeixImatge(String s) {
  img = loadImage(s);
  img.loadPixels();
}

float fVar(int x, int y) {
  // neighborhood 2B+1x2B+1 pixels
  float m=0;
  for (int k1=-B; k1<=B; k1++) {
    for (int k2=-B; k2<=B; k2++) {
      color c=img.pixels[(y+k1)*img.width+(x+k2)];
      m+=brightness(c);
    }
  }
  m/=float((2*B+1)*(2*B+1));
  float v=0;
  for (int k1=-B; k1<B; k1++) {
    for (int k2=-B; k2<B; k2++) {
      color c=img.pixels[(y+k1)*img.width+(x+k2)];
      v+=(brightness(c)-m)*(brightness(c)-m);
    }
  }
  v=sqrt(v)/(float) (2*B+1);    

  return v;
}

void creaPunts() {
  punts = new float[NP][2];
  cpunts = new color[NP];
  usat = new int[NP];

  int nint1=0;
  int nint2=0;

  for (int i=0; i<NP;) {

    int x=B+int(random(width-2*B));
    int y=B+int(random(height-2*B));

    //println(i+" = "+x+", "+y+": "+THR+", "+MD);

    // points need to be at least MD far from each other
    int flag=0;
    if (MD>0.0) {  
      for (int j=0; flag==0 && j<i; j++) {
        if (dist(x, y, punts[j][0], punts[j][1])<MD) {
          flag=1;
        }
      }
    }

    if (flag==0) { 
      nint1=0;
      float f=fVar(x, y);

      // use only "valid" points      
      if (f>=THR) {
        nint2=0;
        punts[i][0]=x;
        punts[i][1]=y;
        cpunts[i]=img.pixels[y*img.width+x];
        usat[i]=0;
        i++;
      } 
      else {
        nint2++;
        if (nint2>=10) {
          THR/=(1+1.0/float(NP-i));
          MD/=(1+1.0/float(NP-i));
          nint2=0;
        }
      }
    } 
    else {
      nint1++;
      if (nint1>=10) {
        MD/=2.0;
        THR*=1.618;
        nint1=0;
      }
    }
  }
}

int NessimMesProper(int i) {
  if (NMP<=1) {
    int mP=-1;
    float dmP=dist(0, 0, width, height);
    for (int j=0; j<NP; j++) {
      if (usat[j]==0) {
        float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
        if (jmP<dmP) {
          dmP=jmP;
          mP=j;
        }
      }
    }
    return mP;
  } 
  else {
    for (int j=0; j<NMP; j++) {
      NmP[j]=-1;    
      NdmP[j]=dist(0, 0, width, height);
    }
    for (int j=0; j<NP; j++) {
      if (usat[j]==0) {
        float jmP=dist(punts[i][0], punts[i][1], punts[j][0], punts[j][1]);
        int k=NMP;
        while(k>0 && NdmP[k-1]>jmP) {
          k--;
        }
        if (k<NMP) {
          for (int l=0; l<(NMP-k)-1; l++) {
            NmP[(NMP-1)-l]=NmP[(NMP-1)-(l+1)];
            NdmP[(NMP-1)-l]=NdmP[(NMP-1)-(l+1)];
          }
          NmP[k]=j;
          NdmP[k]=jmP;
        }
      }
    }
    return NmP[NMP-1];
  }
}

int fase=0;

void draw() {
  if (fase==0) {
    creaPunts();
    background(#FFFFFF);
    fase=1;
  } 
  else {
    if (inici!=-1) {
      stroke(#000000);
      usat[inici]=1;

      int seguent=NessimMesProper(inici);
      if (seguent!=-1) {
        line(punts[inici][0], punts[inici][1], punts[seguent][0], punts[seguent][1]);
      }
      inici=seguent;
    } 
    else {
      //save("outline_"+iName);


    }
  }
}

推荐答案

你应该不要自己调用setup().

步骤 1: 将程序的状态封装在一组变量中.

Step 1: Encapsulate the state of your program in a set of variables.

第 2 步:使用这些变量绘制草图.

Step 2: Use those variables to draw your sketch.

第 3 步:修改这些变量以更改正在绘制的内容.

Step 3: Modify those variables to change what's being drawn.

第 4 步:当您想要重置草图时,只需将这些变量重置为其初始值.

Step 4: Simply reset those variables to their initial values when you want to reset the sketch.

这是一个示例程序,它将其状态(用户单击的位置)存储在 ArrayList 中.它使用 ArrayList 来绘制草图,并在用户点击时添加新点.当用户键入一个键时,通过清除 ArrayList 来重置草图:

Here's an example program that stores its state (the positions the user has clicked) in an ArrayList. It uses that ArrayList to draw the sketch, and new points are added whenever the user clicks. When the user types a key, the sketch is reset by clearing out the ArrayList:

ArrayList<PVector> points = new ArrayList<PVector>();

void setup(){
  size(500, 500);
}

void draw(){
  background(0);
  for(PVector p : points){
    ellipse(p.x, p.y, 20, 20);
  }
}

void mousePressed(){
  points.add(new PVector(mouseX, mouseY));
}

void keyPressed(){
 points.clear(); 
}

这篇关于如何重新启动正在处理的草图项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆