Skip to content

Commit d2645a6

Browse files
committed
pakoo
1 parent f6bda13 commit d2645a6

16 files changed

Lines changed: 859 additions & 366 deletions

File tree

lib/MathForest/Algebra/Trunk/Fertile/DNum.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
import 'dart:math';
2+
import 'dart:math' as math;
33

44
/*
55
@@ -13,6 +13,9 @@ class DNum {
1313
n1 = n1 ?? -1,
1414
n2 = n2 ?? 1;
1515

16+
num get min => math.min(n1, n2);
17+
num get max => math.max(n1, n2);
18+
1619
@override
1720
String toString() {
1821
return 'DNum(${n1.toString()}, ${n2.toString()})';

lib/MathForest/Geometry/D2/Conic/Circle.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//
22

33
import 'dart:math';
4+
import '../../../Algebra/Functions/Main.dart' as funcs;
45
//
56
import '../../../Algebra/Trunk/Fertile/DNum.dart';
67
import '../../../Algebra/Trunk/Fertile/QNum.dart';
@@ -37,6 +38,10 @@ class Circle {
3738
indexPoint(theta.n4),
3839
);
3940

41+
num disP(Vector p0){
42+
return funcs.abs((p0 - p).len - r);
43+
}
44+
4045
@override
4146
String toString() {
4247
return "Cir2(${p.toString()} , $r})";

lib/MathForest/Geometry/D2/Conic/Conic0.dart

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,61 @@ class Conic0 {
143143
}
144144

145145
//
146-
num thetaClosestP2P(Vector P){
147-
num t0 ;
148-
if (u.len>=v.len) {
149-
if ((P-p).dot(v)>0 ){ t0=pi/2; }else{ t0=-pi/2; }
150-
} else {
151-
if ((P-p).dot(u)>0) { t0=0; }else{ t0=pi; }
146+
num thetaClosestP2P(Vector P, {double tolerance = 1e-8, int maxIterations = 50}) {
147+
// 局部优化函数
148+
num optimizeFrom(num t0) {
149+
num t = t0;
150+
num k = -0.5; // 初始学习率
151+
num prevDistance = double.infinity;
152+
153+
for (var i = 0; i < maxIterations; i++) {
154+
// 计算梯度(距离平方的导数)
155+
num gradient = derDisP2P(P, t);
156+
157+
// 收敛检测
158+
if (gradient.abs() < tolerance) break;
159+
160+
// 更新参数
161+
t += k * gradient;
162+
163+
// 规范化角度到 [0, 2π)
164+
t = t % (2 * pi);
165+
if (t < 0) t += 2 * pi;
166+
167+
// 计算当前距离
168+
num currentDistance = disP2P(P, t);
169+
170+
// 检查距离变化
171+
if ((prevDistance - currentDistance).abs() < tolerance) break;
172+
prevDistance = currentDistance;
173+
174+
// 衰减学习率
175+
k *= 0.85; // 每次衰减15%
176+
}
177+
return t;
178+
}
179+
180+
// 初始点:覆盖一个周期 [0, 2π)
181+
List<num> initialThetas = [];
182+
for (int i = 0; i < 12; i++) {
183+
initialThetas.add(i * pi / 6); // 每30°一个点
152184
}
153-
num t=t0;
154-
num k=-0.05;
155-
for (var i = 0; i < 12; i++) {
156-
t = t + k * derDisP2P(P,t);
157-
k=k/exp(0.0618*i);
185+
186+
// 优化每个初始点并找到最佳结果
187+
num bestTheta = initialThetas[0];
188+
num minDistance = double.infinity;
189+
190+
for (num t0 in initialThetas) {
191+
num theta = optimizeFrom(t0);
192+
num distance = disP2P(P, theta);
193+
194+
if (distance < minDistance) {
195+
minDistance = distance;
196+
bestTheta = theta;
197+
}
158198
}
159-
return t;
199+
200+
return bestTheta;
160201
}
161202

162203
Vector pClosestP2P(Vector P){
@@ -167,6 +208,10 @@ class Conic0 {
167208
return P.dis(pClosestP2P(P));
168209
}
169210

211+
num disP(Vector P){
212+
return disClosestP2P(P);
213+
}
214+
170215

171216

172217
//Conic get conic => Conic().byConic0(this);

lib/MathForest/Geometry/D2/Fertile/DPoint.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// 平面骈点
22
import '../Linear/Line.dart';
33
import '../Linear/Vector.dart';
4+
import '../../../Algebra/Trunk/Fertile/DNum.dart';
45

56
/*
67
共生双点,没有区分方法和必要
@@ -26,6 +27,10 @@ class DPoint {
2627

2728
Line get l => Line.new2P(p1, p2);
2829

30+
DNum disP(Vector p0){
31+
return DNum(p0.dis(p1), p0.dis(p2));
32+
}
33+
2934
static DPoint zero = DPoint(Vector.zero, Vector.zero);
3035
static DPoint inf = DPoint(Vector.inf, Vector.inf);
3136
static DPoint nan = DPoint(Vector.nan, Vector.nan);

lib/MathForest/Geometry/D2/GMK/Core/GMKCompiler.dart

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import '../Monxiv/GOBJStyle.dart' as gStyle;
88

99
import 'GMKCommand.dart';
1010
import 'GMKStructure.dart';
11-
import 'GMKError.dart';
11+
import 'GMKLib.dart' as g_lib;
1212

1313
import '../../Linear/Vector.dart';
1414
import '../../Conic/Circle.dart';
@@ -157,14 +157,21 @@ GMKStructure goCompiler(String source) {
157157
String label = subStringBetween(line, '@', ' is ').trim();
158158
String method = subStringBetween(line, ' is ', ' of ').trim();
159159
List<dynamic> factor = str2Factor(extractAfter(line, ' of '));
160-
structure.addStep(GMKCommand(method, label, factor));
160+
//
161+
GMKCommand cmd = GMKCommand(method, label, factor);
162+
cmd.type = g_lib.method2type[method] ?? '?unType';
163+
gStyle.GOBJStyle style = gStyle.GOBJStyle.apply(cmd.type);
164+
cmd.style = style;
165+
structure.addStep(cmd);
161166
} catch (e, stackTrace) {
162167
Exception('错误: $e');
163168
Exception('堆栈跟踪: $stackTrace');
164169
}
165170
} else if (line.startsWith('#')) {
166-
gStyle.GOBJStyle style = styleCompiler(line);
167171
String label = subStringBetween(line, '#', ' ').trim();
172+
gStyle.GOBJStyle? oldStyle = structure.step[label]?.style;
173+
gStyle.GOBJStyle style = styleCompiler(oldStyle!, line);
174+
168175
structure.step[label]?.style = style;
169176
if (kDebugMode) {
170177
print('set style<$label>:${style.toString()}');
@@ -241,20 +248,26 @@ String gmkCommand2Str(GMKCommand gc) {
241248
}
242249

243250

244-
gStyle.GOBJStyle styleCompiler(String sCode) {
245-
gStyle.GOBJStyle result = gStyle.GOBJStyle.none();
251+
gStyle.GOBJStyle styleCompiler(gStyle.GOBJStyle oldStyle, String sCode) {
252+
gStyle.GOBJStyle result = oldStyle;
246253
String label = subStringBetween(sCode, '#', ' ').trim();
247254
String sFactor = extractAfter(sCode, '#$label');
248255
List<dynamic> factor = str2Factor(sFactor);
249256
for (int i = 0; i < factor.length; i++) {
250257
dynamic itemFactor = factor[i];
251-
print(itemFactor.runtimeType);
258+
//print(itemFactor.runtimeType);
252259
if (gStyle.color.contains(itemFactor)) {
253260
result.color = gStyle.colors[itemFactor]!;
254261
} else if (gStyle.shape.contains(itemFactor)) {
255262
result.shape = itemFactor;
256263
} else if (itemFactor.runtimeType == double || itemFactor.runtimeType == int) {
257264
result.size = itemFactor;
265+
} else if (gStyle.show.contains(itemFactor)) {
266+
result.show = (itemFactor == 'show');
267+
} else if (gStyle.labelShow.contains(itemFactor)) {
268+
result.labelShow = (itemFactor == 'labelShow');
269+
} else if (gStyle.fertileWaveLink.contains(itemFactor)) {
270+
result.fertileWaveLink = (itemFactor == 'fertileWaveLink');
258271
}
259272
}
260273
return result;

lib/MathForest/Geometry/D2/GMK/Core/GMKCore.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class GMKCore {
6363
String label = itemCommand.label;
6464
gStyle.GOBJStyle style = itemCommand.style;
6565
var (obj, type) = lib.analysis(itemCommand, gmkData);
66-
itemCommand.type = type;
66+
//itemCommand.type = type;
67+
// 改为编译时生成type
6768
if (obj == null) {
6869
if (type == 'e-findMethod:none') {
6970
Exception(

0 commit comments

Comments
 (0)