returns the angle subtended by three points - CSharp System

CSharp examples for System:Math Geometry

Description

returns the angle subtended by three points

Demo Code

/*//from   w  ww . j a va 2 s .  com
    Computational geometry routines
    Copyright (C) 2000-2007 Bob Mottram
    fuzzgun@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;

public class Main{
        /// <summary>
        /// returns the angle subtended by three points
        /// </summary>
        /// <param name="x0">line first point x coordinate</param>
        /// <param name="y0">line first point y coordinate</param>
        /// <param name="x1">line second point x coordinate</param>
        /// <param name="y1">line second point y coordinate</param>
        /// <param name="x2">line third point x coordinate</param>
        /// <param name="y2">line third point y coordinate</param>
        /// <returns>angle in radians</returns>
        public static float threePointAngle(float x0, float y0,
                                            float x1, float y1,
                                            float x2, float y2)
        {
            float pt1 = x0 - x1;
            float pt2 = y0 - y1;
            float pt3 = x2 - x1;
            float pt4 = y2 - y1;

            float angle = ((pt1 * pt3) + (pt2 * pt4)) /
                           (((float)Math.Sqrt((pt1 * pt1) + (pt2 * pt2))) *
                            ((float)Math.Sqrt((pt3 * pt3) + (pt4 * pt4))));

            angle = (float)Math.Acos(angle);
            return (angle);
        }
}

Related Tutorials