We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to implement pan, zoom, and start and stop settings into this program but the boolean drawing won't go away with any function. I don't know how to reset the drawn image, despite looking all through the references for processing and numerous code examples. How do I reset the boolean drawing in this code thus clearing the screen on keypressed?
// main setup settings
int dimy = 800; // screen width 1681
int dimx = 850; //screen height 1019
int bailout = 2000; // number of iterations before bail
int plots = 171294; // number of plots to execute per frame (x30 = plots per second) 171294
//zoom/magnification
float magx = 4.0; // default 3.0, smaller means more zoom
float magy = magx*(dimy/float(dimx));
//pan
float panx = 0.3; //default is 0.5
float pany = 0.0; //default is 0.0
//brightness multiplier
float brt = 1.0; // brightness factor
// 2D array to hold exposure values
int[] exposure = new int[dimx*dimy];
int maxexposure; // maximum exposure value
int time = 0;
int exposures = 0;
float x, y;
float x0, y0;
boolean drawing;
int sgn(float value) { //if signus is needed sgn(x)=x/abs(x) = 1, 0 or -1
if (value < 0) {return -1;}
if (value > 0) {return 1;}
return 0;
}
float cosh(float value1) {
return 0.5*(exp(value1)+exp(-value1));
}
float sinh(float value2) {
return 0.5*(exp(value2)-exp(-value2));
}
float arg(float axis1, float axis2) {
if (axis2==0) {return PI;}
else {return -atan(axis1/axis2)+PI*sgn(axis2)*0.5;}
}
float spharg(float axisa, float axisb, float axisc) {
return acos(axisc/sqrt(sq(axisa)+sq(axisb)+sq(axisc)));
}
void setup() {
// set up drawing area
size(800,850,P3D);
}
void draw() {
plotPlots();
time++;
if (time%1==0) { //every second
findMaxExposure();
renderBrot();
// USE FOR TIFF IMAGE CAPTURE, EVERY 3 SECONDS
// if ( millis() >= timeFlag + 3000 ) {
// timeFlag = millis();
// saveFrame();
//}
if (keyPressed) {
if (key == 'x') {
magx=0.5+magx;
} else if (key == 'X') {
magx=0.5-magx;
}}
}
// stop plotting = s
// start plotting = S
if (keyPressed) {
if (key == 's') {
plots=0;
//reset();
//bailout=0;
} else if (key == 'S') {
plots=171294;
//bailout=2000;
}
}
}
/* void reset(){
// clear();
// Allows for commands outside of keypress loop
}
*/
void plotPlots() {
// iterate through some plots
for (int n=0;n<plots;n++) {
// Choose a random point in same range
x = random(-2.0,2.0);
y = random(-2.0,2.0);
x0 = random(-2.0,2.0);
y0 = random(-2.0,2.0);
if (iterate(x,y,false)) {
iterate(x,y,true);
exposures++;
}
}
}
void renderBrot() {
colorMode(RGB,1.0);
// draw to screen
for (int i=0;i<dimx;i++) {
for (int j=0;j<dimy;j++) {
float ramp = brt * exposure[i*dimy+j] / maxexposure;
// blow out ultra bright regions
if (ramp > 1) {
ramp = 1;
}
//different shading formulae
//color c = color(pow(ramp,0.45454545454545454545),pow(ramp,0.45454545454545454545),pow(ramp,0.45454545454545454545)); //default greyscale
//color c = color(pow(ramp,0.413223140495867768595041322314049586776859504132231404958677685950413223140495867769),pow(ramp,0.45454545454545454545),pow(ramp,0.378787878787878787878787878787878787878787878787878787878787878787878787878787878788)); //purple touch
color c = color(pow(sin(ramp*1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314),0.4),pow(ramp,0.75),pow(ramp,2.5)); // sunset
//color c = color(pow(sin(ramp*1.570796326794896619231321691639751442098584699687552910487472296153908203143104499314),0.45454545455),pow(ramp,17/11.0),pow(ramp,109/55.0));
// "love"
set(j,i,c);
}
}
}
// Iterate the Mandelbrot and return TRUE if the point exits
// Also handle the drawing of the exit points
boolean iterate(float x0, float y0, boolean drawIt) {
float x = 0.0;
float y = 0.0;
//float t = random(0,1);
//float r = random(0,1);
//x0 = 0.0;
//y0 = 0.0;
float xnew, ynew;
int ix,iy;
for (int i=0;i<bailout;i++) {
//set your costum formula here...
//example:
//default Mandelbrot
xnew = x * x - y * y + x0;
ynew = 2 * x * y + y0;
if (drawIt && (i > 3)) {
ix = int(dimx*(panx+xnew+magx/2.0)/magx);
iy = int(dimy*(pany+ynew+magy/2.0)/magy);
if (ix >= 0 && iy >= 0 && ix < dimx && iy < dimy) {
// rotate and expose point
exposure[ix*dimy+iy]++;
}
}
if ((xnew*xnew + ynew*ynew) > 4) {
// escapes
return true;
}
x = xnew;
y = ynew;
}
// does not escape
return false;
}
void findMaxExposure() {
// assume no exposure
maxexposure=0;
// find the largest density value
for (int i=0;i<dimy;i++) {
for (int j=0;j<dimx;j++) {
maxexposure = max(maxexposure,exposure[i*dimx+j]);
}
}
}
Answers
I think the problem is that you're not initializing the boolean and the value is
null
by default. Try initializing it like@theliquu69,
boolean
is 1 of the 8 Java primitive datatypes:https://Docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Therefore, it can't have value
null
. It's eithertrue
orfalse
. Andfalse
is its default value.Could one of you provide an example in how this boolean method would be utilized?
Next code clears the sketch every time you clicked. I hope this helps.
Kf