Resize PImage: "width or height must be > 0 for resize"

I want to scale myImage to fit in the height of the screen
FIRST: divide the width and height of the screen by the width and height of myImage.
SECOND: multiply the width and height of myImage by one of the two previous results, according to whether we want it to fit the width or height of the screen (in this case, the height).
The problem is that there are no real-floats as you can see in the console´s first line
The code:

PImage myImage;

void setup(){
  float myOperation= 600/1024;
  println("Why 600/1024 = "+myOperation+"???");
  PImage myImage = createImage(512, 1024, RGB);
  for (int i = 0; i < myImage.pixels.length; i++) {
    myImage.pixels[i] = color(0, 90, 102); 
  int myImageWidth= myImage.width;
  int myImageHeight= myImage.height;
  println("myImageSize= "+myImageWidth+" X "+myImageHeight);
  // scale(0.5);  // just to check
  // image(myImage, 17, 17);  //it´s OK
  println("screenSize = "+width+" X "+height);
  float widthRatio= width /myImageWidth;
  float sameWidthRatio= 600/512;
  println("widthRatio = "+widthRatio+"; same Operation: 600/512 = "+sameWidthRatio);
  float heightRatio= height /myImageHeight;
  float sameHeightRatio= 600/1024;
  println("heightRatio = "+heightRatio+"; same Operation: 600/1024 = "+sameHeightRatio);
  int resizeWidth=int(myImageWidth*heightRatio);
  int resizeHeight=int(myImageHeight*heightRatio);
  myImage.resize(resizeWidth,resizeHeight);  // width or height must be > 0 for resize

Am I doing something wrong? I tried this:

void setup(){
  float a=1024;
  float b= 600;
  float c=b/a;

And c is 0,586… of course…
There is something I don’t understand but I don’t know what it is
How can i resize the Pimage? Thanks!!

1 Like

These are performing integer division so the decimal part is truncated, hence sameWidthRatio = 1 not 1.171875

The solution is to convert one or both of the values to a float before doing the division e.g.

float widthRatio= ((float) width) / myImageWidth;
float sameWidthRatio= 600.0/512;


Thank you quark!! it works