C#
  Home arrow C# arrow Page 12 - Numbers
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Numbers
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 19
    2004-06-29

    Table of Contents:
  • Numbers
  • Determining Approximate Equality Between a Fraction and Floating-Point Value
  • Converting Degrees to Radians and Converting Radians to Degrees
  • Using the Bitwise Complement Operator with Various Data Types
  • Test for an Even or Odd Value
  • Converting a Number in Another Base to Base10 and Determining Whether a String Is a Valid Number
  • Determining Whether a String Is a Valid Number
  • Rounding a Floating-Point Value and Different Rounding Algorithms Problem
  • Converting Celsius to Fahrenheit and
  • Safely Performing a Narrowing Numeric Cast
  • Discussion
  • Finding the Length of Any Three Sides of a Right Triangle and Finding the Angles of a Right Triangle Problem

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Numbers - Finding the Length of Any Three Sides of a Right Triangle and Finding the Angles of a Right Triangle Problem


    (Page 12 of 12 )

     
    1.14 Finding the Length of Any Three Sides of a Right Triangle
     
    Problem

    You need to calculate the length of one side of a triangle when either the lengths of two sides are known or one angle and the length of a side are known.

    Solution

    Use the Math.Sin, Math.Cos, and Math.Tan methods of the Math class to find the length of one side. The equations for these methods are as follows:

    double theta = 40;
    double hypotenuse = 5;
    double oppositeSide;
    double adjacentSide;

    oppositeSide = Math.Sin(theta) * hypotenuse;
    oppositeSide = Math.Tan(theta) * adjacentSide;
    adjacentSide = Math.Cos(theta) * hypotenuse;
    adjacentSide = oppositeSide / Math.Tan(theta);
    hypotenuse = oppositeSide / Math.Sin(theta);
    hypotenuse = adjacentSide / Math.Cos(theta);

    where theta is the known angle, and the oppositeSide variable is equal to the length of the side opposite to the angle theta, and the adjacentSide variable is equal to the length of the side adjacent to the angle theta. The hypotenuse variable is equal to the length of the hypotenuse of the triangle. See Figure 1-1.

    In addition to these three static methods, the length of the hypotenuse of a right triangle can be calculated using the Pythagorean theorem. This theorem states that the hypotenuse of a right triangle is equal to the square root of the sum of the squares of the other two sides. This equation can be realized through the use of the Math.Pow and Math.Sqrt static methods of the Math class, as follows:

    double hypotenuse = Math.Sqrt(Math.Pow(xSide, 2) + Math.Pow(ySide, 2))

    where xSide and ySide are the lengths of the two sides that are not the hypotenuse of the triangle.

    CSharpCookbook

    Adjacent

    Figure 1-1. A right triangle

    Discussion

    Finding the length of a side of a right triangle is easy when an angle and the length of one of the sides are known. Using the trigonometric functions sine, cosine, and tangent, we can derive the lengths of either of the two unknown sides. The equations for sine, cosine, and tangent are defined here:

    sin(Theta) = oppositeSide / hypotenuseSide
    cos(Theta) = adjacentSide / hypotenuseSide
    tan(Theta) = oppositeSide / adjacentSide

    where theta is the value of the known angle. Rearranging these equations allows us to derive the following equations:

    oppositeSide = sin(theta) * hypotenuse;
    oppositeSide = tan(theta) * adjacentSide;
    adjacentSide = cos(theta) * hypotenuse;
    adjacentSide = oppositeSide / tan(theta);
    hypotenuse = oppositeSide / sin(theta);
    hypotenuse = adjacentSide / cos(theta);

    These equations give us two methods to find the length of each side of the triangle.

    In the case where none of the angles are known, but the lengths of two of the sides are known, use the Pythagorean theorem to determine the length of the hypotenuse. This theorem is defined as follows:

    Math.Sqrt(Math.Pow(hypotenuse)) = Math.Sqrt(Math.Pow(xSide, 2) + Math.Pow(ySide, 2))

    Simplifying this equation into a syntax usable by C#, we obtain the following code:

    double hypotenuse = Math.Sqrt(Math.Pow(xSide, 2) + Math.Pow(ySide, 2));

    where hypotenuse is equal to the length of the hypotenuse, and xSide and ySide are the lengths of the other two sides.

    See Also: See the “Math Class” topic in the MSDN documentation.

    1.15 Finding the Angles of a Right Triangle Problem

    You need to calculate an angle of a triangle when the lengths of two sides are known.

    Solution

    Use the Math.Atan, Math.Acos, or Math.Asin static methods of the Math class. The following code calculates the angle theta and returns the value in radian measure:

    double theta = Math.Atan(OppositeSide / AdjacentSide);
    theta = Math.Acos(AdjacentSide / Hypotenuse);
    theta = Math.Asin(OppositeSide / Hypotenuse);

    To get the angle in degrees, use the following code:

    double theta = Math.Atan(oppositeSide / adjacentSide) * (180 / Math.PI);
    theta = Math.Acos(adjacentSide / hypotenuse) * (180 / Math.PI); theta = Math.Asin(oppositeSide / hypotenuse) * (180 / Math.PI);

    where theta is the known angle value, the oppositeSide is equal to the length of the side opposite to the angle, and adjacentSide is equal to the length of the side adjacent to the angle. The hypotenuse is the length of the hypotenuse of the triangle. See Figure 1-1 in Recipe 1.14 for a graphical representation of these sides of a right triangle.

    Discussion

    In some cases, we need to determine an angle of a right triangle when only the lengths of two sides are known. The three trigonometric functions arcsine, arccosine, and arctangent allowus to find any angle of a right triangle, given this information. The static methods Math.Atan, Math.Acos, and Math.Asin on the Math class provide the functionality to implement these trigonometric operations.

    See Also: See Recipe 1.14; see the “Math Class” topic in the MSDN documentation.

    Buy the book!If you've enjoyed what you've seen here, or to get more information, click on the "Buy the book!" graphic. Pick up a copy today!

    Visit the O'Reilly Network http://www.oreillynet.com for more online content.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

    C# ARTICLES

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT