circle_equation.dart 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // tools/circle_equation.dart
  2. import 'package:flutter/material.dart';
  3. import 'tool_kit.dart';
  4. /// 圆的方程 —— 过三点的圆。
  5. ///
  6. /// 设圆为 x² + y² + Dx + Ey + F = 0,把三个点代进去解 D、E、F。
  7. /// 未知量在 Symja 侧用 dd/ee/ff 命名(D、E 是 Symja 的内置名)。
  8. /// 三点共线时 Symja 的 Solve 返回空集,我们的表达式直接回一句中文说明。
  9. class CircleEquationPage extends StatefulWidget {
  10. const CircleEquationPage({super.key});
  11. @override
  12. State<CircleEquationPage> createState() => _CircleEquationPageState();
  13. }
  14. class _CircleEquationPageState extends State<CircleEquationPage> {
  15. final _x1 = ToolField('点1 x', hint: '分数如 1/2,或小数');
  16. // 点的横纵坐标是一对,并排一行
  17. final _y1 = ToolField('点1 y', hint: '分数如 1/2,或小数', rowWithPrevious: true);
  18. final _x2 = ToolField('点2 x', hint: '分数如 1/2,或小数');
  19. // 点的横纵坐标是一对,并排一行
  20. final _y2 = ToolField('点2 y', hint: '分数如 1/2,或小数', rowWithPrevious: true);
  21. final _x3 = ToolField('点3 x', hint: '分数如 1/2,或小数');
  22. // 点的横纵坐标是一对,并排一行
  23. final _y3 = ToolField('点3 y', hint: '分数如 1/2,或小数', rowWithPrevious: true);
  24. @override
  25. Widget build(BuildContext context) => ToolScaffold(
  26. title: '圆的方程',
  27. fields: [_x1, _y1, _x2, _y2, _x3, _y3],
  28. compute: (session) async {
  29. final bind =
  30. 'x1=${_need(_x1)},y1=${_need(_y1)},'
  31. 'x2=${_need(_x2)},y2=${_need(_y2)},'
  32. 'x3=${_need(_x3)},y3=${_need(_y3)}';
  33. final eqs =
  34. '{x1^2+y1^2+dd*x1+ee*y1+ff==0, '
  35. 'x2^2+y2^2+dd*x2+ee*y2+ff==0, '
  36. 'x3^2+y3^2+dd*x3+ee*y3+ff==0}';
  37. const noCircle = '"三点共线(没有外接圆)"';
  38. // 三个系数
  39. final defExpr =
  40. 'With[{$bind}, Module[{s}, s=Solve[$eqs,{dd,ee,ff}]; '
  41. 'If[s==={}, $noCircle, {dd,ee,ff} /. First[s]]]]';
  42. final defs = parseSymjaList((await session.compute(defExpr)).text);
  43. if (defs.length != 3) {
  44. // 共线:Symja 返回的就是那句中文,直接显示。
  45. return [ToolText('结果', '${defs.isEmpty ? '' : defs.first}')];
  46. }
  47. // 圆心 (-dd/2, -ee/2) 与半径 √(dd²+ee²-4ff)/2
  48. final centerExpr =
  49. 'With[{$bind}, Module[{s}, s=Solve[$eqs,{dd,ee,ff}]; '
  50. 'If[s==={}, $noCircle, '
  51. '{-dd/2, -ee/2, Sqrt[dd^2+ee^2-4*ff]/2} /. First[s]]]]';
  52. // 精确值 + 近似值都要:系数和圆心半径都是分数,小数形式有时更直观
  53. final defsN = parseSymjaList(
  54. (await session.compute(
  55. approxExpr('Rationalize[$defExpr]', session.approximateDigits),
  56. )).text,
  57. );
  58. final center = parseSymjaList((await session.compute(centerExpr)).text);
  59. final centerN = parseSymjaList(
  60. (await session.compute(
  61. approxExpr('Rationalize[$centerExpr]', session.approximateDigits),
  62. )).text,
  63. );
  64. String at(List<Object> l, int i) => i < l.length ? '${l[i]}' : '';
  65. final out = <ToolOutput>[
  66. ToolText('D', withApprox('${defs[0]}', at(defsN, 0))),
  67. ToolText('E', withApprox('${defs[1]}', at(defsN, 1))),
  68. ToolText('F', withApprox('${defs[2]}', at(defsN, 2))),
  69. ];
  70. if (center.length == 3) {
  71. // 圆心是坐标,两个分量各自配自己的近似值
  72. out.add(
  73. ToolText(
  74. '圆心',
  75. '(${withApprox('${center[0]}', at(centerN, 0))}, '
  76. '${withApprox('${center[1]}', at(centerN, 1))})',
  77. ),
  78. );
  79. out.add(ToolText('半径', withApprox('${center[2]}', at(centerN, 2))));
  80. }
  81. return out;
  82. },
  83. );
  84. /// 取一个标量输入框:空着或填了多个值都直接报中文错。
  85. String _need(ToolField f) {
  86. if (f.count == 0) throw Exception('请填写「${f.label}」');
  87. if (f.count > 1) throw Exception('「${f.label}」只能填一个数');
  88. return f.text;
  89. }
  90. }