(float)?

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();



  }


  }



}
Tagged:

Answers

  • edited October 2015 Answer ✓
    • Placing a datatype between () is called type casting.
    • It attempts to coerce an expression to become the specified casting type.
    • For the (float)'s case, Processing also provides an alt. as the converter function float():
      https://Processing.org/reference/floatconvert_.html
    • The reason (float) casting was used @ mouseX / (float) width expression was b/c both mouseX & width are integral datatypes.
    • The result from a division operation / where both operands are of whole datatypes always evals as a whole datatype too. In Java & C/C++ at least. But not in JS!
    • When we need to avoid that, we've gotta force at least 1 of the operands to become a fractional datatype lest the division result lose its decimal part.
    • I particular think placing the (float) before mouseX instead woulda been more appealing: :P
      float z = (float) mouseX/width * brightness(img.pixels[loc]) - 10;
Sign In or Register to comment.