private void TestSqrt() { mResult.Append("Testing Sqrt function\r\n"); for (float x = 0; x < 2000; x += 3.243f) { Check((float)Math.Sqrt(x), Math2.Sqrt(x), "Sqrt: {0} != {1}"); } }
public ArcInterpolation(CPointF origin, CPointF end, float radius, bool clockwise) { // PhlatScript uses the radius format, so we need to support it. Origin = origin; End = end; Distance = radius; Clockwise = !clockwise; // Calculate center. Best explanation found here: // http://mathforum.org/library/drmath/view/53027.html float x1 = Origin.X; float y1 = Origin.Y; float x2 = End.X; float y2 = End.Y; float r = Distance; // Distance between start and end float q = Math2.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); // middle ploint between both points float x3 = (x1 + x2) / 2; float y3 = (y1 + y2) / 2; if (!Clockwise) { Center = new CPointF( x3 - Math2.Sqrt(r * r - (q / 2) * (q / 2)) * (y1 - y2) / q, y3 - Math2.Sqrt(r * r - (q / 2) * (q / 2)) * (x2 - x1) / q); } else { Center = new CPointF( x3 + Math2.Sqrt(r * r - (q / 2) * (q / 2)) * (y1 - y2) / q, y3 + Math2.Sqrt(r * r - (q / 2) * (q / 2)) * (x2 - x1) / q); } const string E_NO_ARC_CENTER = "Could not find a suitable center for arc"; if (Center.X == float.MinValue) { Machine.Error(E_NO_ARC_CENTER); } if (Center.Y == float.MinValue) { Machine.Error(E_NO_ARC_CENTER); } Initialize(); }
private void Initialize() { // Distance from start to center. const float twoPi = 2 * 3.141592654f; float oox = Origin.X - Center.X; float ooy = Origin.Y - Center.Y; float eex = End.X - Center.X; float eey = End.Y - Center.Y; Distance = (float)Math2.Sqrt(oox * oox + ooy * ooy); // Alpha angle: start with X axis Alpha = (float)Math2.Atan2(ooy, oox); // Beta angle: end with X axis Beta = (float)Math2.Atan2(eey, eex); // Gamma angle is arc angle (beta - alpha) if (Alpha < 0 && Beta > 0) { Gamma = Beta - (Alpha + twoPi); } else if (Alpha > 0 && Beta < 0) { Gamma = (Beta + twoPi) - Alpha; } else { Gamma = Beta - Alpha; } if (Math2.Abs(Gamma) > 3.141592654f) { Gamma = Beta - Alpha; } }