Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Require formula assistancee

Author
Message
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Dec 2011 06:00 Edited at: 22nd Dec 2011 06:03
Require formula assistance;

abs(50*sin((0.25/4)*X))

Here is my equation plugged into a Graphical Function Explorer;



http://www.mathopenref.com/graphfunctions.html

I double checked it using the following site;

http://www.wolframalpha.com/

And yes, I'm having a geek-gasm with these graphing tools, as it lets me skip mathematical theory and jump straight into trial & error! (not necessarily a good thing , but I like to think of it as the mathematical equivalent of spell check, and we all use that now don't we. )

But, I'm missing something when it comes to plotting my formula into DBPro using a FOR loop, as per my code below.



My Y value is clearly not functioning as per the graph calculators that I created my formula in... I'm forgetting to do something really simple here! What is it?

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Dec 2011 06:46
The more I play with this and discuss it with others online, the more I think I've found a bug?

If you swap out;

Yfloat# = abs(50*sin((0.25/4)*X))

with

Yfloat# = abs(50*sin((0.25/4)*X*10))

where we add a "*10" to X, it works! But this is wrong, no where else where I've tested this requires the multiplication of X by 10...

Gah, I know I've missed something here.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
mr_d
DBPro Tool Maker
17
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 22nd Dec 2011 07:12
hey there,
ithink it's just a case of the 0.25/4 part messing things up (or actually making the values very small).
if you take the normal sin function where sin(0)=0 and sin(90)=1, then we know that to reach the peak of 50 (as shown in your graph) that we have to multiply the sin(90) by 50.
your example above is just making the value large enough to see properly.
please see the following:


Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 22nd Dec 2011 07:45
Haven't tested the code, but suspect the difference might be due to Dbpro wanting Cos()/Sin() parameters in degrees, rather than radians..

Neuro Fuzzy
16
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 22nd Dec 2011 09:19
I do a lot of weird graphing stuff like this and there are a lot of issues with the way you're trying to do it.

Firstly, I like to have a bit of a UI, instead of setting the sync rate really low, so I changed your code to this:

Just so that you can have user interactivity. Also the print command messes up stuff sometimes so I replaced it with text.

Secondly, as Kevin Picone pointed out, the trig functions used in scientific and mathematical calculations are in radians, not degrees. That means you have to multiply your radian value by 180/pi to get the result in degrees. So now your equation would look like this:
Yfloat# = abs(50*sin((0.25/4)*X*57.2957795))

If we remove the call to "cls" and use the dot function instead of the circle function, we get this:

The shape is exactly what we want.

The downside of this is that one pixel corresponds to exactly one unit, the y axis is flipped, and we can't translate by half a unit. All of these things can be corrected using adding, subtracting, multiplication and division. To achieve these transformations, you can either take the summarized route (affine transformations) or just think about it until you get it right.

In my graphing programs, I like to have a set of four functions: xPixelToUnit(x as float), yPixelToUnit(y as float), xUnitToPixel(x as float), and yUnitToPixel(y as float). Using this you can get mouse input on the graph and draw anything.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Dec 2011 09:43 Edited at: 22nd Dec 2011 11:10
Gah, beaten! On the drive home, I started thinking that my problem was Radians/Degrees! I'll have a play with it further tonight after dinner.

OK, it seems to be a simple radian road block, I had to multiply things by 57.295779513 to get the right answer. Will read up on how DBPro deals with Radians and Degrees tonight. Tidied my code a little too.




EDIT: Note how punching this DBPro modified formula into those graph calculators now gives screwy results. Must be an easier more formal way around this!

EDIT: Updated my code, once again, IanM saves the day! Degtorad() & Radtodeg()

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 22nd Dec 2011 16:42
One thing that I always try when I can't get expected results, is to make sure that every number has a .0 if it's a float - because DBPro tries to make it's own mind up about whether your calculating an integer or a float, based on the numbers. Your mixing up integers and floats, but to eliminate that, I suggest just adding in those .0's, to be on the safe side...

So if you have this: Yfloat# = abs(50*sin(RADTODEG(XFloat#)/5/pi()))

Try: Yfloat# = abs(50.0*sin(RADTODEG(XFloat#)/5.0/pi()))

Worth a shot.

Health, Ammo, and bacon and eggs!
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Dec 2011 22:16 Edited at: 22nd Dec 2011 22:16
Now I have to learn how to combine formulas, so that I can produce a result like a bouncing ball.



Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Jan 2012 19:22
Does this help?

Bounce - Returns a value between 0 and 1 that can be used to easily make bouncing GUI items (a la OS X's Dock)



Login to post a reply

Server time is: 2024-03-29 07:34:26
Your offset time is: 2024-03-29 07:34:26