Variables and conditions in processing - processing

Just wondering if I want to do a loop to make every time I mousePressed to draw a circle and each time fill with 3 different colour eg yellow orange and blue and so on I use for(int countmouseClick=n; n<=3 ; n++) how should I finish the rest? Should I reset n if n=3 to n= 0?. The circles can stay on the canvas
Not sure if it correct.
void mousePressed(){
for ( int count=0; n<= 2; n++){
fill(255,0,0);
fill(0,255,0);
fill(0,0,255);
}
circle(mouseX,mouseY,20);
}
//i have adopted the idea of %, it telling me the variable n doesn't exist, why is this
int count;
void setup(){
size(400,400);
background(255);
}
void draw(){
}
void mousePressed(){
int count=n;
if(n>2){ //reset mouse click to 0
n=0;
}
for(int n= 0;n<=2;n++){//count mouseclicked, if n<=2, n=n+1
if(n%3=0){
fill(255,0,0);
}else if (n%3=1){
fill(0,255,0);
}
else {
fill(0,0,255);
}
circle(mouseX,mouseY,20);
}
}

a better way of writing the code is to write a helper function which takes countmouseclick and return the color
you can read these articles for understanding the code below
https://processing.org/reference/modulo.html
https://processing.org/reference/switch.html
Color getColor(int countMouseClick){
switch(countMouseClick%3) {
case 0:
return COLOR.ORANGE;
case 1:
return COLOR.YELLOW;
break;
case 2:
return COLOR.BLUE;
default:
return COLOR.GREEN;
}

Related

Processing: How to fix timer display and how to display time score at end of game?

I am working on an ascending order puzzle that records the time it takes the user to complete the puzzle and once the puzzle is completed it takes the user to a game over screen that shows the user their time. My issue is that the time that is being displayed is overlapping and not updating as it should be. I am also unsure how to save the time that the user took to complete the puzzle.
final int NUM_SQUARES = 4;
int[][] board = new int[NUM_SQUARES][NUM_SQUARES];
int sqSide;
Timer startTimer;
void setup(){
size(500, 500);
setupGame();
sqSide = width/NUM_SQUARES;
}
void setupGame(){
sqSide = width/NUM_SQUARES;
startTimer = new Timer(0);
//populate the board
//generate random number
//check if we have it already inside the array
//if we have, then go on to generate another random number
//if we do not have it, then we can store inside array
for(int row=0; row<NUM_SQUARES; row++){
for(int col=0; col<NUM_SQUARES; col++){
int randVal;
do{
randVal = int(random(1, NUM_SQUARES*NUM_SQUARES+1) );
}while( searchFor(randVal) );
board[row][col] = randVal;
}
}
//visual representation of the board
for(int row=0; row<NUM_SQUARES; row++){
for(int col=0; col<NUM_SQUARES; col++){
fill(random(0,255), random(0,255), random(0,255));
rect(col*sqSide, row*sqSide, sqSide, sqSide);
fill(0);
textSize(30);
text(board[row][col], (col+0.5)*sqSide, (row+0.5)*sqSide);
}
}
}
class Timer{
float Time;
Timer(float set){
Time = set;
}
float getTime(){
return(Time);
}
void setTime(float set){
Time = set;
}
void countUP(){
Time += 1/frameRate;
}
}
boolean searchFor(int itemToBeSearched){
for(int i=0; i<NUM_SQUARES; i++){
for(int j=0; j<NUM_SQUARES; j++){
if(itemToBeSearched == board[i][j]){
return true;
}
}
}
return false;
}
void draw(){
startTimer.countUP();
fill(0);
text(startTimer.getTime(), 20,20);
}
int clickedRow, clickedCol, releasedRow, releasedCol;
void mousePressed(){
clickedRow = int(mouseY/sqSide);
clickedCol = int(mouseX/sqSide);
}
void mouseReleased(){
releasedRow = int(mouseY/sqSide);
releasedCol = int(mouseX/sqSide);
//swap
int buffer = board[clickedRow][clickedCol];
board[clickedRow][clickedCol] = board[releasedRow][releasedCol];
board[releasedRow][releasedCol] = buffer;
//visual representation - finish up
//show what is inside board[clickedRow][clikedCol]
//then show what is inside board[releasedRow][releasedCol]
//where the child pressed
fill(random(0,255), random(0,255), random(0,255));
rect(clickedCol*sqSide, clickedRow*sqSide, sqSide, sqSide);
fill(0);
text(board[clickedRow][clickedCol],(clickedCol+0.5)*sqSide, (clickedRow+0.5)*sqSide) ;
//where the child released
fill(random(0,255), random(0,255), random(0,255));
rect(releasedCol*sqSide, releasedRow*sqSide, sqSide, sqSide);
fill(0);
text(board[releasedRow][releasedCol],(releasedCol+0.5)*sqSide, (releasedRow+0.5)*sqSide);
if(gameOver()==true){ //calling function gameOver
background(255);
String s = "Congratulations!";
String d = "Click to start again!";
fill(0);
text(s, 125, 225);
text(d, 125, 250);
if(mousePressed == true){
setupGame();
}
}
}
//definition of gameOver
boolean gameOver(){
int counter=1;
for(int row=0; row<NUM_SQUARES; row++){
for(int col=0; col<NUM_SQUARES; col++){
if(board[row][col] !=counter){
return false;
}
counter++;
}
}
return true;
}
There's plenty of good work there, especially when it comes to managing the board / swapping elements / etc. It takes focus to keep it in check.
It also looks like there's a bit of confusion regarding drawing.
You can draw elements once and in draw(); render overlapping elements on top as you are doing now, however you need to pay attention to what needs to be rendered once or multiple times.
The timer text is leaving trails because the background is never cleared, but the board is only drawn once. You will want to redraw the background/board to fix the text, but some bits of code will need to move or need to be handled differently.
For example:
fill(random(0,255), random(0,255), random(0,255));
If you call that in draw() the colours will change continuously you don't want.
You could however generate the colours once in setup() then re-use them in draw() without resetting the values.
There seems to be also some confusion with mouse events and when or in which state should resets happen.
Here's a re-organised version of your code taking into account the notes above:
final int NUM_SQUARES = 4;
int[][] board = new int[NUM_SQUARES][NUM_SQUARES];
// also remember the colours set once in setup() to re-use in draw()
int[][] boardColors = new int[NUM_SQUARES][NUM_SQUARES];
int sqSide;
Timer startTimer;
int clickedRow, clickedCol, releasedRow, releasedCol;
// this will store the result of gameOver() once so it can be re-used
boolean isGameOver;
void setup(){
size(500, 500);
setupGame();
}
void setupGame(){
sqSide = width/NUM_SQUARES;
startTimer = new Timer(0);
clearBoard();
//populate the board
//generate random number
//check if we have it already inside the array
//if we have, then go on to generate another random number
//if we do not have it, then we can store inside array
shuffleBoard();
}
void clearBoard(){
for(int row = 0; row < NUM_SQUARES; row++){
for(int col = 0; col < NUM_SQUARES; col++){
board[row][col] = 0;
}
}
}
void shuffleBoard(){
for(int row = 0; row < NUM_SQUARES; row++){
for(int col = 0; col < NUM_SQUARES; col++){
int randVal;
do{
randVal = int(random(1, NUM_SQUARES*NUM_SQUARES+1) );
}while( searchFor(randVal) );
board[row][col] = randVal;
boardColors[row][col] = color(random(0,255), random(0,255), random(0,255));
}
}
}
void drawBoard(){
//visual representation of the board
for(int row = 0; row < NUM_SQUARES; row++){
for(int col = 0; col < NUM_SQUARES; col++){
fill(boardColors[row][col]);
rect(col * sqSide, row * sqSide, sqSide, sqSide);
fill(255);
textSize(30);
text(board[row][col], (col + 0.5) * sqSide, (row + 0.5) * sqSide);
}
}
}
boolean searchFor(int itemToBeSearched){
for(int i = 0; i < NUM_SQUARES; i++){
for(int j = 0; j < NUM_SQUARES; j++){
if(itemToBeSearched == board[i][j]){
return true;
}
}
}
return false;
}
void draw(){
background(255);
// draw based on state
if(gameOver()){
// render game over state
String s = "Congratulations!";
String d = "Click to start again!";
fill(0);
text(s, 125, 225);
text(d, 125, 250);
// reset game only if clicking in game over state
if(mousePressed == true){
setupGame();
}
}else{
// render game state
startTimer.countUp();
// re-render board
drawBoard();
// render text on top
fill(0);
text(startTimer.getTime(), 20, 30);
}
}
void mousePressed(){
clickedRow = int(mouseY/sqSide);
clickedCol = int(mouseX/sqSide);
}
void mouseReleased(){
releasedRow = int(mouseY/sqSide);
releasedCol = int(mouseX/sqSide);
//swap
int buffer = board[clickedRow][clickedCol];
board[clickedRow][clickedCol] = board[releasedRow][releasedCol];
board[releasedRow][releasedCol] = buffer;
}
//definition of gameOver
boolean gameOver(){
int counter=1;
for(int row=0; row<NUM_SQUARES; row++){
for(int col=0; col<NUM_SQUARES; col++){
if(board[row][col] !=counter){
return false;
}
counter++;
}
}
return true;
}
class Timer{
float time;
Timer(float set){
time = set;
}
float getTime(){
return(time);
}
void setTime(float set){
time = set;
}
void countUp(){
time += 1 / frameRate;
}
}
It might make it easier to organise a basic finite state machine(FSM).
It sounds more complicated than it is: the basic idea is to isolate functionality per state and tightly handle transitions between states which allow you to reset/update data accordingly. For example a game play state will only have to deal with gameplay related data until the state completes. Similarly the game over state only deals with it's data until it completes (when a user clicks). There are many ways you can write this.
Here's a suggestion organised around how a Processing sketch runs:
setup() is where things get set up once at the start
draw() is where things get updated/rendered
mouse / key / etc. events handle user input
You're already using a class (Timer) this will be very similar:
// the two states
StateDisplay playState;
StateDisplay gameOverState;
// a reference to the current state
StateDisplay currentState;
void setup(){
size(500, 500);
textAlign(CENTER);
// setup each state
playState = new PlayState();
gameOverState = new GameOverState();
// set the reference to the 1st state
currentState = playState;
}
// forward events to the current state: doesn't matter if it's play or game over, they're can all handle it
void draw(){
currentState.draw();
}
void mousePressed(){
currentState.mousePressed();
}
void mouseReleased(){
currentState.mouseReleased();
}
// global function to be called by each state as it exits
// this does the state switch: reset state data as required
void onStateExit(StateDisplay exitingState){
// game play state to game over state
if(exitingState == playState){
// cast each state to access specialised variables
// in this case store the last timer value to display on the game over screen
((GameOverState)gameOverState).timerValue = ((PlayState)playState).startTimer.getTimeFormatted();
// set the state
currentState = gameOverState;
}
// game over state to game play state
if(exitingState == gameOverState){
// reset game
playState.setup();
// set the state
currentState = playState;
}
}
class Timer{
float time;
Timer(float set){
time = set;
}
float getTime(){
return time;
}
String getTimeFormatted(){
return nfc(time, 2);
}
void setTime(float set){
time = set;
}
void countUp(){
time += 1 / frameRate;
}
}
// "parent" state class that all other states will extend
// and inherit functionality from
class StateDisplay{
StateDisplay(){
// as soon as the constructor is called, set up this state
this.setup();
}
void setup(){
// to be overriden by subclass
}
void draw(){
// to be overriden by subclass
}
void mousePressed(){
// to be overriden by subclass
}
void mouseReleased(){
// to be overriden by subclass
}
}
class PlayState extends StateDisplay{
final int NUM_SQUARES = 4;
int[][] board;
int[][] boardColors;
int sqSide;
Timer startTimer;
int clickedRow, clickedCol, releasedRow, releasedCol;
void setup(){
setupGame();
}
void setupGame(){
this.board = new int[NUM_SQUARES][NUM_SQUARES];
this.boardColors = new int[NUM_SQUARES][NUM_SQUARES];
sqSide = width/NUM_SQUARES;
startTimer = new Timer(0);
//populate the board
//generate random number
//check if we have it already inside the array
//if we have, then go on to generate another random number
//if we do not have it, then we can store inside array
for(int row = 0; row < NUM_SQUARES; row++){
for(int col = 0; col < NUM_SQUARES; col++){
int randVal;
do{
randVal = int(random(1, NUM_SQUARES*NUM_SQUARES+1) );
}while( searchFor(randVal) );
board[row][col] = randVal;
boardColors[row][col] = color(random(0,255), random(0,255), random(0,255));
}
}
}
boolean searchFor(int itemToBeSearched){
for(int i = 0; i < NUM_SQUARES; i++){
for(int j = 0; j < NUM_SQUARES; j++){
if(itemToBeSearched == board[i][j]){
return true;
}
}
}
return false;
}
//definition of gameOver
boolean gameOver(){
int counter=1;
for(int row=0; row<NUM_SQUARES; row++){
for(int col=0; col<NUM_SQUARES; col++){
if(board[row][col] !=counter){
return false;
}
counter++;
}
}
return true;
}
void drawBoard(){
//visual representation of the board
for(int row = 0; row < NUM_SQUARES; row++){
for(int col = 0; col < NUM_SQUARES; col++){
fill(boardColors[row][col]);
rect(col * sqSide, row*sqSide, sqSide, sqSide);
fill(255);
textSize(30);
text(board[row][col], (col+0.5) * sqSide, (row + 0.5) * sqSide);
}
}
}
void draw(){
// clear screen
background(255);
// redraw board
drawBoard();
// optional: drag and drop visual cue
if(mousePressed){
fill(255, 192);
text(board[clickedRow][clickedCol], mouseX, mouseY);
}
startTimer.countUp();
fill(255);
text(startTimer.getTimeFormatted()+"s", width * 0.5, 30);
}
void mousePressed(){
clickedRow = int(mouseY/sqSide);
clickedCol = int(mouseX/sqSide);
}
void mouseReleased(){
releasedRow = int(mouseY/sqSide);
releasedCol = int(mouseX/sqSide);
//swap
int buffer = board[clickedRow][clickedCol];
board[clickedRow][clickedCol] = board[releasedRow][releasedCol];
board[releasedRow][releasedCol] = buffer;
if(gameOver()){
onStateExit(this);
}
}
}
class GameOverState extends StateDisplay{
// time taken to solve
String timerValue = "";
void draw(){
String s = "Congratulations!\n" +
"Solved in " + timerValue + "s\n" +
"Click to start again!";
background(255);
fill(0);
text(s, width * 0.5, 225);
}
void mouseReleased(){
onStateExit(this);
}
}
The code may look verbose but essentially most of the work that's doing is grouping functionality per state: as if you're running mini-sketches and swapping between them.
The things that may be new are:
extending a class: the class that extends (subclass) will inherit properties/methods from the parent class (superclass)
polymorphism: relying on what subclasses have in common with the superclass to treat them as if they are the same (though internally the implementation for each is different). (e.g. all states have draw(), but each state renders something else)
casting: in this particular case allowing subclass to behave as itself (with all it's extra properties/methods), not just what's shared from the superclass. More specifically in the code above the timer data is passed from game play state to the game over state to display.
Even though it's a bit more work to set up this way it may be worth doing to keep states isolated and easily add more states using the same logic (e.g. a high scores stable, a start / game menu screen / etc.). With each state as a min-sketch you can also keep it organised with tabs, saving time having scroll up and down all the time:

How can I create points, display them and store them in some sort of Array

I want to create points and then store them in an array. I'm doing this to put a linear regression through my data points afterwards. So I need to be able to cycle through all my points.
I could not find anything like that on the web for processing and as I was not really able to do it, I need your help. Here is my approach, but it doesn't seem to work:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
background(255);
}
void draw(){
for (int i = 1; i == dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
I would like to have a program to create points and store them in an array (or something similar, that you can cycle through).
Most of your code makes sense, there are only two gotchas I could spot that may prevent you from cycling through all your points and visualising them:
your condition is will go to an array index out of bounds: try for (int i = 0; i < dataPoints.size(); i++)
remember to clear the frame, otherwise you're drawing on top of the same dots over and over again
Remember array indices start at 0 in Processing/Java (and likewise the last index will not be the size() of your array, but the 1 less, hence the < in the for condition)
Here is your code with the above tweaks:
ArrayList<dataPoint> dataPoints = new ArrayList<dataPoint>();
void setup(){
size(1000, 1000);
}
void draw(){
background(255);
for (int i = 0; i < dataPoints.size(); i++) {
// An ArrayList doesn't know what it is storing so we have to cast the object coming out
dataPoint Point = dataPoints.get(i);
Point.display();
}
}
void mousePressed() {
dataPoints.add(new dataPoint(mouseX, mouseY));
}
class dataPoint {
float x;
float y;
dataPoint(int tempX, int tempY) {
x = tempX;
y = tempY;
}
void display() {
strokeWeight(10);
stroke(255,0,0);
point(x,y);
}
}
Note that Processing has a handy PVector class (which has x,y properties) so you could do something like this:
ArrayList<PVector> dataPoints = new ArrayList<PVector>();
void setup(){
size(1000, 1000);
strokeWeight(10);
stroke(255,0,0);
noFill();
}
void draw(){
background(255);
beginShape();
for (int i = 0; i < dataPoints.size(); i++) {
PVector point = dataPoints.get(i);
vertex(point.x,point.y);
}
endShape();
}
void mousePressed() {
dataPoints.add(new PVector(mouseX, mouseY));
}
This a bit of a detail, but I recommend to following Java Naming Convention to keep the code consistent. (For example: renaming the dataPoint class to DataPoint and renaming the Point instance to point)

Automation of selection in Processing

I am currently using a processing sketch to work through a large number of images I have in a folder. I have set an onClick command to advance to the next image in the string. It is quite time consuming and I would like to automate the action that once the sketch is completed the sketch would repeat it's self selecting the next image from the string. The onClick command also save the image the export folder but each time saves with the same file name, I've tried to set up sequential numbering but it hasn't worked so far. Any help would be greatly appreciated.
String[] imgNames = {"1.jpg", "2.jpg", "3.jpg"};
PImage img;
int imgIndex = 0;
void nextImage() {
background(255);
frameRate(10000);
loop();
frameCount = 0;
img = loadImage(imgNames[imgIndex]);
img.loadPixels();
imgIndex += 1;
if (imgIndex >= imgNames.length) {
imgIndex = 0;
}
}
void paintStroke(float strokeLength, color strokeColor, int strokeThickness) {
float stepLength = strokeLength/4.0;
// Determines if the stroke is curved. A straight line is 0.
float tangent1 = 0;
float tangent2 = 0;
float odds = random(1.0);
if (odds < 0.7) {
tangent1 = random(-strokeLength, strokeLength);
tangent2 = random(-strokeLength, strokeLength);
}
// Draw a big stroke
noFill();
stroke(strokeColor);
strokeWeight(strokeThickness);
curve(tangent1, -stepLength*2, 0, -stepLength, 0, stepLength, tangent2, stepLength*2);
int z = 1;
// Draw stroke's details
for (int num = strokeThickness; num > 0; num --) {
float offset = random(-50, 25);
color newColor = color(red(strokeColor)+offset, green(strokeColor)+offset, blue(strokeColor)+offset, random(100, 255));
stroke(newColor);
strokeWeight((int)random(0, 3));
curve(tangent1, -stepLength*2, z-strokeThickness/2, -stepLength*random(0.9, 1.1), z-strokeThickness/2, stepLength*random(0.9, 1.1), tangent2, stepLength*2);
z += 1;
}
}
void setup() {
size(1600, 700);
nextImage();
}
void draw() {
translate(width/2, height/2);
int index = 0;
for (int y = 0; y < img.height; y+=1) {
for (int x = 0; x < img.width; x+=1) {
int odds = (int)random(20000);
if (odds < 1) {
color pixelColor = img.pixels[index];
pixelColor = color(red(pixelColor), green(pixelColor), blue(pixelColor), 100);
pushMatrix();
translate(x-img.width/2, y-img.height/2);
rotate(radians(random(-90, 90)));
// Paint by layers from rough strokes to finer details
if (frameCount < 20) {
// Big rough strokes
paintStroke(random(150, 250), pixelColor, (int)random(20, 40));
} else if (frameCount < 1000) {
// Thick strokes
paintStroke(random(75, 125), pixelColor, (int)random(8, 12));
} else if (frameCount < 1500) {
// Small strokes
paintStroke(random(20, 30), pixelColor, (int)random(1, 4));
} else if (frameCount < 10000) {
// Big dots
paintStroke(random(5, 10), pixelColor, (int)random(5, 8));
} else if (frameCount < 10000) {
// Small dots
paintStroke(random(1, 2), pixelColor, (int)random(1, 3));
}
popMatrix();
}
index += 1;
}
}
if (frameCount > 10000) {
noLoop();
}
// if(key == 's'){
// println("Saving...");
// saveFrame("screen-####.jpg");
// println("Done saving.");
// }
}
void mousePressed() {
save("001.tif");
nextImage();
}
Can't you just call the nextImage() function from inside this if statement?
if (frameCount > 10000) {
noLoop();
}
Would be:
if (frameCount > 10000) {
nextImage();
}
As for using different filenames, just use an int that you increment whenever you save the file, and use that value in the save() function. Or you could use the frameCount variable:
save("img" + frameCount + ".tif");
If you have follow-up questions, please post a MCVE instead of your whole project. Try to isolate one problem at a time in a small example program that only does that one thing. Good luck.

How to restart a sketch project in processing?

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);
}
}
}
You should not call setup() yourself.
Step 1: Encapsulate the state of your program in a set of variables.
Step 2: Use those variables to draw your sketch.
Step 3: Modify those variables to change what's being drawn.
Step 4: Simply reset those variables to their initial values when you want to reset the sketch.
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();
}

Processing: Numbered Image squence

I am trying to create 100 frames where the numbers 0-100 are "printed" in the center of every frame. If I try the code below I get the error "It looks like you're mixing "active" and "static" modes".
size(1440,900);
font = loadFont("Arial-Black-96.vlw");
textFont(font,96);
int x = 0;
void draw() {
background(204);
y=0;
if (x < 100) {
text(y, 10, 50);
x = x + 1;
y=y+1;
} else {
noLoop();
}
// Saves each frame as screen-0001.tif, screen-0002.tif, etc.
saveFrame();
}
You need to wrap the first 3 lines in the setup() function.
Like:
void setup(){
size(1440,900);
font = loadFont("Arial-Black-96.vlw");
textFont(font,96);
}
I answered this without running the code, there were others issues, here a version of your code:
PFont font;
int x = 0;
int size = 96;
void setup() {
size(1440, 900);
font = createFont("Arial-Black", size);
textFont(font);
}
void draw() {
background(204);
if (x <= 100) {
String number = nf(x, 2);
text(number, width/2 - textWidth(number)/2, height/2);
x++;
saveFrame();
}
else {
exit();
}
}

Resources