/// <summary> /// Gets the angle in degrees for a ray that extends from the center /// of the circle through the given point. An exception will occur, /// however, if the point specified is the same point as the center /// of the circle. /// </summary> /// <param name="pt">The point.</param> public decimal AngleThroughPoint(Point2D pt) { decimal a = 0m; // Shift point so it's relative to the origin. pt += new Vector2D(-Center.X, -Center.Y); // Do check here after we've translated the point // to account for small precision rounding. if (pt == Point2D.Origin) { throw new Exception("No angle through given point since point is at center of circle!"); } // Special case for 0 and 180 where we would get // a divide by zero below. if (pt.Y == 0) { if (pt.X > 0) { return(0); } else { return(180); } } // Special case for 90 and 270 where we can't // determine quadrant below. if (pt.X == 0) { if (pt.Y > 0) { return(90); } else { return(270); } } a = DecimalEx.ToDeg(DecimalEx.ATan(pt.X / pt.Y)); switch (pt.Quadrant) { case 1: case 2: a = 90 - a; break; case 3: case 4: a = 270 - a; break; } return(a); }
/// <summary> /// Gets the angle in degrees between this vector and another. /// </summary> /// <param name="other">The other vector to get the angle to.</param> /// <remarks>See http://en.wikipedia.org/wiki/Dot_product</remarks> public decimal AngleTo(Vector2D other) { if (this.IsNull || other.IsNull) { throw new Exception("Can't find angle when one or both vectors are null vectors!"); } // Cos(theta) = DotProduct(v1,v2) / (length(v1) * length(v2)) // aka theta = acos(v.normalize.dot(other.normalize)), however, the equation // used gives us better precision return(DecimalEx.ToDeg(DecimalEx.ACos(this.Dot(other) / (this.Magnitude * other.Magnitude)))); }
/// <summary> /// Gets the angle of the vector in degrees from the X+ axis. Will return result in the range /// -180 to +180. Will return 0 for null vector. /// </summary> public decimal Angle() { return(DecimalEx.ToDeg(DecimalEx.ATan2(Y, X))); }
/// <summary> /// Gets angle in degrees from the adjacent side and the hypotenuse. /// </summary> /// <param name="adjacentSide">Length of the known side adjacent to the angle.</param> /// <param name="hypotenuse">Length of the hypotenuse.</param> public static decimal GetAngleFromAdjSideHyp(decimal adjacentSide, decimal hypotenuse) { // cos(a) = adjacentSide / hypotenuse // a = acos(adjacentSide / hypotenuse) return(DecimalEx.ToDeg(DecimalEx.ACos(adjacentSide / hypotenuse))); }
/// <summary> /// Gets angle in degrees from the opposite side and the hypotenuse. /// </summary> /// <param name="oppositeSide">Length of the known side opposite the angle.</param> /// <param name="hypotenuse">Length of the hypotenuse.</param> public static decimal GetAngleFromOppSideHyp(decimal oppositeSide, decimal hypotenuse) { // sin(a) = oppositeSide / hypotenuse // a = asin(oppositeSide / hypotenuse) return(DecimalEx.ToDeg(DecimalEx.ASin(oppositeSide / hypotenuse))); }
/// <summary> /// Gets angle in degrees from the two known sides. /// </summary> /// <param name="oppositeSide">Length of the known side opposite the angle.</param> /// <param name="adjacentSide">Length of the known side adjacent to the angle.</param> public static decimal GetAngleFromSides(decimal oppositeSide, decimal adjacentSide) { // tan(a) = opposideSide / adjacentSide // a = atan(opposideSide / adjacentSide) return(DecimalEx.ToDeg(DecimalEx.ATan(oppositeSide / adjacentSide))); }