10 Replies Latest reply on Dec 5, 2009 3:22 AM by BKBK

# Find a ratio.... hum?

I'm writing a neat little image editing tool for image uploadds, but I need to constrain the images to a 4:3 ratio....  I can get the images, dimeansions all that etc no problems, but, with CFMX, how do I determine the ratio between 2 numbers??  looping trough division statements looking for integers and then comapring to see where the lowest is found seems REALLY REALLY messy...

is there a better way?

-thanks

-sean

• ###### 1. Re: Find a ratio.... hum?

Your question is not clear.  What division statements do you have to loop through?  Why do you have to look for integers?

• ###### 2. Re: Find a ratio.... hum?

to find a ratio you need to find the highest common factor [?] that both dimensions [height & width] are divisible by i.e. 640x480 highest common factor  is 160 divide them both is 4:3 ....  [I hope I am using the math terms right]

-sean

• ###### 3. Re: Find a ratio.... hum?

How are you going to use this number once you have it?

• ###### 4. Re: Find a ratio.... hum?

basically all I need to do is determine if the uploaded image dimensions are in a 4:3 ratio, [640x480, 800x600, etc.] so I can resize it without distortion or reject the image.

-sean

UPDATE:

hmmm, if I divide the first dimension [width] by 4 then multiply the result by 3 that result should equal the provided image's height to equal a 4:3 ratio.

-sean

• ###### 5. Re: Find a ratio.... hum?

if (height / width == 3 / 4)

accept;

else

reject;

• ###### 6. Re: Find a ratio.... hum?

@Sean69

if I divide the first dimension [width] by 4 then multiply the result by 3 that result should equal the provided image's height to equal a 4:3 ratio.

@Jochemd

if (height / width == 3 / 4)
accept;
else
reject;

Not quite. When you divide by integers, you're into floating-point arithmetic.

Floating-point arithmetic is, in general, not exact. To avoid truncation errors, keep the logic within the set of integers, something like

if (4*height EQ 3*width)
...
else
...

• ###### 7. Re: Find a ratio.... hum?

I think jochemd's answer was closest to the mark I am looking for here....

height/width = 0.75 and I want to give a litlle bit of leeway here say up to 1% in any dimension so....

```ratio = (height / width);

if ( (ratio lte 0.76) and (ratio gte 0.74)) {
do;
}else{
or do not;
//there is no try.
}
```

that gives me approximately 10 pixel leeway if someone were to upload a 1024x768 image.[i.e. they could upload a 1034x770 image and it would still pass muster.

-thanks guys

-sean

• ###### 8. Re: Find a ratio.... hum?

That's actually what I meant by "Floating-point arithmetic is, in general, not exact". Your earlier attempt and indeed Jochemd's didn't take account of that.

Now to something else. A deviation of 0.76 or 0.74 from 3/4 isn't 1%. It is 0.01 of 0.75, which is approximately 1.33%.

If you wish to have a 1% tolerance from 3/4, then you have to do something like

ratio = height/width;

if (abs(4*ratio/3 -1) LT 0.01) {
do;
}else{
or do not;
//there is no try.
}

• ###### 9. Re: Find a ratio.... hum?

Like I said "about" - "up to" not exactly 1% cause users are not going to get it perfect every time

though, not sure about your 1.33% comment, by dividing height/width, we are finding what % height is of width,  so by saying "is it 75%" then giving it a leeway of "1" either way, is exactly 1%..... unless I am missing somehting.

either way - no big deal, it's supposed to be somewhat fuzzy.

-sean

• ###### 10. Re: Find a ratio.... hum?
@Sean69
Like I said "about" - "up to"

No misunderstanding there. That's what I meant, too.

are finding what % height is of width,  so by saying "is it 75%" then
giving it a leeway of "1" either way, is exactly 1%..... unless I am
missing somehting.

What you're measuring isn't height or width, but ratio. As the variable under consideration is the aspect ratio 3:4, a leeway of 1 either way is therefore 1 out of 75. That is around 1.33%.

When you say 1%, it implies 1% of 0.75, which is 0.0075. In other words, the condition for a value of the aspect ratio to be within 1% of 0.75 is: abs(ratio - 0.75)/0.75 <= 0.01.

Aspect Ratio is a dimensionless quantity. That is, the unit of length(inches, metres, centimetres) in the height will cancel out that in the width. This imples that a deviation from the ratio remains the same as you scale up, say, from 800x600 to 1024x768. Therefore, your solution, abs(ratio - 0.75) <= 0.01, is as good as any.

either way - no big deal

I agree. Main thing is, your problem has been solved.