We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › differences
Page Index Toggle Pages: 1
differences (Read 835 times)
differences
Jun 6th, 2005, 6:42pm
 
i have a currentX and prevX

currentX wraps to 0 (as of course does prevX)

subtracting one from the other gives me a positive or negative difference.
My problem is when 1 of the numbers is <max and the other has just wrapped at %max. The difference is then thrown. by the overrun.

Is there a good way of maintaining the proportional difference even through the the % wrap.

Thanks.
Re: differences
Reply #1 - Jun 9th, 2005, 9:02am
 
Try letting the currentX and prevX values stack up as high as need be, then use a normalization function (like the one I wrote below) to put the value inside the desired range when you need to use it:
Code:
// Recursive normalization function
// Moves supplied value inside min/max constraints
int normalize(int n, int min, int max) {
int diff = max - min;
if(n < min) {
return normalize(n+diff, min, max);
} else
if(n > max) {
return normalize(n-diff, min, max);
} else return n;
}

Also, if you take the difference of two values and feed it through the above normalization function you will get the same difference whatever the offset.

In this example the normalized difference between x and y is always 66 because the actual difference is always the same (1134): Code:
int x1 = -512; int y1 = 622;
int x2 = -24; int y2 = 1110;
int x3 = 0; int y3 = 1134;
int min = 0;
int max = 100;

println(normalize(x1-y1, min, max));
println(normalize(x2-y2, min, max));
println(normalize(x3-y3, min, max));
Re: differences
Reply #2 - Jun 10th, 2005, 11:59am
 

Magic!

Thanks alot.
Page Index Toggle Pages: 1