How to resolve a and b when one knows the sum and the dividend

How to resolve a and b when one knows the sum and the dividend

am 26.09.2007 14:58:07 von Mark Knoop

Hi

I know this is more maths than programming but as there are some very smart
people on this list...

Say we know that

a + b = 60

and

a/b = 0.5

then of course

a = 20, b = 40

which you can see by common sense or by plotting two lines on a graph.

But how to do this programmatically so that it works for any input values
(without attempting some way of running through possible values and
testing)?

use strict;
use warnings;
my $a_plus_b = 60;
my $a_divided_by_b = 0.5;
my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
print "a = $a , b = $b\n";

sub resolve_a_b {
my $a_plus_b = shift;
my $a_divided_by_b = shift;
my ($a,$b);
### what goes here?
return ($a, $b);
}

To put this in context this is what I am actually trying to do...

Say I have an image which is 150 pixels wide by 100 pixels high.
So I know width/height = 1.5
I want to crop the image to get to a target image with a ratio of 0.9
Because 0.9 is less than 1.5 I know I need to crop the left and/or right
edges (or pad the top and bottom edges but for this example I am only
interested in cropping).
The maximum I am allowed to crop is by 25 pixels on the left, and 50 pixels
on the right (this would give me an image 75 pixels wide by 100 pixels high
which is a ratio of 0.75 - smaller than my target so I know it is possible
to get there by cropping).
My train of thought is - I know the factor I need to multiply 150 by to get
to the new width is 0.6 which is target ratio / original ratio which gives
me a new width of 90.
150 - 90 is 60 which is the amount of pixels I need to remove.
I want to remove pixels in proportion to the maximum cropping allowed at
each edge.
So max cropping on left = 25, max cropping on right = 50.
If we call the amount to crop on left a and the amount to crop on the right
b then:
a / b must equal (max cropping on left / max cropping on right) which in
this instance = 15/50 = 0.5
The total amount of pixels to remove is a + b which in this instance = 60
Although it is quite clear that in this instance a = 20 and b = 40 I can't
see how to create a function which gives the correct result for any input
values.

I suspect I've made this far more complicated than I need to... any
pointers?

Cheers
Mark


_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: How to resolve a and b when one knows the sum and the dividend

am 26.09.2007 15:51:08 von Mark Knoop

> Say we know that
>
> a + b = 60
>
> and
>
> a/b = 0.5
>
> then of course
>
> a = 20, b = 40
>
> which you can see by common sense or by plotting two lines on a graph.
>
> But how to do this programmatically so that it works for any input values
> (without attempting some way of running through possible values and
> testing)?
>
> use strict;
> use warnings;
> my $a_plus_b = 60;
> my $a_divided_by_b = 0.5;
> my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
> print "a = $a , b = $b\n";
>
> sub resolve_a_b {
> my $a_plus_b = shift;
> my $a_divided_by_b = shift;
> my ($a,$b);
> ### what goes here?
> return ($a, $b);
> }
>

>From the response I got from one list member I have ended up with:

use strict;
use warnings;
my $a_plus_b = 60;
my $a_divided_by_b = 1.5;
my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
print "a = $a , b = $b\n";

sub resolve_a_b {
my $a_plus_b = shift;
my $a_divided_by_b = shift;
my ($a,$b);
$b = $a_plus_b/($a_divided_by_b + 1);
$a = $a_plus_b - $b;
return ($a, $b);
}

which works for any input values. Whether or not this solves my cropping
problem or not is another matter!

Thanks
Mark



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: How to resolve a and b when one knows the sum and the dividend

am 26.09.2007 18:00:28 von fwashbur

Mark,

You have 2 equations, so you can solve them to the same variable:
a+b = $a_plus_b
a/b = $a_divided_by_b

so a = b * $a_divided_by_b
and b * $a_divided_by_b + b = $a_plus_b
and b * ($a_divided_by_b + 1) = $a_plus_b
and b = $a_plus_b/($a_divided_by_b + 1)
which you can solve given your input values, then plug the calculated b
value back into a = b * $a_divided_by_b to get a.

Rick

-----Original Message-----
From: activeperl-bounces@listserv.ActiveState.com
[mailto:activeperl-bounces@listserv.ActiveState.com] On Behalf Of Mark
Knoop
Sent: Wednesday, September 26, 2007 6:51 AM
To: activeperl@listserv.ActiveState.com
Subject: Re: How to resolve a and b when one knows the sum and the
dividend

> Say we know that
>
> a + b = $a_plus_b
>
> and
>
> a/b = 0.5
>
> then of course
>
> a = 20, b = 40
>
> which you can see by common sense or by plotting two lines on a graph.
>
> But how to do this programmatically so that it works for any input
values
> (without attempting some way of running through possible values and
> testing)?
>
> use strict;
> use warnings;
> my $a_plus_b = 60;
> my $a_divided_by_b = 0.5;
> my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
> print "a = $a , b = $b\n";
>
> sub resolve_a_b {
> my $a_plus_b = shift;
> my $a_divided_by_b = shift;
> my ($a,$b);
> ### what goes here?
> return ($a, $b);
> }
>

>From the response I got from one list member I have ended up with:

use strict;
use warnings;
my $a_plus_b = 60;
my $a_divided_by_b = 1.5;
my ($a, $b) = resolve_a_b($a_plus_b, $a_divided_by_b);
print "a = $a , b = $b\n";

sub resolve_a_b {
my $a_plus_b = shift;
my $a_divided_by_b = shift;
my ($a,$b);
$b = $a_plus_b/($a_divided_by_b + 1);
$a = $a_plus_b - $b;
return ($a, $b);
}

which works for any input values. Whether or not this solves my cropping
problem or not is another matter!

Thanks
Mark



_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs