public void TestMethodSolve() { QuadraticEquation myQuadraticEquation = new QuadraticEquation(); CollectionAssert.AreEqual(new List<float>() { 2f }, myQuadraticEquation.Solve(0, 2, -4)); CollectionAssert.AreEqual(new List<float>() { 3, -2.5f }, myQuadraticEquation.Solve(2, -1, -15)); CollectionAssert.AreEqual(new List<float>() { 1, 0.6666667f }, myQuadraticEquation.Solve(3, -5, 2)); myQuadraticEquation.Solve(0, 0, 1); OborotovException ex = Assert.ThrowsException<OborotovException>(() => myQuadraticEquation.Solve(0, 0, 5)); Assert.AreEqual("Такое уравнение не существует", ex.Message); }
public static bool GetCollisionSphere(Vector3 lineSource, Vector3 lineDestination, Vector3 position, float radius, out float distanceFromSource) { // Abs(ベクトルの始点 + a線分ベクトル - 球の中心) = 球の半径 // |E + aD - C| = R // D^2 x^2 + 2D(E-C)x + (E-C)^2 - R^2 = 0 distanceFromSource = 0; Vector3 diff = lineSource - position; Vector3 lineEdge = lineDestination - lineSource; float coefQuadric = lineEdge.LengthSq(); float coefLinear = 2f * Vector3.Dot(lineEdge, diff); float coefConst = diff.LengthSq() - radius * radius; QuadraticEquation qe = QuadraticEquation.Solve(coefQuadric, coefLinear, coefConst); if (qe.Answers.Length > 0) { foreach (var ans in qe.Answers) { if (ans >= 0 && ans <= 1) { distanceFromSource = ans * lineEdge.Length(); return(true); } } } return(false); }
static void Main(string[] args) { using (StreamReader sr = new StreamReader("version")) { MyLog.Log($"Версия программы {sr.ReadToEnd().Trim()}"); } QuadraticEquation myQuadraticEquation = new QuadraticEquation(); float a, b, c; Console.WriteLine("Введите 3 параметра a, b и с"); Console.Write("a = "); a = Convert.ToSingle(Console.ReadLine()); Console.Write("b = "); b = Convert.ToSingle(Console.ReadLine()); Console.Write("c = "); c = Convert.ToSingle(Console.ReadLine()); try { MyLog.Log("Корни уравнения: " + String.Join(" ", myQuadraticEquation.Solve(a, b, c))); } catch (TrimailovException e) { MyLog.Log(e.Message); } MyLog.Write(); Console.ReadKey(); }
private void button1_Click(object sender, EventArgs e) { if (a.Text.Trim() == string.Empty || a.Text.Trim() == "0") { MessageBox.Show("Пожалуйста, проверьте аргумент a", "Ошибка"); return; } else if (textBox2.Text.Trim() == string.Empty) { MessageBox.Show("Пожалуйста, проверьте аргумент b", "Ошибка"); return; } else if (textBox3.Text.Trim() == string.Empty) { MessageBox.Show("Пожалуйста, проверьте аргумент с", "Ошибка"); return; } else if (textBox1.Text.Trim() == string.Empty) { MessageBox.Show("Пожалуйста, проверьте аргумент d", "Ошибка"); return; } try { QuadraticEquation qe = new QuadraticEquation(a.Text, textBox2.Text, textBox3.Text, textBox1.Text); double?[] res = qe.Solve(); string result = ""; foreach (double?i in res) { result += " " + Math.Round(Convert.ToDecimal(i), 2) + ";"; } if (res[0] != res[1]) { label3.Text = "x = " + result; } else if (res[0].Equals(null) || res[1].Equals(null)) { label3.Font = new System.Drawing.Font("Leelawadee", 8); label3.Text = "Уравнение имеет комплексные корни."; } else { label3.Text = "x = " + res[0]; } } catch (Exception) { MessageBox.Show("Проверьте введенные данные."); } }
static void Main(string[] args) { var quadraticEquation = new QuadraticEquation(); Console.WriteLine("Digite A"); var a = double.Parse(Console.ReadLine()); Console.WriteLine("Digite B"); var b = double.Parse(Console.ReadLine()); Console.WriteLine("Digite C"); var c = double.Parse(Console.ReadLine()); var x = quadraticEquation.Solve(a, b, c); Console.WriteLine($"X1: {x.x1:N}"); Console.WriteLine($"X2: {x.x2:N}"); }
/// <summary> /// 線分と円筒が交点を持つかを返します. /// </summary> /// <param name="lineSource">線分の始点</param> /// <param name="lineDestination">線分の終点</param> /// <param name="end1">円筒の一方の側面の中心</param> /// <param name="end2">円筒の他方の側面の中心</param> /// <param name="radius">円筒の半径</param> /// <param name="distanceFromSource">線分の始点から円筒までの最短距離</param> /// <returns></returns> public static bool GetCollisionCylinder(Vector3 lineSource, Vector3 lineDestination, Vector3 end1, Vector3 end2, float radius, out float distanceFromSource) { distanceFromSource = 0; // B:line.end T:line.lineDir E:cylinder.End N:cylinder.lineDir R:cylinder.radius // |(B+aT)-E - ((B+aT)-E) . N * N| = R // [(T-T.N N)a + (B-E-B.N N+E.N N)]^2 = R Vector3 lineEdge = lineDestination - lineSource; Vector3 axis = end2 - end1; Vector3 axisDir = Vector3.Normalize(axis); float axisLength = axis.Length(); Vector3 position = end1 - axisDir * radius; Vector3 coefQuadricVec = lineEdge - Vector3.Dot(lineEdge, axisDir) * axisDir; Vector3 endDiff = lineSource - end1; Vector3 coefConstVec = endDiff - Vector3.Dot(endDiff, axisDir) * axisDir; float A = coefQuadricVec.LengthSq(); float B = 2f * Vector3.Dot(coefQuadricVec, coefConstVec); float C = coefConstVec.LengthSq() - radius * radius; QuadraticEquation eq = QuadraticEquation.Solve(A, B, C); if (eq.Indefinite) { distanceFromSource = 0; return(true); } foreach (var ans in eq.Answers) { if (ans < 0) { continue; } Vector3 point = lineSource + lineEdge * ans; Vector3 toPoint = point - end1; float horiz = Vector3.Dot(toPoint, axisDir); if (0 <= horiz && horiz <= axisLength) { distanceFromSource = ans * lineEdge.LengthSq(); return(true); } } return(false); }
public PointF[] Intersect(CircleEquation circle) { var cx = circle.Center.X; var cy = circle.Center.Y; var r = circle.Radius; if (isVertical) { var distance = Math.Abs(cx - xConstForVertical); if (distance > r) { return(new PointF[0]); } if (distance == r) { return new[] { new PointF(xConstForVertical, cy) } } ; // two intersections var dx = cx - xConstForVertical; var qe = new QuadraticEquation( 1, -2 * cy, r * r - dx * dx); return(qe.Solve()); } var t = b - cy; var q = new QuadraticEquation( 1 + a * a, 2 * a * t - 2 * cx, cx * cx + t * t - r * r); var solutions = q.Solve(); for (var i = 0; i < solutions.Length; i++) { solutions[i] = Intersect(solutions[i].X).Value; } return(solutions); } }
/// <summary> /// 给定初始速度大小,目标位置,求角度(复数解),不考虑重力xz方向分力和阻力 /// </summary> /// <param name="initialPosition">初始位置</param> /// <param name="targetPosition">目标位置</param> /// <param name="v">初始速度</param> /// <param name="gravity">重力</param> /// <returns></returns> public static ProjectileMotion[] CalculateAngle(Vector3 initialPosition, Vector3 targetPosition, float v, Vector3 gravity) { var distance = targetPosition - initialPosition; var dirXZ = new Vector3(distance.x, 0f, distance.z).normalized; var x = distance.SetValue(null, 0, null).magnitude; var y = distance.y; var item = gravity.y * x * x / (2 * v * v); float a = item, b = x, c = item - y; var result = QuadraticEquation.Solve(a, b, c); if (result == null) { Debug.LogWarning("给定力度无法达到目标位置"); return(null); } var angle1 = Mathf.Atan(result[0]) * Mathf.Rad2Deg; var angle2 = Mathf.Atan(result[1]) * Mathf.Rad2Deg; //var root = b * b - (4f * a * c); //if (root < 0) //{ // Debug.LogWarning("给定力度无法达到目标位置"); // return null; //} //var angle1 = Mathf.Atan((-b + Mathf.Sqrt(root)) / (2f * a)) * Mathf.Rad2Deg; //var angle2 = Mathf.Atan((-b - Mathf.Sqrt(root)) / (2f * a)) * Mathf.Rad2Deg; var asix = Vector3.Cross(dirXZ, Vector3.up); var v0_1 = v * (Quaternion.AngleAxis(angle1, asix) * dirXZ); var v0_2 = v * (Quaternion.AngleAxis(angle2, asix) * dirXZ); return(new ProjectileMotion[] { new ProjectileMotion(initialPosition, v0_1, Vector3.zero, Vector3.zero, gravity), new ProjectileMotion(initialPosition, v0_2, Vector3.zero, Vector3.zero, gravity) }); }
protected virtual void OnQuadraticValueChanged(double oldValue, double newValue) { if (QuadraticValue > 0) { QuadraticEquation quadraticEquation = new QuadraticEquation(a, b, c - QuadraticValue); quadraticEquation.Solve(); double rootRealPart; double derivate = 2 * a * Value + b; if (derivate > 0) { rootRealPart = quadraticEquation.Root1RealPart; } else { rootRealPart = quadraticEquation.Root2RealPart; } Debug.WriteLine(derivate); if (Math.Abs(Value - rootRealPart) > 0.5) { Value = rootRealPart; } } }
public void QuadraticEquation() { var eq = new QuadraticEquation("A".Parse(), "B".Parse(), "C".Parse()); var roots = eq.Solve(); var context = ImmutableContext.Empty .RegisterValue("A".Parse(), 1) .RegisterValue("B".Parse(), -2) .RegisterValue("C".Parse(), -3); Assert.AreEqual(3, roots.Item1.ToReal(context)); Assert.AreEqual(-1, roots.Item2.ToReal(context)); context = ImmutableContext.Empty .RegisterValue("A".Parse(), 2) .RegisterValue("B".Parse(), -4) .RegisterValue("C".Parse(), -6); Assert.AreEqual(3, roots.Item1.ToReal(context)); Assert.AreEqual(-1, roots.Item2.ToReal(context)); eq = new QuadraticEquation("X + 1".Parse(), "Y - 2".Parse(), "Z / 2".Parse()); roots = eq.Solve(); context = ImmutableContext.Empty .RegisterValue("X".Parse(), 0) .RegisterValue("Y".Parse(), 0) .RegisterValue("Z".Parse(), -6); Assert.AreEqual(3, roots.Item1.ToReal(context)); Assert.AreEqual(-1, roots.Item2.ToReal(context)); }