0

Method toVertical, which accepts two parameters parameters in the following order:

(a) An image of type BufferedImage

(b) A proportional y-coordinate of type double

It returns as an int the pixel coordinate in the image that corresponds to the proportional coordinate. The proportion is inversely applied to the image’s height, with proportion 0 mapping to the bottommost row of pixels (whose index is one less than the image’s height), and proportion 1 mapping to the top row of pixels, whose index is 0. This inverse mapping has the effect of situating the image’s origin in proportional space to the bottom left of the image.

For example, if an image has width 30, the following values should be produced for various proportions:

• toVertical(image30, 0.0) → 29

• toVertical(image30, 1.0) → 0

• toVertical(image30, 0.75) → 7

• toVertical(image30, 0.77) → 7

I am confused as to how I should approach this problem. Here is the code I have so far.

public static int toVertical(BufferedImage image, double ycoor) {
    int height = image.getHeight(); 
    double prop = height * (1/ycoor);
    int prop1 = (int) ()
    return prop1 -+ 1;

1 Answers1

0

If you modify your function like this you should get your desired output as you described:

public static int toVertical(BufferedImage image, double ycoor) {
    int height = image.getHeight(); 
    double prop = (height - 1) - (height - 1)*ycoor;
    int prop1 = (int)prop;
    return prop1;

(height - 1) will give you the max y index of your BufferedImage

T A
  • 1,677
  • 4
  • 21
  • 29
  • You may have to adjust the roundings as toVertical(image30, 0.77) will yield the result 6 instead of 7 – T A Nov 06 '17 at 09:18