Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 01-16-2011, 07:37 PM   #1 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default [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.
iSDK is offline   Reply With Quote
Old 01-18-2011, 04:50 PM   #2 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

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.
iisword is offline   Reply With Quote
Old 01-18-2011, 05:07 PM   #3 (permalink)
Indie Developer
 
iSDK's Avatar
 
Join Date: Jul 2010
Posts: 1,346
iSDK is on a distinguished road
Send a message via AIM to iSDK
Default

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 View Post
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;
  }
 }
}
iSDK is offline   Reply With Quote
Old 01-18-2011, 05:25 PM   #4 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

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.
__________________
iisword is offline   Reply With Quote
Old 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
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by iisword View Post
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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 01-19-2011, 02:15 PM   #6 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

and why not kal
__________________
iisword is offline   Reply With Quote
Old 01-19-2011, 02:30 PM   #7 (permalink)
Knows SQL
 
iisword's Avatar
 
Join Date: Oct 2009
Location: Somewhere the streets are on fire, the sewers are flooded, and the cats are high on catnip
Posts: 529
iisword is on a distinguished road
Default

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.
iisword is offline   Reply With Quote
Old 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
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by iisword View Post
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.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0