We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm doing Daniel Shiffman's tutorial on image manipulation and I came across (float). I'm pretty sure this is quite basic, but this is the first time I see it. What does it do? I mean, I know without it, the code won't work, but I thought data types were only used to create variables. Anyone? Thanks.
PImage img;
int cellsize=8;
int cols,rows;
void setup() {
size(200,200,P3D);
img=loadImage("IMG_6753.jpg");
surface.setResizable(true);
surface.setSize(img.width,img.height);
cols=width/cellsize;
rows=height/cellsize;
}
void draw() {
background(0);
loadPixels();
for(int i=0; i<cols; i++) {
for(int j=0; j<rows; j++) {
int x=i*cellsize+cellsize/2;
int y=j*cellsize+cellsize/2;
int loc=x+y*width;
color c=img.pixels[loc];
float z=(mouseX/(float)width)*brightness(img.pixels[loc])-10;
pushMatrix();
translate(x,y,z);
fill(c);
noStroke();
rectMode(CENTER);
rect(0,0,cellsize,cellsize);
popMatrix();
}
}
}
Answers
()
is called type casting.(float)
's case, Processing also provides an alt. as the converter function float():https://Processing.org/reference/floatconvert_.html
(float)
casting was used @mouseX / (float) width
expression was b/c both mouseX & width are integral datatypes./
where both operands are of whole datatypes always evals as a whole datatype too. In Java & C/C++ at least. But not in JS!(float)
before mouseX instead woulda been more appealing: :Pfloat z = (float) mouseX/width * brightness(img.pixels[loc]) - 10;