vector_normal.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // tools/vector_normal.dart
  2. //
  3. // 计算部分走 Symja(符号计算库),不再用自研的 rationalsqrt2.dart。
  4. // 那个自研的 √n 代数只能表示"有理数 × 一层根号 + 若干项之和",遇到
  5. // 坐标里出现两层根号、分数套根号、或者三角函数就得放弃;交给 CAS 之后
  6. // 输入什么就能精确算什么。
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_math_fork/flutter_math.dart';
  9. import 'package:provider/provider.dart';
  10. import '../symja.dart';
  11. class VectorNormalPage extends StatefulWidget {
  12. @override
  13. _VectorNormalPageState createState() => _VectorNormalPageState();
  14. }
  15. class _VectorNormalPageState extends State<VectorNormalPage> {
  16. final List<TextEditingController> _controllers = List.generate(9, (_) => TextEditingController());
  17. String _result = '';
  18. String? _resultTex;
  19. String? _error;
  20. bool _isCalculating = false;
  21. @override
  22. void dispose() {
  23. for (var controller in _controllers) {
  24. controller.dispose();
  25. }
  26. super.dispose();
  27. }
  28. // 计算法向量:AB × AC,全部交给 Symja 做符号运算
  29. Future<void> _calculateNormal() async {
  30. final session = context.read<SymjaSession>();
  31. setState(() {
  32. _isCalculating = true;
  33. _result = '';
  34. _resultTex = null;
  35. _error = null;
  36. });
  37. // 每个坐标先过一遍语法转换,这样用户沿用旧的 `2√3` 写法也能用
  38. String coord(int point, int axis) {
  39. final raw = _controllers[point * 3 + axis].text.trim();
  40. // 原样交给 Symja
  41. return raw.isEmpty ? '0' : raw;
  42. }
  43. // 边向量:to - from
  44. String vec(int from, int to) => '{'
  45. '(${coord(to, 0)} - ${coord(from, 0)}), '
  46. '(${coord(to, 1)} - ${coord(from, 1)}), '
  47. '(${coord(to, 2)} - ${coord(from, 2)})}';
  48. final expr = 'Cross[${vec(0, 1)}, ${vec(0, 2)}]';
  49. try {
  50. final r = await session.compute(expr);
  51. if (!mounted) return;
  52. setState(() {
  53. _result = r.text;
  54. _resultTex = r.tex;
  55. });
  56. } on SymjaException catch (e) {
  57. if (!mounted) return;
  58. setState(() => _error = '$e');
  59. } finally {
  60. if (mounted) setState(() => _isCalculating = false);
  61. }
  62. }
  63. // 清除所有输入
  64. void _clearAll() {
  65. for (var controller in _controllers) {
  66. controller.clear();
  67. }
  68. setState(() {
  69. _result = '';
  70. _resultTex = null;
  71. _error = null;
  72. });
  73. }
  74. @override
  75. Widget build(BuildContext context) {
  76. return Scaffold(
  77. appBar: AppBar(
  78. title: Text('三点法向量计算'),
  79. backgroundColor: Colors.white,
  80. elevation: 1,
  81. ),
  82. body: SingleChildScrollView(
  83. padding: EdgeInsets.all(16),
  84. child: Column(
  85. crossAxisAlignment: CrossAxisAlignment.start,
  86. children: [
  87. _buildInputSection('点A', 0),
  88. SizedBox(height: 16),
  89. _buildInputSection('点B', 3),
  90. SizedBox(height: 16),
  91. _buildInputSection('点C', 6),
  92. SizedBox(height: 24),
  93. _buildControlButtons(),
  94. SizedBox(height: 24),
  95. _buildResultSection(),
  96. SizedBox(height: 24),
  97. _buildInstructions(),
  98. ],
  99. ),
  100. ),
  101. );
  102. }
  103. Widget _buildInputSection(String title, int startIndex) {
  104. return Card(
  105. elevation: 2,
  106. child: Padding(
  107. padding: EdgeInsets.all(16),
  108. child: Column(
  109. crossAxisAlignment: CrossAxisAlignment.start,
  110. children: [
  111. Text(
  112. title,
  113. style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  114. ),
  115. SizedBox(height: 12),
  116. Row(
  117. children: [
  118. _buildCoordinateInput('x', _controllers[startIndex]),
  119. SizedBox(width: 12),
  120. _buildCoordinateInput('y', _controllers[startIndex + 1]),
  121. SizedBox(width: 12),
  122. _buildCoordinateInput('z', _controllers[startIndex + 2]),
  123. ],
  124. ),
  125. ],
  126. ),
  127. ),
  128. );
  129. }
  130. Widget _buildCoordinateInput(String label, TextEditingController controller) {
  131. return Expanded(
  132. child: Column(
  133. crossAxisAlignment: CrossAxisAlignment.start,
  134. children: [
  135. Text(
  136. label,
  137. style: TextStyle(fontSize: 14, color: Colors.grey[600]),
  138. ),
  139. SizedBox(height: 4),
  140. TextField(
  141. controller: controller,
  142. decoration: InputDecoration(
  143. hintText: '0',
  144. border: OutlineInputBorder(),
  145. contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
  146. ),
  147. keyboardType: TextInputType.number,
  148. ),
  149. ],
  150. ),
  151. );
  152. }
  153. Widget _buildControlButtons() {
  154. return Row(
  155. children: [
  156. Expanded(
  157. flex: 3,
  158. child: ElevatedButton(
  159. onPressed: _isCalculating ? null : _calculateNormal,
  160. style: ElevatedButton.styleFrom(
  161. backgroundColor: Colors.blue,
  162. padding: EdgeInsets.symmetric(vertical: 12),
  163. ),
  164. child: _isCalculating
  165. ? SizedBox(
  166. width: 20,
  167. height: 20,
  168. child: CircularProgressIndicator(
  169. strokeWidth: 2,
  170. valueColor: AlwaysStoppedAnimation(Colors.white),
  171. ),
  172. )
  173. : Text(
  174. '计算法向量',
  175. style: TextStyle(fontSize: 16, color: Colors.white),
  176. ),
  177. ),
  178. ),
  179. SizedBox(width: 12),
  180. Expanded(
  181. flex: 2,
  182. child: OutlinedButton(
  183. onPressed: _clearAll,
  184. style: OutlinedButton.styleFrom(
  185. padding: EdgeInsets.symmetric(vertical: 12),
  186. ),
  187. child: Text('清除', style: TextStyle(fontSize: 16)),
  188. ),
  189. ),
  190. ],
  191. );
  192. }
  193. Widget _buildResultSection() {
  194. if (_error != null) {
  195. return Card(
  196. color: Colors.red[50],
  197. elevation: 2,
  198. child: Padding(
  199. padding: EdgeInsets.all(16),
  200. child: Row(
  201. crossAxisAlignment: CrossAxisAlignment.start,
  202. children: [
  203. Icon(Icons.error_outline, color: Colors.red[700], size: 20),
  204. SizedBox(width: 8),
  205. Expanded(
  206. child: SelectableText(
  207. '计算错误:$_error',
  208. style: TextStyle(color: Colors.red[700], fontSize: 14),
  209. ),
  210. ),
  211. ],
  212. ),
  213. ),
  214. );
  215. }
  216. if (_result.isEmpty) return SizedBox();
  217. return Card(
  218. color: Colors.grey[50],
  219. elevation: 2,
  220. child: Padding(
  221. padding: EdgeInsets.all(16),
  222. child: Column(
  223. crossAxisAlignment: CrossAxisAlignment.start,
  224. children: [
  225. Text(
  226. '计算结果',
  227. style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  228. ),
  229. SizedBox(height: 12),
  230. // 法向量是精确的符号结果(可能带根号、分数),用 LaTeX 排出来
  231. // 比纯文本好读得多。
  232. if (_resultTex != null)
  233. Math.tex(
  234. _resultTex!,
  235. mathStyle: MathStyle.display,
  236. textStyle: TextStyle(fontSize: 20, color: Colors.blue[800]),
  237. onErrorFallback: (_) => Text(
  238. _result,
  239. style: TextStyle(fontSize: 16, color: Colors.blue[700]),
  240. ),
  241. )
  242. else
  243. Text(
  244. _result,
  245. style: TextStyle(fontSize: 16, color: Colors.blue[700]),
  246. ),
  247. SizedBox(height: 8),
  248. // 纯文本那份留着,方便复制到别处
  249. SelectableText(
  250. _result,
  251. style: TextStyle(fontSize: 12, color: Colors.grey[700]),
  252. ),
  253. SizedBox(height: 8),
  254. Text(
  255. '说明:法向量是通过向量AB和向量AC的叉积计算得到',
  256. style: TextStyle(fontSize: 12, color: Colors.grey[600]),
  257. ),
  258. ],
  259. ),
  260. ),
  261. );
  262. }
  263. Widget _buildInstructions() {
  264. return Card(
  265. color: Colors.grey[50],
  266. elevation: 2,
  267. child: Padding(
  268. padding: EdgeInsets.all(16),
  269. child: Column(
  270. crossAxisAlignment: CrossAxisAlignment.start,
  271. children: [
  272. Text(
  273. '使用说明',
  274. style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
  275. ),
  276. SizedBox(height: 12),
  277. Text(
  278. '坐标按 Symja 语法输入,例如:\n'
  279. ' 2/3 分数\n'
  280. ' Sqrt[2] 根号(也可写 √2)\n'
  281. ' Sqrt[2]/2、1 + Sqrt[3]、Pi\n'
  282. '\n'
  283. '结果是精确值:带根号、分数会原样保留,不会转成小数。\n'
  284. '如果已知两向量坐标,将一个点设为原点即可。',
  285. style: TextStyle(fontSize: 14, color: Colors.grey[700]),
  286. ),
  287. ],
  288. ),
  289. ),
  290. );
  291. }
  292. }