Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
- Subject: Re: [Math] sin(), cos() not working? What special magic must I use to summon their powers?
- From: Manfred Schwind <email@hidden>
- Date: Tue, 30 Sep 2008 13:47:43 +0200
I think there is more than one problem here. You may want to get a
good book about the C language (Obj-C is just a superset of C).
Variables i, cornerSize are passed to the function from a for loop.
i being the counter for the loop, cornerSize being the size of
corner desired by the user.
float retVal;
retVal = cornerSize*(1-cos(asin(i/cornerSize)));
return (int)retVal;
Obviously, I'm no whiz at ObjC, and the fact that this keeps return
'0' has got me stumped.
Most probably you declared i as an integer value. When dividing with
cornerSize, C does not automatically cast this into a floating point
value, so the result gets rounded down the next integer value (and
maybe always 0 in your case; I don't know).
To solve that, e.g. cast i to a float: (float)i / cornerSize.
When one of both variables is a float, the other will also be
converted to a float and the result is also a float.
I would write the formula above that way:
retVal = cornerSize * (1.0 - cos(asin((float)i / cornerSize)));
sin(90); returns 0 as well. When I use Apple's Calculator and ask
it to tell me the result of sin(90), it gives me 1.
sin takes the argument in radians, not in degrees.
So you have to use e.g. sin(M_PI / 180 * 90) or sin(M_PI / 2) to get
the result of "sinus of 90 degrees".
Regards,
Mani
--
http://mani.de - friendly software
iVolume - listen to music freehand
LittleSecrets - the encrypted notepad
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden