// tools/vector_normal.dart // // 三点法向量:AB × AC 全部交给 Symja 做符号运算。 // // 坐标按**用户原文**拼进表达式(不转成小数)—— 用户写 `2/3`、`Sqrt[2]`、`Pi`, // 算出来的法向量就是精确值,带根号和分数,而不是 0.3333 这种近似。 import 'package:flutter/material.dart'; import 'tool_kit.dart'; class VectorNormalPage extends StatefulWidget { const VectorNormalPage({super.key}); @override State createState() => _VectorNormalPageState(); } class _VectorNormalPageState extends State { // 9 个坐标框建在 State 里:主题一变页面重建时控制器不会跟着换掉、用户打的字会丢。 // 顺序:A.x A.y A.z B.x B.y B.z C.x C.y C.z。 final List _coords = [ ToolField('点 A 的 x', hint: '例如 2/3、Sqrt[2]、Pi'), // 一个点的三个坐标并排一行 ToolField('点 A 的 y', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ToolField('点 A 的 z', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ToolField('点 B 的 x', hint: '例如 2/3、Sqrt[2]、Pi'), // 一个点的三个坐标并排一行 ToolField('点 B 的 y', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ToolField('点 B 的 z', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ToolField('点 C 的 x', hint: '例如 2/3、Sqrt[2]、Pi'), // 一个点的三个坐标并排一行 ToolField('点 C 的 y', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ToolField('点 C 的 z', hint: '例如 2/3、Sqrt[2]、Pi', rowWithPrevious: true), ]; /// 取第 [point] 个点(0=A、1=B、2=C)的第 [axis] 个坐标(0=x、1=y、2=z)。 /// 空着当 0;其余**原样**返回,交给 Symja 解析(Dart 侧不做数字转换)。 String _coord(int point, int axis) { final raw = _coords[point * 3 + axis].text; return raw.isEmpty ? '0' : raw; } /// 边向量 to - from,写成 Symja 的列表。每个坐标都加括号, /// 免得用户写的 `1 + Sqrt[3]` 和后面的减号黏在一起算错。 String _edge(int from, int to) => '{' '(${_coord(to, 0)}) - (${_coord(from, 0)}), ' '(${_coord(to, 1)}) - (${_coord(from, 1)}), ' '(${_coord(to, 2)}) - (${_coord(from, 2)})}'; @override Widget build(BuildContext context) => ToolScaffold( title: '三点法向量计算', fields: _coords, compute: (session) async { final ab = _edge(0, 1); // B - A final ac = _edge(0, 2); // C - A final r = await session.compute('Cross[$ab, $ac]'); return [ ToolText('法向量 AB × AC', r.text, tex: r.tex), const ToolText('说明', '法向量由向量 AB 与向量 AC 的叉积计算得到。'), ]; }, ); }