|
-
January 3rd, 2005, 12:32 PM
#1
Maybe more math than C++, but....
I'm sure some of you have used a sin or cos function to "loop" an array index back and forth through a set area. Whats troubling me is how to decide when the total amount moved equals one times something. I'll try and explain a little better....
if I have:
Code:
int movement = 0;
movement+= (int)(sin(j++ * PI / 180) * width);
....then as j increases from 0 "movement" will approach and then pass the value "width", but at which point will it match (or almost match) "width". i.e. if width = 1, at what value j will movement equal 1.
I hope that makes sense.
I could check the value continuosly using an "if" suppose - but I'd rather check values passed in are ok before the function starts than have it break half way through.
I've gotten round this before but looking back at the code I used I can't even figure out what I was doing now, no comments, slightly different situation.....and it was probably dodgy solution anyway.
Cheers for any ideas.
-
January 4th, 2005, 02:34 AM
#2
Re: Maybe more math than C++, but....
I dont find any "Question" in your post!
-
January 4th, 2005, 02:58 AM
#3
Re: Maybe more math than C++, but....
 Originally Posted by Ajay Vijay
I dont find any "Question" in your post! 
Well...
...it does not always have to be a question mark to post here...
-
January 4th, 2005, 03:01 AM
#4
Re: Maybe more math than C++, but....
 Originally Posted by Andreas Masur
Well...
...it does not always have to be a question mark to post here... 
I did not look for '?' anyway , But I do not find any question there...!
Have you?
-
January 4th, 2005, 03:05 AM
#5
Re: Maybe more math than C++, but....
let me check first if i got you right; at what value of j will the value of movement be 1? If i got your right, i wrote a piece of code to see the answer. i am not that good in math so i have to get the answer using a piece of code.
Code:
int main() {
int j=0;
int width=2;
int movement=0;
while ( movement <= 1) {
movement += (int)(sin((j++ * 3.14) / 180) * width);
cout << "value of j: " << j << endl;
cout << "value of movement: " << movement << endl;
}
getch();
return 0;
}
note that i changed value of width from 1 to 2. Why? Because when u use 1 for width, value of movement will remain at 0 - infinite loop.
*** instead of 1 i used 2
Last edited by jbp2004; January 4th, 2005 at 03:11 AM.
I am inviting you to join GreenZap. It is just like PAYPAL but it is more rewarding! You get $25 when you join (FREE to sign up) AND you get rewarded also when someone opened an account using your GreenZap promo code! GreenZap is launching June 1st.
Click here and PREREGISTER
Have a break. Visit JonelsPlace
-
January 4th, 2005, 03:09 AM
#6
Re: Maybe more math than C++, but....
 Originally Posted by ggmn
...but at which point will it match (or almost match) "width". i.e. if width = 1, at what value j will movement equal 1.
The value of sin(x) is maximum (i.e., equals 1) when x=pi/2
So, sin(j*pi/180)=1 when j*pi/180=pi/2, or more simply when j=90
HTH,
Mike
-
January 4th, 2005, 05:43 AM
#7
Re: Maybe more math than C++, but....
Let me see now...
If I understand you correctly you want to find out at what point movement becomes 1.
If what you've written is really what you want then I think you have to use the solution your figured out yourself. There may be some tricks involved with the fact that sin is probably one of the most well-known functions in the world but I can't see it right now.
Anyway, as you probably you know what you have written is sort of a numerical integration of the function: sin(x * pi / 180) * width
If we do an analytical integration of your base function:
sin(x * pi / 180) * width
Then we get:
-cos(x * pi / 180) * width * 180 / pi + C
This is the analytical expression of movement in your code:
Since movement = 0 for x = 0 it means that : C = width * 180 / pi
You wanted to find out when movement = 1. Now it's rather easy.
1 = ( - cos(x * pi / 180) + 1) * width * 180 / pi =>
x = arccos(-pi / ( 180*width ) + 1) * 180 / pi
Note that x won't match your calculated j since the integral is an exact result of the "area" under sin(x) function. What you do with your movement function is that approximate the "area" in the following the way.
1. Your approximation rectangles in the integration have the "width" = 1. A bit coarse.
2. You cast the double returned by the sin function to an integer which means that you loses all decimals. This is also the reason you get an infinite loop.
I have taken the liberty of modifying your program a bit to better match the analytical solution:
Code:
#include <cstddef>
#include <cstdlib>
#include <cmath>
#include <iostream>
int main(int, char*)
{
double const l_rectangle_width(1 / 10000.0);
double const l_multiplier(3.141592654 / 180.0);
double const l_width(10);
double l_sum(0);
double l_x(0);
// Simple numeric integration of std::sin(l_x * l_multiplier) * l_width
while( l_sum < 1.0 )
{
l_sum += l_rectangle_width * std::sin(l_x * l_multiplier) * l_width;
l_x += l_rectangle_width;
}
// Integral of std::sin(l_x * l_multiplier) * l_width is
// (1 - std::cos(l_x * l_multiplier)) * l_width / l_multiplier
double const l_calculated_x(
std::acos(
(-l_multiplier / l_width) + 1) / l_multiplier);
// compare l_x with l_calculated_x
std::cout << l_x << std::endl;
std::cout << l_calculated_x << std::endl;
return EXIT_SUCCESS;
}
1. l_rectangle_width allows you to make the resolution finer
2. l_multiplier corresponds to 3.14 / 180 in your example
3. l_width corresponds to width in your example
4. l_sum corresponds to movement.
The while loop iterate until l_sum reaches 1.0. The code then prints l_x. It then calculates what l_x should be using arrcos (l_calculated_x).
This may or may not work for you. If you want it to behave the way you specified with the casting of doubles to integers (losing decimals and all that) and fixed approximation rectangles width = 1 then at least I can't come up with a "fast" solution right now.
-
January 4th, 2005, 05:54 AM
#8
Re: Maybe more math than C++, but....
What are you trying to do? Why do you mix radians (PI) with degrees (180) in the same function? Or I am wrong about the interpretation of those numbers...
-
January 4th, 2005, 06:34 AM
#9
Re: Maybe more math than C++, but....
The other point is that if you are writing a program, you cannot test for equality with floating point calculations. The result will almost never be exactly 1. You have to get the absolute value of the (number - 1) and see if it is some very small value.
If you don't do this, your program may go into an infinite loop.
Regards,
Paul McKenzie
-
January 4th, 2005, 06:41 AM
#10
Re: Maybe more math than C++, but....
To complete what Paul said see these:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|