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: Michael Robinson <email@hidden>
- Date: Wed, 1 Oct 2008 00:59:23 +1300
Thanks for your incredibly swift replies!
changing "float retVal" to "double retVal", using "return
lround(retVal);" and casting ints as doubles in the formula gave me
some real values, which is wonderful!
I could have saved myself two hours by asking the list when I first
ran into this problem!
I've got it spitting out some (jagged but acceptable) corners now,
which is wonderful.
In case any of you were interested, I'm doing this to create an HTML
"object" that can be placed over any background, be given rounded
corners and have the background show through. It was being
accomplished with Javascript, but I figured it'd be better if this was
done one time, when the HTML is generated, instead of at client side.
Thanks for your help, no way I could have figured this out.
Mike
The best argument against democracy is a five-minute conversation with
the average voter.
-- Sir Winston Churchill
On 1/10/2008, at 12:47 AM, Manfred Schwind wrote:
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