01-16-2011, 07:37 PM
#1 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
[CODE SNIPPET]Pythagorus'es Theorem
Hey,
I was doing some maths homework before on Pythagorus'es theorem, and I decided to make a method which will return the hypotenuse. Unfortunately so far, I have only implemented the ability to work out the unknown side opposite the right angle. Anyways here is the code to do so:
Please PM me if you are going to use this code in your application.
Code:
- (float)getXFromSide1:(int)one andSide2:(int)two {
int a = (one*one) + (two*two);
float sqr = sqrt(a);
return sqr;
}
You can call it by:
Code:
int side1 = 10;
int side2 = 1762
float hypotenuse = [self getXFromSide1:side1 andSide2:side2];
NSLog(@"%.2f", hypotenuse);
Please PM me if you are going to use this code in your application. Enjoy..
Last edited by iSDK; 01-17-2011 at 12:09 PM .
01-18-2011, 04:50 PM
#2 (permalink )
Knows SQL
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
Try this(only works with right triangles):
Code:
-(float)getTriangleSide:(float)sideOne withSide:(float)sideTwo findingHypotenuse:(BOOL)hypo
{
float answer;
if(hypo)
{
answer = (float)sqrt(pow(sideOne, 2) + pow(sideTwo,2));
return answer;
}
else
{
if(sideOne > sideTwo)
{
answer = (float)sqrt(pow(sideOne,2) - pow(sideTwo, 2));
return answer;
}
else if (sideOne < sideTwo)
{
answer = (float)sqrt(pow(sideTwo, 2) - pow(sideOne, 2));
return answer;
}
else if (sideOne == sideTwo)
{
return sideOne; //the user has put in an equilateral triangle
}
}
}
__________________
Last edited by iisword; 01-19-2011 at 02:35 PM .
01-18-2011, 05:07 PM
#3 (permalink )
Indie Developer
Join Date: Jul 2010
Posts: 1,346
Yeah I know that was a flaw with it. Thanks so much for the contribution, would you like me to put it in the OP? (with credits of course)
Quote:
Originally Posted by
iisword
Try this(only works with right triangles):
Code:
-(float)getTriangleSide:(float)sideOne withSide:(float)sideTwo findingHypotenuse:(BOOL)hypo
{
float answer;
if(hypo)
{
answer = sqrt(sideOne^2 + sideTwo^2);
return answer;
}
else
{
if(sideOne >= sideTwo)
{
answer = sqrt(sideOne^2 - sideTwo^2);
return answer;
}
else
{
answer = sqrt(sideTwo^2 - sideOne^2);
return answer;
}
}
}
01-18-2011, 05:25 PM
#4 (permalink )
Knows SQL
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
Made a correction. sideOne cannot equal sideTwo if not solving for hypotenuse because it will result in sqrt(0) which is an equilateral triangle error.
__________________
01-18-2011, 11:45 PM
#5 (permalink )
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Quote:
Originally Posted by
iisword
Try this(only works with right triangles):
Code:
-(float)getTriangleSide:(float)sideOne withSide:(float)sideTwo findingHypotenuse:(BOOL)hypo
{
float answer;
if(hypo)
{
answer = sqrt(sideOne^2 + sideTwo^2);
return answer;
}
else
{
if(sideOne > sideTwo)
{
answer = sqrt(sideOne^2 - sideTwo^2);
return answer;
}
else if (sideOne < sideTwo)
{
answer = sqrt(sideTwo^2 - sideOne^2);
return answer;
}
else if (sideOne == sideTwo)
{
return sideOne; //the user has put in an equilateral triangle
}
}
}
FWIW, this code doesn't do what you think it's doing.
01-19-2011, 02:15 PM
#6 (permalink )
Knows SQL
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
and why not kal
__________________
01-19-2011, 02:30 PM
#7 (permalink )
Knows SQL
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
also it works math wise, ran it, other than a build error (fixed below) the code is correct:
Code:
-(float)getTriangleSide:(float)sideOne withSide:(float)sideTwo findingHypotenuse:(BOOL)hypo
{
float answer;
if(hypo)
{
answer = (float)sqrt(pow(sideOne, 2) + pow(sideTwo,2));
return answer;
}
else
{
if(sideOne > sideTwo)
{
answer = (float)sqrt(pow(sideOne,2) - pow(sideTwo, 2));
return answer;
}
else if (sideOne < sideTwo)
{
answer = (float)sqrt(pow(sideTwo, 2) - pow(sideOne, 2));
return answer;
}
else if (sideOne == sideTwo)
{
return sideOne; //the user has put in an equilateral triangle
}
}
}
it won't solve any other triangle other than a right.
__________________
Last edited by iisword; 01-19-2011 at 02:32 PM .
01-19-2011, 02:52 PM
#8 (permalink )
Pro. Game Developer
iPhone Dev SDK Supporter
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Quote:
Originally Posted by
iisword
and why not kal
As you probably discovered, "variable^2" doesn't square the value in "variable". It's a perfectly legal C operation, but it's not doing what you think/thought.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 445
21 members and 424 guests
cgokey , dcool , Dj_kades , givensur , iekei , IOOIOIO , IphoneSdk , ipodphone , jeroenkeij , john love , karatebasker , mediaspree , patapple , peterwilli , pipposanta , Punkjumper , QuantumDoja , SLIC , Sloshmonster , sojourner , usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc