Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

The right-angle triangle: Meet your new best friend

Rate me:
Please Sign up or sign in to vote.
4.92/5 (94 votes)
5 Feb 2014CPOL6 min read 127.6K   887   90  
Did you skip class the day trigonometry was taught? This article will explain what you’ve been missing all these years.
using System;
using System.Windows;
using System.Windows.Shapes;

namespace RedCell.Articles.RightAngleTriangle
{
    /// <summary>
    /// A ball
    /// </summary>
    public class Ball
    {
        #region Fields
        private Ellipse _ui;
        #endregion

        #region Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="Ball"/> class.
        /// </summary>
        public Ball()
        {
            Elasticity = 1;
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the X coordinate.
        /// </summary>
        /// <value>
        /// The X coordinate.
        /// </value>
        public double X { get; set; }

        /// <summary>
        /// Gets or sets the Y coordinate.
        /// </summary>
        /// <value>
        /// The Y coordinate.
        /// </value>
        public double Y { get; set; }

        /// <summary>
        /// Gets or sets the speed.
        /// </summary>
        /// <value>
        /// The speed.
        /// </value>
        public double Speed { get; set; }

        /// <summary>
        /// Gets or sets the angle.
        /// </summary>
        /// <value>
        /// The angle.
        /// </value>
        public double Angle { get; set; }

        /// <summary>
        /// Gets or sets the radius.
        /// </summary>
        /// <value>
        /// The radius.
        /// </value>
        public double Radius { get; set; }

        /// <summary>
        /// Gets the mass.
        /// </summary>
        public double Mass
        {
            get { return Math.PI*Math.Pow(Radius, 2); }
        }

        /// <summary>
        /// Gets the width.
        /// </summary>
        public double Width
        {
            get { return Radius*2; }
        }

        /// <summary>
        /// Gets the height.
        /// </summary>
        public double Height
        {
            get { return Radius * 2; }
        }

        /// <summary>
        /// Gets or sets the elasticity.
        /// </summary>
        /// <value>
        /// The elasticity.
        /// </value>
        public double Elasticity { get; set; }

        /// <summary>
        /// Gets or sets the UI element.
        /// </summary>
        /// <value>
        /// The UI element.
        /// </value>
        internal Ellipse UI
        {
            get
            {
                if (_ui == null)
                    _ui = new Ellipse
                                  {
                                      Width = Radius * 2,
                                      Height = Radius * 2,
                                      Margin = new Thickness(X - Radius, Y - Radius, 0, 0)
                                  };
                return _ui;
            }
        }

        /// <summary>
        /// Gets the location.
        /// </summary>
        public Point Location 
        { 
            get { return new Point(X, Y);}
        }

        /// <summary>
        /// Gets a value indicating whether this instance is moving.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is moving; otherwise, <c>false</c>.
        /// </value>
        public bool IsMoving { get; internal set; }
        #endregion

        #region Methods
        /// <summary>
        /// Determines whether the ball contains the specified ball.
        /// </summary>
        /// <param name="point">The ball.</param>
        /// <returns>
        ///   <c>true</c> if the ball contains the specified point; otherwise, <c>false</c>.
        /// </returns>
        public bool Contains(Ball point)
        {
            var cdx = point.X - X;
            var cdy = point.Y - Y;
            var cdh = Math.Sqrt(Math.Pow(cdx, 2) + Math.Pow(cdy, 2));
            return cdh <= Radius;
        }

        /// <summary>
        /// Collides the specified ball.
        /// </summary>
        /// <param name="ball">The ball.</param>
        public void Collide(Ball ball)
        {
            // Calculate angles.
            var cdx = ball.X - X;
            var cdy = ball.Y - Y;
            var oldAngle = ball.Angle;
            var incidence = Math.Atan(cdy/cdx);
            var newAngle = ball.Angle = incidence * 2;
            Angle = newAngle-oldAngle + Math.PI;
        }

        /// <summary>
        /// Moves the specified angle.
        /// </summary>
        public void Move()
        {
            X += Math.Cos(Angle) * Speed;
            Y += Math.Sin(Angle) * Speed;
            UI.Margin = new Thickness(X - Radius, Y - Radius, 0, 0);
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer Robotic Assistance Devices / AITX
Canada Canada
Yvan Rodrigues has 30 years of experience in information systems and software development for the industry. He is Senior Concept Designer at Robotic Assistance Devices

He is a Certified Technician (C.Tech.), a professional designation granted by the Institute of Engineering Technology of Ontario (IETO).

Yvan draws on experience as owner of Red Cell Innovation Inc., Mabel's Labels Inc. as Manager of Systems and Development, the University of Waterloo as Information Systems Manager, and OTTO Motors as Senior Systems Engineer and Senior Concept Designer.

Yvan is currently focused on design of embedded systems.

Comments and Discussions