using System; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; using System.Text; namespace DevComponents.DotNetBar.Charts { internal static class MathHelper { #region ToDegrees public static double ToDegrees(double radians) { return (radians * 180 / Math.PI); } #endregion #region ToRadians public static double ToRadians(double degrees) { return (degrees * Math.PI / 180); } #endregion #region GetPointAngle public static int GetPointAngle(Point pt) { double theta = Math.Atan2(pt.X, -pt.Y); int angle = (int)(MathHelper.ToDegrees(theta) + 270) % 360; return (angle); } #endregion #region GetPointRadius public static int GetPointRadius(ref Point pt, Point cpt) { pt.X -= cpt.X; pt.Y -= cpt.Y; return ((int)Math.Sqrt(pt.X * pt.X + pt.Y * pt.Y)); } #endregion #region GetOffsetPoint public static Point GetOffsetPoint( Point pt, Point cpt, out int radius, out int angle) { radius = MathHelper.GetPointRadius(ref pt, cpt); angle = MathHelper.GetPointAngle(pt); return (pt); } #endregion #region Clamp public static T Clamp(T value, T min, T max) where T : IComparable { T result = value; if (result.CompareTo(max) > 0) result = max; if (result.CompareTo(min) < 0) result = min; return (result); } #endregion #region ToScalar public static double ToScalar(double x) { return (MathHelper.Sqrt((float)(x / Math.PI)) * 2); } #endregion #region Sqrt [StructLayout(LayoutKind.Explicit)] private struct FloatIntUnion { [FieldOffset(0)] public float f; [FieldOffset(0)] public int tmp; } public static float Sqrt(float x) { if (x != 0) { FloatIntUnion u; u.tmp = 0; u.f = x; u.tmp -= 1 << 23; // Subtract 2^m u.tmp >>= 1; // Divide by 2 u.tmp += 1 << 29; // Add ((b + 1) / 2) * 2^m return (u.f); } return (0); } #endregion } }