feat: 全面迁移到 Flutter + 新增 GitHub 源与 CI 流水线
前端
- 移除全部 Avalonia 依赖,模型渲染与界面统一改用 Flutter
- 新增用户页:登录/注册(SHA-512 + 盐)、签到积分(30 天一循环)、
主题色积分购买、二选一随机、今日人品
- 新增百宝箱页、模型内嵌渲染页、自绘标题栏(最小化/最大化/关闭)
- 下载页显示模型下载进度条,下载源读取加速
- 关于页新增「重新下载启动器」并在下载后做 sha256 完整性校验
- 修复浅色主题下标题栏不跟随主题色、无法连接后端等问题
后端
- 新增 UserService:.GC/user/<用户名>.yaml 存用户名/积分/SHA-512 口令,
登录返回 session token,本地 .GC/user/user.yaml 保存登录态
- 新增 /api/user/{register,login,me,signin,theme} 接口
- 去掉 Avalonia 窗口宿主,后端只提供 HTTP API
下载源
- 新增 GitHub 镜像源(Morris-morris-cloud/GC),与 Xhmcl 并发测速
- 单源探测超时收紧到 2 秒,取最快且可用的源;GitHub 复用 GiteaRemote
(contents 带 ref、releases 带 per_page=100)
构建与 CI
- macOS 上默认只出 macOS 包,Windows 交给原生 Windows 构建
- 修复 Windows 交叉编译中 flutter-cpp-client-wrapper 双层目录嵌套
- 新增 GitHub Actions:
· nightly-prerelease:每天 21:00 编译三平台,发布 nightly 预发布
· build-bot:Issue/PR 评论 @build-bot build <mac|win-amd|win-arm|all>,
发布 ip-prerelease 预发布
· pr-review:检查能否编译、静态分析、合并冲突、是否落后 main
- 忽略 79MB 课件产物等高线.html 与工具链临时备份
This commit is contained in:
1 parent
b9cb56cb8b
commit
ea1fa6b6d1
134 files changed
+4309
-16614
No files matched your search
@@ -25,6 +25,6 @@
|
||||
"button": "#9FB4D4",
|
||||
"frame": "#0A1524"
|
||||
},
|
||||
"build": "773ab2161d",
|
||||
"updatedAt": "2026-11-19T10:20:00Z"
|
||||
"build": "d18eb85c8d",
|
||||
"updatedAt": "2026-11-21T10:55:36Z"
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<RootNamespace>GC.ContourMap</RootNamespace>
|
||||
<AssemblyName>ContourMapModel</AssemblyName>
|
||||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\_sdk\GC.ModelSdk\GC.ModelSdk.csproj">
|
||||
<Private>false</Private>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -1,96 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
using GC.ModelSdk;
|
||||
|
||||
namespace GC.ContourMap;
|
||||
|
||||
/// <summary>等高线模型入口(清单里的 type 指向这里)。</summary>
|
||||
public sealed class ContourMapFactory : IModelFactory
|
||||
{
|
||||
public int ApiVersion => 1;
|
||||
public IModelRuntime Create() => new ContourMapRuntime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 等高线模型运行时。
|
||||
/// 依据人教版七年级上册「地形图的判读」:海拔与相对高度、等高线与等高距、
|
||||
/// 山峰/山脊/山谷/鞍部/陡崖的判读、坡度陡缓与等高线疏密的关系。
|
||||
/// </summary>
|
||||
public sealed class ContourMapRuntime : IModelRuntime
|
||||
{
|
||||
private TerrainField? _field;
|
||||
private Dictionary<string, string>? _texts;
|
||||
private ContourPage? _page;
|
||||
|
||||
public void Prepare(IModelContext context)
|
||||
{
|
||||
context.ReportProgress(0.15, "生成地形高度场");
|
||||
_field = new TerrainField(TerrainKind.Mountain, 192);
|
||||
|
||||
context.ReportProgress(0.70, "提取等高线");
|
||||
_texts = LoadTexts(context.DataDirectory, context.Language);
|
||||
|
||||
context.ReportProgress(0.92, "准备界面");
|
||||
context.Log($"[contour-map] 地形 {_field.Size}×{_field.Size}," +
|
||||
$"海拔 {_field.MinHeight:0}~{_field.MaxHeight:0} m,等高距 {_field.EffectiveInterval:0} m");
|
||||
}
|
||||
|
||||
public Control CreateView(IModelContext context)
|
||||
{
|
||||
_texts ??= LoadTexts(context.DataDirectory, context.Language);
|
||||
// 实验地形 + 五大地貌都在界面里现场生成(切换很快,不必预先生成全部)
|
||||
_page = new ContourPage(_texts);
|
||||
return _page.Build();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_page?.Dispose();
|
||||
_page = null;
|
||||
_field = null;
|
||||
_texts = null;
|
||||
System.GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 文案 = 内置默认(保证任何语言都有词)+ dataDir/i18n/<lang>.json 覆盖。
|
||||
/// 语言包缺失或某条没翻译时自动退回内置默认,不会露出裸 key。
|
||||
/// </summary>
|
||||
private static Dictionary<string, string> LoadTexts(string dataDir, string language)
|
||||
{
|
||||
var isEn = language.StartsWith("en", StringComparison.OrdinalIgnoreCase);
|
||||
var map = new Dictionary<string, string>(DefaultTexts.Zh);
|
||||
if (isEn) foreach (var kv in DefaultTexts.En) map[kv.Key] = kv.Value;
|
||||
|
||||
var candidates = new List<string>();
|
||||
if (!string.IsNullOrWhiteSpace(language)) candidates.Add(language);
|
||||
candidates.Add(isEn ? "en" : "zh-CN");
|
||||
|
||||
foreach (var lang in candidates)
|
||||
{
|
||||
var path = System.IO.Path.Combine(dataDir, "i18n", lang + ".json");
|
||||
if (!System.IO.File.Exists(path)) continue;
|
||||
try
|
||||
{
|
||||
using var doc = System.Text.Json.JsonDocument.Parse(System.IO.File.ReadAllText(path));
|
||||
if (doc.RootElement.ValueKind != System.Text.Json.JsonValueKind.Object) continue;
|
||||
foreach (var p in doc.RootElement.EnumerateObject())
|
||||
{
|
||||
if (p.Value.ValueKind != System.Text.Json.JsonValueKind.String) continue;
|
||||
var v = p.Value.GetString();
|
||||
if (!string.IsNullOrWhiteSpace(v)) map[p.Name] = v!;
|
||||
}
|
||||
break;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 语言包损坏不影响开课,继续用内置默认
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
}
|
||||
@@ -1,721 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Media;
|
||||
|
||||
namespace GC.ContourMap;
|
||||
|
||||
/// <summary>
|
||||
/// 等高线模型界面(完整复刻 HTML 课件功能):
|
||||
/// 顶部标题 + 全屏;左侧可交互 3D 画布;右侧实验流程/五大地貌/判读要点观察面板;
|
||||
/// 底部播放控制、俯视切换、上色开关、实时读数;支持键盘 1-5 / 空格快捷键。
|
||||
/// </summary>
|
||||
public sealed class ContourPage
|
||||
{
|
||||
private static readonly Color CAppBg = Color.Parse("#0B1524");
|
||||
private static readonly Color CPanel = Color.Parse("#101E31");
|
||||
private static readonly Color CPanel2 = Color.Parse("#16263C");
|
||||
private static readonly Color CLine = Color.Parse("#1E3350");
|
||||
private static readonly Color CInk = Color.Parse("#EAF2FF");
|
||||
private static readonly Color CInkDim = Color.Parse("#93A8C6");
|
||||
private static readonly Color CInkFaint = Color.Parse("#6A80A0");
|
||||
private static readonly Color CAccent = Color.Parse("#4D8CFF");
|
||||
private static readonly Color CWarn = Color.Parse("#FFB454");
|
||||
private static readonly Color CGood = Color.Parse("#5FD69B");
|
||||
|
||||
private readonly Dictionary<string, string> _texts;
|
||||
private readonly Dictionary<LandformKind, TerrainField> _landformCache = new();
|
||||
private TerrainField _experimentField;
|
||||
|
||||
private ContourView? _view;
|
||||
private TextBlock? _readout;
|
||||
private TextBlock? _featureText;
|
||||
private TextBlock? _hintText;
|
||||
private TextBlock? _playLabel;
|
||||
private TextBlock? _observeHeading;
|
||||
private TextBlock? _observeSummary;
|
||||
private StackPanel? _featureList;
|
||||
private TextBlock? _viewToggleLabel;
|
||||
|
||||
private readonly List<Border> _flowButtons = new();
|
||||
private readonly List<Border> _landformButtons = new();
|
||||
private Border? _playBtn;
|
||||
private Border? _backBtn;
|
||||
private Border? _colorBtn;
|
||||
private TextBlock? _colorBtnText;
|
||||
private ProgressBar? _progress;
|
||||
|
||||
private int _step = 0;
|
||||
private LandformKind? _selectedLandform;
|
||||
private bool _disposed;
|
||||
private TopLevel? _topLevel;
|
||||
|
||||
private static readonly LandformKind[] LandformOrder =
|
||||
{ LandformKind.Peak, LandformKind.Ridge, LandformKind.Valley, LandformKind.Saddle, LandformKind.Cliff };
|
||||
|
||||
public ContourPage(Dictionary<string, string> texts)
|
||||
{
|
||||
_texts = texts;
|
||||
_experimentField = new TerrainField(TerrainKind.Mountain, 192);
|
||||
}
|
||||
|
||||
private string T(string key) => _texts.TryGetValue(key, out var v) ? v : key;
|
||||
private string T(string key, params object[] args)
|
||||
{
|
||||
var s = T(key);
|
||||
try { return string.Format(System.Globalization.CultureInfo.InvariantCulture, s, args); }
|
||||
catch { return s; }
|
||||
}
|
||||
private static SolidColorBrush B(Color c) => new(c);
|
||||
|
||||
private TerrainField GetLandformField(LandformKind kind)
|
||||
{
|
||||
if (!_landformCache.TryGetValue(kind, out var f))
|
||||
{
|
||||
f = new TerrainField(kind, 192);
|
||||
_landformCache[kind] = f;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
|
||||
public Control Build()
|
||||
{
|
||||
var root = new Grid { Background = B(CAppBg) };
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Star));
|
||||
root.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
|
||||
root.Children.Add(BuildHeader());
|
||||
|
||||
var body = new Grid { Margin = new Thickness(12, 8, 12, 6) };
|
||||
body.ColumnDefinitions.Add(new ColumnDefinition(GridLength.Star));
|
||||
body.ColumnDefinitions.Add(new ColumnDefinition(new GridLength(344)));
|
||||
body.RowDefinitions.Add(new RowDefinition(GridLength.Auto));
|
||||
body.RowDefinitions.Add(new RowDefinition(GridLength.Star));
|
||||
|
||||
_view = new ContourView(_experimentField, T);
|
||||
_view.HoverChanged += OnHover;
|
||||
_view.StepChanged += OnStepChanged;
|
||||
_view.ProgressChanged += OnProgressChanged;
|
||||
_view.PlaybackFinished += OnPlaybackFinished;
|
||||
|
||||
var canvasHolder = new Border
|
||||
{
|
||||
Background = B(Color.Parse("#08111E")),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(12),
|
||||
ClipToBounds = true,
|
||||
Child = _view
|
||||
};
|
||||
Grid.SetRow(canvasHolder, 1);
|
||||
body.Children.Add(canvasHolder);
|
||||
|
||||
// ── 五大典型地貌标签(横向,像课件的选项卡)──
|
||||
var tabs = new WrapPanel
|
||||
{
|
||||
Orientation = Orientation.Horizontal,
|
||||
Margin = new Thickness(0, 0, 0, 8)
|
||||
};
|
||||
_backBtn = MakePillButton(T("btn.backExperiment"), CAccent);
|
||||
_backBtn.IsVisible = false;
|
||||
_backBtn.PointerPressed += (_, _) => BackToExperiment();
|
||||
tabs.Children.Add(_backBtn);
|
||||
foreach (var kind in LandformOrder)
|
||||
{
|
||||
var btn = MakeLandformButton(kind);
|
||||
_landformButtons.Add(btn);
|
||||
tabs.Children.Add(btn);
|
||||
}
|
||||
body.Children.Add(tabs);
|
||||
|
||||
var side = BuildSidePanel();
|
||||
Grid.SetColumn(side, 1);
|
||||
Grid.SetRow(side, 0);
|
||||
Grid.SetRowSpan(side, 2);
|
||||
body.Children.Add(side);
|
||||
|
||||
// 窄窗口(封面缩略图、小屏)隐藏右侧栏,画布优先;宽窗口才展示流程与观察面板
|
||||
void ApplyResponsive(double width)
|
||||
{
|
||||
var narrow = width < 980;
|
||||
side.IsVisible = !narrow;
|
||||
body.ColumnDefinitions[1].Width = narrow ? new GridLength(0) : new GridLength(344);
|
||||
}
|
||||
root.SizeChanged += (_, e) => ApplyResponsive(e.NewSize.Width);
|
||||
|
||||
Grid.SetRow(body, 1);
|
||||
root.Children.Add(body);
|
||||
|
||||
var footer = BuildFooter();
|
||||
Grid.SetRow(footer, 2);
|
||||
root.Children.Add(footer);
|
||||
|
||||
root.AttachedToVisualTree += (_, _) =>
|
||||
{
|
||||
_topLevel = TopLevel.GetTopLevel(root);
|
||||
};
|
||||
root.KeyDown += OnKeyDown;
|
||||
root.Focusable = true;
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
/// <summary>窗口关闭时释放视图计时器,避免持有控件。</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_view?.DisposeView();
|
||||
}
|
||||
|
||||
private Control BuildHeader()
|
||||
{
|
||||
var title = new TextBlock
|
||||
{
|
||||
Text = T("app.title"),
|
||||
Foreground = B(CInk),
|
||||
FontSize = 18,
|
||||
FontWeight = FontWeight.SemiBold
|
||||
};
|
||||
var sub = new TextBlock
|
||||
{
|
||||
Text = T("app.subtitle"),
|
||||
Foreground = B(CInkDim),
|
||||
FontSize = 11.5
|
||||
};
|
||||
var stack = new StackPanel { Spacing = 2 };
|
||||
stack.Children.Add(title);
|
||||
stack.Children.Add(sub);
|
||||
|
||||
var fsLabel = new TextBlock { Text = T("btn.fullscreen"), Foreground = B(CInk), FontSize = 11.5 };
|
||||
var fsBtn = new Border
|
||||
{
|
||||
Background = B(CPanel2),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(12, 6),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = fsLabel
|
||||
};
|
||||
fsBtn.PointerPressed += (_, _) => ToggleFullscreen(fsLabel);
|
||||
|
||||
var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
|
||||
grid.Children.Add(stack);
|
||||
Grid.SetColumn(fsBtn, 1);
|
||||
grid.Children.Add(fsBtn);
|
||||
|
||||
return new Border
|
||||
{
|
||||
Background = B(CPanel),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(0, 0, 0, 1),
|
||||
Padding = new Thickness(18, 11, 18, 12),
|
||||
Child = grid
|
||||
};
|
||||
}
|
||||
|
||||
private Control BuildSidePanel()
|
||||
{
|
||||
var panel = new StackPanel { Spacing = 8, Margin = new Thickness(12, 0, 0, 0) };
|
||||
|
||||
// ── 实验流程四步 ──
|
||||
panel.Children.Add(Section(T("flow.title")));
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
var btn = MakeFlowButton(i);
|
||||
_flowButtons.Add(btn);
|
||||
panel.Children.Add(btn);
|
||||
}
|
||||
|
||||
panel.Children.Add(Divider());
|
||||
|
||||
// ── 观察面板 ──
|
||||
_observeHeading = new TextBlock
|
||||
{
|
||||
Text = T("observe.title"),
|
||||
Foreground = B(CWarn),
|
||||
FontSize = 12.5,
|
||||
FontWeight = FontWeight.SemiBold
|
||||
};
|
||||
_observeSummary = new TextBlock
|
||||
{
|
||||
Text = T("observe.default"),
|
||||
Foreground = B(CInkDim),
|
||||
FontSize = 10.5,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
LineHeight = 16
|
||||
};
|
||||
_featureList = new StackPanel { Spacing = 3 };
|
||||
RenderObservation(null);
|
||||
|
||||
panel.Children.Add(_observeHeading);
|
||||
panel.Children.Add(_observeSummary);
|
||||
panel.Children.Add(_featureList);
|
||||
|
||||
// ── 判读要点 ──
|
||||
panel.Children.Add(Section(T("side.rules")));
|
||||
panel.Children.Add(Bullet(T("rule.interval")));
|
||||
panel.Children.Add(Bullet(T("rule.dense")));
|
||||
panel.Children.Add(Bullet(T("rule.index")));
|
||||
panel.Children.Add(Bullet(T("rule.height")));
|
||||
|
||||
var scroll = new ScrollViewer
|
||||
{
|
||||
Content = new Border
|
||||
{
|
||||
Background = B(CPanel),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(12),
|
||||
Padding = new Thickness(14, 12),
|
||||
Child = panel
|
||||
},
|
||||
VerticalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Auto
|
||||
};
|
||||
return scroll;
|
||||
}
|
||||
|
||||
private Border MakeFlowButton(int index)
|
||||
{
|
||||
var name = new TextBlock
|
||||
{
|
||||
Text = $"{index + 1}. {T("flow.step." + index)}",
|
||||
Foreground = B(CInk),
|
||||
FontSize = 12.5,
|
||||
FontWeight = FontWeight.SemiBold
|
||||
};
|
||||
var desc = new TextBlock
|
||||
{
|
||||
Text = T("flow.desc." + index),
|
||||
Foreground = B(CInkFaint),
|
||||
FontSize = 11,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
LineHeight = 15
|
||||
};
|
||||
var stack = new StackPanel { Spacing = 1 };
|
||||
stack.Children.Add(name);
|
||||
stack.Children.Add(desc);
|
||||
var border = new Border
|
||||
{
|
||||
Background = B(CPanel2),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(9, 6),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = stack,
|
||||
Tag = index
|
||||
};
|
||||
border.PointerPressed += (_, _) =>
|
||||
{
|
||||
BackToExperiment();
|
||||
_view?.SetStep((ExperimentStep)index);
|
||||
UpdateFlowStyles();
|
||||
};
|
||||
return border;
|
||||
}
|
||||
|
||||
private Border MakeLandformButton(LandformKind kind)
|
||||
{
|
||||
var key = kind.ToString().ToLowerInvariant();
|
||||
var name = new TextBlock
|
||||
{
|
||||
Text = T("landform." + key),
|
||||
Foreground = B(CInk),
|
||||
FontSize = 12.5,
|
||||
FontWeight = FontWeight.SemiBold
|
||||
};
|
||||
var sub = new TextBlock
|
||||
{
|
||||
Text = T("landform." + key + ".sub"),
|
||||
Foreground = B(CInkFaint),
|
||||
FontSize = 10.5,
|
||||
VerticalAlignment = VerticalAlignment.Center
|
||||
};
|
||||
var stack = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 7 };
|
||||
stack.Children.Add(name);
|
||||
stack.Children.Add(sub);
|
||||
var border = new Border
|
||||
{
|
||||
Background = B(CPanel2),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(18),
|
||||
Padding = new Thickness(14, 7),
|
||||
Margin = new Thickness(0, 0, 8, 4),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = stack,
|
||||
Tag = kind
|
||||
};
|
||||
border.PointerPressed += (_, _) => SelectLandform(kind);
|
||||
return border;
|
||||
}
|
||||
|
||||
private void SelectLandform(LandformKind kind)
|
||||
{
|
||||
_selectedLandform = kind;
|
||||
_view?.Pause();
|
||||
_view?.SetField(GetLandformField(kind));
|
||||
_view?.SetMode(ViewMode.Perspective);
|
||||
_view?.ShowContourPattern(); // 地貌专题先看完整的等高线形态
|
||||
UpdateLandformStyles();
|
||||
UpdateViewToggleLabel();
|
||||
|
||||
var key = kind.ToString().ToLowerInvariant();
|
||||
if (_observeHeading != null) _observeHeading.Text = T("landform." + key) + " · " + T("landform." + key + ".sub");
|
||||
if (_observeSummary != null) _observeSummary.Text = T("landform." + key + ".summary");
|
||||
RenderFeatureList(key);
|
||||
if (_backBtn != null) _backBtn.IsVisible = true;
|
||||
if (_hintText != null) _hintText.Text = T("landform." + key + ".hint");
|
||||
SetPlayLabel(T("btn.replay"));
|
||||
}
|
||||
|
||||
private void BackToExperiment()
|
||||
{
|
||||
if (_selectedLandform == null) return;
|
||||
_selectedLandform = null;
|
||||
_view?.SetField(_experimentField);
|
||||
_view?.SetMode(ViewMode.Perspective);
|
||||
_view?.Reset();
|
||||
UpdateLandformStyles();
|
||||
UpdateViewToggleLabel();
|
||||
RenderObservation(null);
|
||||
if (_backBtn != null) _backBtn.IsVisible = false;
|
||||
if (_hintText != null) _hintText.Text = T("hint.play");
|
||||
SetPlayLabel(T("btn.play"));
|
||||
}
|
||||
|
||||
private void RenderObservation(LandformKind? kind)
|
||||
{
|
||||
if (_observeHeading == null || _observeSummary == null || _featureList == null) return;
|
||||
if (kind == null)
|
||||
{
|
||||
_observeHeading.Text = T("observe.title");
|
||||
_observeSummary.Text = T("observe.default");
|
||||
_featureList.Children.Clear();
|
||||
_featureList.Children.Add(Bullet(T("observe.default.f1")));
|
||||
_featureList.Children.Add(Bullet(T("observe.default.f2")));
|
||||
}
|
||||
else
|
||||
{
|
||||
var key = kind.Value.ToString().ToLowerInvariant();
|
||||
_observeHeading.Text = T("landform." + key) + " · " + T("landform." + key + ".sub");
|
||||
_observeSummary.Text = T("landform." + key + ".summary");
|
||||
RenderFeatureList(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void RenderFeatureList(string key)
|
||||
{
|
||||
if (_featureList == null) return;
|
||||
_featureList.Children.Clear();
|
||||
for (var i = 1; i <= 3; i++)
|
||||
{
|
||||
var k = $"landform.{key}.f{i}";
|
||||
if (_texts.ContainsKey(k)) _featureList.Children.Add(Bullet(T(k)));
|
||||
}
|
||||
}
|
||||
|
||||
private Control BuildFooter()
|
||||
{
|
||||
// 播放 / 重置
|
||||
_playLabel = new TextBlock { Text = T("btn.play"), Foreground = B(Colors.White), FontSize = 11.5, FontWeight = FontWeight.SemiBold };
|
||||
_playBtn = new Border
|
||||
{
|
||||
Background = B(CAccent),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(14, 7),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = _playLabel
|
||||
};
|
||||
_playBtn.PointerPressed += (_, _) => TogglePlay();
|
||||
|
||||
var resetBtn = MakePillButton(T("btn.reset"), Color.Parse("#2A3B55"));
|
||||
resetBtn.PointerPressed += (_, _) =>
|
||||
{
|
||||
BackToExperiment();
|
||||
_view?.Reset();
|
||||
if (_progress != null) _progress.Value = 0;
|
||||
if (_hintText != null) _hintText.Text = T("hint.play");
|
||||
SetPlayLabel(T("btn.play"));
|
||||
};
|
||||
|
||||
// 俯视 / 立体切换
|
||||
_viewToggleLabel = new TextBlock { Text = T("btn.viewTop"), Foreground = B(CInk), FontSize = 11.5 };
|
||||
var viewBtn = new Border
|
||||
{
|
||||
Background = B(CPanel2),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(1),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(11, 6),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = _viewToggleLabel
|
||||
};
|
||||
viewBtn.PointerPressed += (_, _) =>
|
||||
{
|
||||
_view?.ToggleMode();
|
||||
UpdateViewToggleLabel();
|
||||
};
|
||||
|
||||
// 上色开关
|
||||
_colorBtnText = new TextBlock { Text = T("btn.colorOn"), Foreground = B(Colors.White), FontSize = 11.5 };
|
||||
_colorBtn = new Border
|
||||
{
|
||||
Background = B(CAccent),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(11, 6),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = _colorBtnText
|
||||
};
|
||||
_colorBtn.PointerPressed += (_, _) => ToggleColor();
|
||||
|
||||
var controls = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8 };
|
||||
controls.Children.Add(_playBtn);
|
||||
controls.Children.Add(resetBtn);
|
||||
controls.Children.Add(viewBtn);
|
||||
controls.Children.Add(_colorBtn);
|
||||
|
||||
_progress = new ProgressBar
|
||||
{
|
||||
Minimum = 0,
|
||||
Maximum = 100,
|
||||
Value = 0,
|
||||
Height = 4,
|
||||
VerticalAlignment = VerticalAlignment.Center,
|
||||
Foreground = B(CAccent),
|
||||
Background = B(Color.Parse("#1E3350"))
|
||||
};
|
||||
|
||||
_hintText = new TextBlock { Text = T("hint.play"), Foreground = B(CInkDim), FontSize = 11.5 };
|
||||
|
||||
_readout = new TextBlock { Text = T("footer.hint"), Foreground = B(CInk), FontSize = 11.5 };
|
||||
_featureText = new TextBlock
|
||||
{
|
||||
Text = "",
|
||||
Foreground = B(CGood),
|
||||
FontSize = 11.5,
|
||||
FontWeight = FontWeight.SemiBold,
|
||||
HorizontalAlignment = HorizontalAlignment.Right
|
||||
};
|
||||
|
||||
var info = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
|
||||
info.Children.Add(_readout);
|
||||
Grid.SetColumn(_featureText, 1);
|
||||
info.Children.Add(_featureText);
|
||||
|
||||
var leftStack = new StackPanel { Spacing = 4, VerticalAlignment = VerticalAlignment.Center };
|
||||
leftStack.Children.Add(_hintText);
|
||||
leftStack.Children.Add(info);
|
||||
|
||||
var progStack = new StackPanel { Spacing = 6, VerticalAlignment = VerticalAlignment.Center };
|
||||
progStack.Children.Add(leftStack);
|
||||
progStack.Children.Add(_progress);
|
||||
|
||||
var row = new Grid { ColumnDefinitions = new ColumnDefinitions("*,Auto") };
|
||||
row.Children.Add(progStack);
|
||||
Grid.SetColumn(controls, 1);
|
||||
row.Children.Add(controls);
|
||||
|
||||
return new Border
|
||||
{
|
||||
Background = B(CPanel),
|
||||
BorderBrush = B(CLine),
|
||||
BorderThickness = new Thickness(0, 1, 0, 0),
|
||||
Padding = new Thickness(16, 9),
|
||||
Child = row
|
||||
};
|
||||
}
|
||||
|
||||
private void TogglePlay()
|
||||
{
|
||||
if (_view == null) return;
|
||||
if (_selectedLandform != null) BackToExperiment();
|
||||
_view.TogglePlay();
|
||||
SetPlayLabel(_view.IsPlaying ? T("btn.pause") : T("btn.play"));
|
||||
if (_hintText != null)
|
||||
_hintText.Text = _view.IsPlaying ? T("hint.playing") : T("hint.paused");
|
||||
}
|
||||
|
||||
private void ToggleColor()
|
||||
{
|
||||
if (_view == null) return;
|
||||
_view.Colored = !_view.Colored;
|
||||
if (_colorBtn != null) _colorBtn.Background = B(_view.Colored ? CAccent : Color.Parse("#2A3B55"));
|
||||
if (_colorBtnText != null) _colorBtnText.Text = _view.Colored ? T("btn.colorOn") : T("btn.color");
|
||||
}
|
||||
|
||||
private void SetPlayLabel(string text) { if (_playLabel != null) _playLabel.Text = text; }
|
||||
|
||||
private void UpdateViewToggleLabel()
|
||||
{
|
||||
if (_viewToggleLabel == null || _view == null) return;
|
||||
_viewToggleLabel.Text = _view.Mode == ViewMode.Top ? T("btn.view3d") : T("btn.viewTop");
|
||||
}
|
||||
|
||||
private void UpdateFlowStyles()
|
||||
{
|
||||
for (var i = 0; i < _flowButtons.Count; i++)
|
||||
{
|
||||
var b = _flowButtons[i];
|
||||
var active = i == _step && _selectedLandform == null;
|
||||
var done = i < _step && _selectedLandform == null;
|
||||
StylePill(b, active ? CAccent : done ? Color.Parse("#1C3A5E") : CPanel2,
|
||||
active ? Colors.White : done ? CGood : CInkDim);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLandformStyles()
|
||||
{
|
||||
for (var i = 0; i < _landformButtons.Count; i++)
|
||||
{
|
||||
var b = _landformButtons[i];
|
||||
var kind = LandformOrder[i];
|
||||
var active = _selectedLandform == kind;
|
||||
StylePill(b, active ? CAccent : CPanel2, active ? Colors.White : CInkDim);
|
||||
}
|
||||
}
|
||||
|
||||
private static void StylePill(Border b, Color bg, Color fg)
|
||||
{
|
||||
b.Background = B(bg);
|
||||
if (b.Child is TextBlock tb) tb.Foreground = B(fg);
|
||||
else if (b.Child is StackPanel sp)
|
||||
foreach (var c in sp.Children)
|
||||
if (c is TextBlock t) t.Foreground = B(fg);
|
||||
}
|
||||
|
||||
private void OnStepChanged(ExperimentStep step)
|
||||
{
|
||||
_step = (int)step;
|
||||
UpdateFlowStyles();
|
||||
if (_selectedLandform == null && _observeSummary != null)
|
||||
_observeSummary.Text = T("flow.desc." + _step);
|
||||
}
|
||||
|
||||
private void OnProgressChanged(ExperimentStep step, double value)
|
||||
=> _progress?.Value = value * 100;
|
||||
|
||||
private void OnPlaybackFinished()
|
||||
{
|
||||
SetPlayLabel(T("btn.replay"));
|
||||
if (_hintText != null) _hintText.Text = T("hint.done");
|
||||
}
|
||||
|
||||
private void OnHover(double u, double v, float height, TerrainFeature feature)
|
||||
{
|
||||
if (_readout == null || _featureText == null) return;
|
||||
var field = _view?.Field ?? _experimentField;
|
||||
var relative = height - field.MinHeight;
|
||||
_readout.Text = T("footer.readout", (int)height, (int)relative);
|
||||
_featureText.Text = FeatureName(feature);
|
||||
_featureText.Foreground = B(feature switch
|
||||
{
|
||||
TerrainFeature.Peak => Color.Parse("#FFD166"),
|
||||
TerrainFeature.Cliff => Color.Parse("#FF7B7B"),
|
||||
TerrainFeature.Saddle => Color.Parse("#C08CFF"),
|
||||
TerrainFeature.Ridge => Color.Parse("#5FD69B"),
|
||||
TerrainFeature.Valley => Color.Parse("#5BC8FF"),
|
||||
_ => CInkDim
|
||||
});
|
||||
}
|
||||
|
||||
private string FeatureName(TerrainFeature f) => f switch
|
||||
{
|
||||
TerrainFeature.Peak => T("feature.peak"),
|
||||
TerrainFeature.Ridge => T("feature.ridge"),
|
||||
TerrainFeature.Valley => T("feature.valley"),
|
||||
TerrainFeature.Saddle => T("feature.saddle"),
|
||||
TerrainFeature.Cliff => T("feature.cliff"),
|
||||
TerrainFeature.Slope => T("feature.slope"),
|
||||
_ => ""
|
||||
};
|
||||
|
||||
private void OnKeyDown(object? sender, KeyEventArgs e)
|
||||
{
|
||||
// 空格:播放/暂停
|
||||
if (e.Key == Key.Space)
|
||||
{
|
||||
e.Handled = true;
|
||||
TogglePlay();
|
||||
return;
|
||||
}
|
||||
// 1-5:切换五大地貌
|
||||
var idx = -1;
|
||||
switch (e.Key)
|
||||
{
|
||||
case Key.D1: case Key.NumPad1: idx = 0; break;
|
||||
case Key.D2: case Key.NumPad2: idx = 1; break;
|
||||
case Key.D3: case Key.NumPad3: idx = 2; break;
|
||||
case Key.D4: case Key.NumPad4: idx = 3; break;
|
||||
case Key.D5: case Key.NumPad5: idx = 4; break;
|
||||
}
|
||||
if (idx >= 0 && idx < LandformOrder.Length)
|
||||
{
|
||||
e.Handled = true;
|
||||
SelectLandform(LandformOrder[idx]);
|
||||
}
|
||||
}
|
||||
|
||||
private void ToggleFullscreen(TextBlock label)
|
||||
{
|
||||
if (_topLevel is not Window window) return;
|
||||
if (window.WindowState == WindowState.FullScreen)
|
||||
{
|
||||
window.WindowState = WindowState.Normal;
|
||||
label.Text = T("btn.fullscreen");
|
||||
}
|
||||
else
|
||||
{
|
||||
window.WindowState = WindowState.FullScreen;
|
||||
label.Text = T("btn.exitFullscreen");
|
||||
}
|
||||
}
|
||||
|
||||
// ── 控件工厂
|
||||
|
||||
private static TextBlock Section(string text) => new()
|
||||
{
|
||||
Text = text,
|
||||
Foreground = BConst(CInk),
|
||||
FontSize = 12.5,
|
||||
FontWeight = FontWeight.SemiBold,
|
||||
Margin = new Thickness(0, 2, 0, 2)
|
||||
};
|
||||
|
||||
private static Border Divider() => new()
|
||||
{
|
||||
Height = 1,
|
||||
Background = BConst(CLine),
|
||||
Margin = new Thickness(0, 4, 0, 2)
|
||||
};
|
||||
|
||||
private static TextBlock Bullet(string text) => new()
|
||||
{
|
||||
Text = "• " + text,
|
||||
Foreground = BConst(CInkDim),
|
||||
FontSize = 11.5,
|
||||
TextWrapping = TextWrapping.Wrap,
|
||||
LineHeight = 17
|
||||
};
|
||||
|
||||
private Border MakePillButton(string label, Color bg)
|
||||
{
|
||||
var tb = new TextBlock { Text = label, Foreground = B(Colors.White), FontSize = 11.5, HorizontalAlignment = HorizontalAlignment.Center };
|
||||
return new Border
|
||||
{
|
||||
Background = B(bg),
|
||||
CornerRadius = new CornerRadius(8),
|
||||
Padding = new Thickness(11, 6),
|
||||
Cursor = new Avalonia.Input.Cursor(Avalonia.Input.StandardCursorType.Hand),
|
||||
Child = tb
|
||||
};
|
||||
}
|
||||
|
||||
private static SolidColorBrush BConst(Color c) => new(c);
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,225 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GC.ContourMap;
|
||||
|
||||
/// <summary>内置全量文案兜底:没有语言包也能完整显示(复刻 HTML 课件全部文案)。</summary>
|
||||
internal static class DefaultTexts
|
||||
{
|
||||
public static readonly Dictionary<string, string> Zh = new()
|
||||
{
|
||||
// 标题
|
||||
["app.title"] = "等高线地形 · 五大典型地貌判读",
|
||||
["app.subtitle"] = "七年级上册 · 从立体地形到平面等高线,判读山峰/山脊/山谷/鞍部/陡崖",
|
||||
|
||||
// 实验流程四步
|
||||
["flow.title"] = "实验流程",
|
||||
["flow.step.0"] = "等高切割",
|
||||
["flow.step.1"] = "形成交线",
|
||||
["flow.step.2"] = "投影成图",
|
||||
["flow.step.3"] = "疏密判坡",
|
||||
["flow.desc.0"] = "观察水平面与山体在同一海拔相交,形成连续闭合线。",
|
||||
["flow.desc.1"] = "每个水平切面都会留下同高交线,多层交线共同描述山体起伏。",
|
||||
["flow.desc.2"] = "把各高度交线垂直投影到同一平面,得到完整等高线地形图。",
|
||||
["flow.desc.3"] = "等高线越密,坡度越陡;等高线越疏,坡度越缓。",
|
||||
|
||||
// 观察面板
|
||||
["observe.title"] = "实验演示",
|
||||
["observe.default"] = "点击开始实验,在立体地形中观察等高切割与交线形成,再通过四步图解理解投影与判坡。",
|
||||
["observe.features"] = "判读要点",
|
||||
["observe.default.f1"] = "同一条等高线上海拔相等",
|
||||
["observe.default.f2"] = "相邻等高线高差保持一致",
|
||||
|
||||
// 五大典型地貌
|
||||
["side.landforms"] = "五大典型地貌",
|
||||
["landform.peak"] = "山峰",
|
||||
["landform.ridge"] = "山脊",
|
||||
["landform.valley"] = "山谷",
|
||||
["landform.saddle"] = "鞍部",
|
||||
["landform.cliff"] = "陡崖",
|
||||
["landform.peak.sub"] = "闭合等高线",
|
||||
["landform.ridge.sub"] = "凸低为脊",
|
||||
["landform.valley.sub"] = "凸高为谷",
|
||||
["landform.saddle.sub"] = "两峰之间",
|
||||
["landform.cliff.sub"] = "密集或重合",
|
||||
|
||||
["landform.peak.summary"] = "山峰四周较低、中心突起;等高线闭合成环,海拔数值由外向内逐渐升高。",
|
||||
["landform.ridge.summary"] = "等高线弯曲部分向海拔低处凸出,脊线常成为两侧水系的分水岭。",
|
||||
["landform.valley.summary"] = "等高线弯曲部分向海拔高处凸出,谷底低洼并容易汇水形成河流。",
|
||||
["landform.saddle.summary"] = "鞍部位于相邻两座山峰之间,是沿山脊方向相对低洼的过渡地带。",
|
||||
["landform.cliff.summary"] = "陡崖坡面近直立,不同高程的等高线会高度密集,局部可投影到同一位置而重合。",
|
||||
|
||||
["landform.peak.f1"] = "中心海拔高、四周海拔低",
|
||||
["landform.peak.f2"] = "闭合线越向内,数值越大",
|
||||
["landform.peak.f3"] = "若数值向内降低,则应判为洼地",
|
||||
["landform.ridge.f1"] = "等高线凸向低值方向",
|
||||
["landform.ridge.f2"] = "脊线由高处向低处延伸",
|
||||
["landform.ridge.f3"] = "雨水沿脊线两侧分流",
|
||||
["landform.valley.f1"] = "等高线凸向高值方向",
|
||||
["landform.valley.f2"] = "V 形尖端指向上游高处",
|
||||
["landform.valley.f3"] = "谷底常发育河流",
|
||||
["landform.saddle.f1"] = "两组闭合等高线相邻",
|
||||
["landform.saddle.f2"] = "中部线条收窄,形似马鞍",
|
||||
["landform.saddle.f3"] = "常是翻越山岭的低垭口",
|
||||
["landform.cliff.f1"] = "密集表示陡坡,重合才是陡崖特征",
|
||||
["landform.cliff.f2"] = "单位水平距离内高差很大",
|
||||
["landform.cliff.f3"] = "n 条线重合时:(n−1)d ≤ ΔH < (n+1)d",
|
||||
|
||||
["landform.peak.hint"] = "山峰:闭合曲线,数值向中心升高",
|
||||
["landform.ridge.hint"] = "山脊:等高线向低处凸出,凸低为脊",
|
||||
["landform.valley.hint"] = "山谷:等高线向高处凸出,凸高为谷",
|
||||
["landform.saddle.hint"] = "鞍部:两峰之间相对低洼,俯视轮廓呈收腰形",
|
||||
["landform.cliff.hint"] = "陡崖:等高线密集或重合,坡度极陡",
|
||||
|
||||
// 按钮
|
||||
["btn.play"] = "开始实验",
|
||||
["btn.pause"] = "暂停实验",
|
||||
["btn.replay"] = "重新实验",
|
||||
["btn.reset"] = "重置",
|
||||
["btn.view3d"] = "立体图",
|
||||
["btn.viewTop"] = "俯视图",
|
||||
["btn.color"] = "上色",
|
||||
["btn.colorOn"] = "取消上色",
|
||||
["btn.fullscreen"] = "全屏",
|
||||
["btn.exitFullscreen"] = "退出全屏",
|
||||
["btn.backExperiment"] = "返回实验",
|
||||
|
||||
// 侧栏判读要点(通用地形)
|
||||
["side.features"] = "五种地形部位",
|
||||
["feature.peak"] = "山峰",
|
||||
["feature.peakDesc"] = "等高线闭合,数值由中心向四周递减。",
|
||||
["feature.ridge"] = "山脊",
|
||||
["feature.ridgeDesc"] = "等高线凸向低处,脊线是分水线。",
|
||||
["feature.valley"] = "山谷",
|
||||
["feature.valleyDesc"] = "等高线凸向高处,谷线是集水线,常发育河流。",
|
||||
["feature.saddle"] = "鞍部",
|
||||
["feature.saddleDesc"] = "两个山峰之间的低地,形如马鞍。",
|
||||
["feature.cliff"] = "陡崖",
|
||||
["feature.cliffDesc"] = "等高线重叠或非常密集,近乎垂直。",
|
||||
["feature.slope"] = "坡面",
|
||||
|
||||
// 判读规则
|
||||
["side.rules"] = "判读要点",
|
||||
["rule.interval"] = "等高距:相邻两条等高线的高度差,本图 100 米。",
|
||||
["rule.dense"] = "等高线越密坡越陡,越疏坡越缓。",
|
||||
["rule.index"] = "每 5 条加粗一条计曲线,标注海拔便于读数。",
|
||||
["rule.height"] = "海拔:某地高出海平面的垂直距离;相对高度:两点海拔之差。",
|
||||
|
||||
// 底部读数 / 图例
|
||||
["footer.hint"] = "拖动立体地形旋转、滚轮缩放;鼠标悬停可查看该点海拔与地形部位",
|
||||
["footer.readout"] = "海拔 {0} m 相对高度 {1} m",
|
||||
["legend.title"] = "分层设色",
|
||||
["profile.title"] = "地形剖面图",
|
||||
["slope.gentle"] = "缓坡(线疏)",
|
||||
["slope.steep"] = "陡坡(线密)",
|
||||
|
||||
["hint.play"] = "点击“开始实验”,观察等高切割与交线形成",
|
||||
["hint.playing"] = "实验播放中",
|
||||
["hint.paused"] = "实验已暂停 · 点击继续恢复播放",
|
||||
["hint.done"] = "实验流程播放完成 · 可重新实验或选择典型地貌观察"
|
||||
};
|
||||
|
||||
public static readonly Dictionary<string, string> En = new()
|
||||
{
|
||||
["app.title"] = "Contour Lines · Five Classic Landforms",
|
||||
["app.subtitle"] = "Grade 7 · From 3D relief to contour maps: peaks, ridges, valleys, saddles and cliffs",
|
||||
|
||||
["flow.title"] = "Experiment flow",
|
||||
["flow.step.0"] = "Contour cutting",
|
||||
["flow.step.1"] = "Forming lines",
|
||||
["flow.step.2"] = "Projection",
|
||||
["flow.step.3"] = "Slope density",
|
||||
["flow.desc.0"] = "Watch a horizontal plane intersect the mountain at one elevation, forming closed lines.",
|
||||
["flow.desc.1"] = "Each horizontal slice leaves an equal-height line; stacked lines describe the relief.",
|
||||
["flow.desc.2"] = "Project every intersection line vertically onto one plane to build the contour map.",
|
||||
["flow.desc.3"] = "Contours closer together mean a steeper slope; wider apart mean a gentler one.",
|
||||
|
||||
["observe.title"] = "Experiment demo",
|
||||
["observe.default"] = "Press play to watch contour cutting and line formation, then use the four steps to learn projection and slope reading.",
|
||||
["observe.features"] = "Key points",
|
||||
["observe.default.f1"] = "All points on one contour share the same elevation",
|
||||
["observe.default.f2"] = "Neighbouring contours keep a constant height interval",
|
||||
|
||||
["side.landforms"] = "Five classic landforms",
|
||||
["landform.peak"] = "Peak",
|
||||
["landform.ridge"] = "Ridge",
|
||||
["landform.valley"] = "Valley",
|
||||
["landform.saddle"] = "Saddle",
|
||||
["landform.cliff"] = "Cliff",
|
||||
["landform.peak.sub"] = "Closed contours",
|
||||
["landform.ridge.sub"] = "Bulges toward low ground",
|
||||
["landform.valley.sub"] = "Bulges toward high ground",
|
||||
["landform.saddle.sub"] = "Between two peaks",
|
||||
["landform.cliff.sub"] = "Dense or overlapping",
|
||||
|
||||
["landform.peak.summary"] = "A peak rises in the centre with lower ground around it; contours close into rings and values increase inward.",
|
||||
["landform.ridge.summary"] = "Contours bulge toward lower ground; a ridge line often divides two drainage systems.",
|
||||
["landform.valley.summary"] = "Contours bulge toward higher ground; valleys are low and collect water into streams.",
|
||||
["landform.saddle.summary"] = "A saddle lies between two adjacent peaks, a relatively low pass along the ridge.",
|
||||
["landform.cliff.summary"] = "A cliff face is nearly vertical, so contours crowd tightly or even overlap.",
|
||||
|
||||
["landform.peak.f1"] = "High centre, low surroundings",
|
||||
["landform.peak.f2"] = "Closed lines grow in value inward",
|
||||
["landform.peak.f3"] = "If values drop inward, it is a depression",
|
||||
["landform.ridge.f1"] = "Contours bulge toward lower values",
|
||||
["landform.ridge.f2"] = "Ridge line runs from high to low",
|
||||
["landform.ridge.f3"] = "Rain drains to either side of the ridge",
|
||||
["landform.valley.f1"] = "Contours bulge toward higher values",
|
||||
["landform.valley.f2"] = "V tip points upstream to high ground",
|
||||
["landform.valley.f3"] = "Streams often form along the valley floor",
|
||||
["landform.saddle.f1"] = "Two neighbouring closed contour groups",
|
||||
["landform.saddle.f2"] = "Contours pinch in the middle like a saddle",
|
||||
["landform.saddle.f3"] = "Often a low mountain pass",
|
||||
["landform.cliff.f1"] = "Dense means steep; overlap means cliff",
|
||||
["landform.cliff.f2"] = "Large elevation change over a short distance",
|
||||
["landform.cliff.f3"] = "n overlapping lines: (n−1)d ≤ ΔH < (n+1)d",
|
||||
|
||||
["landform.peak.hint"] = "Peak: closed curves, values rise toward the centre",
|
||||
["landform.ridge.hint"] = "Ridge: contours bulge toward low ground",
|
||||
["landform.valley.hint"] = "Valley: contours bulge toward high ground",
|
||||
["landform.saddle.hint"] = "Saddle: low ground between two peaks",
|
||||
["landform.cliff.hint"] = "Cliff: contours dense or overlapping, very steep",
|
||||
|
||||
["btn.play"] = "Start experiment",
|
||||
["btn.pause"] = "Pause experiment",
|
||||
["btn.replay"] = "Replay experiment",
|
||||
["btn.reset"] = "Reset",
|
||||
["btn.view3d"] = "3D view",
|
||||
["btn.viewTop"] = "Top view",
|
||||
["btn.color"] = "Colour",
|
||||
["btn.colorOn"] = "No colour",
|
||||
["btn.fullscreen"] = "Fullscreen",
|
||||
["btn.exitFullscreen"] = "Exit fullscreen",
|
||||
["btn.backExperiment"] = "Back to experiment",
|
||||
|
||||
["side.features"] = "Five terrain features",
|
||||
["feature.peak"] = "Peak",
|
||||
["feature.peakDesc"] = "Closed contours; values decrease outward from the centre.",
|
||||
["feature.ridge"] = "Ridge",
|
||||
["feature.ridgeDesc"] = "Contours bulge toward lower ground; a watershed line.",
|
||||
["feature.valley"] = "Valley",
|
||||
["feature.valleyDesc"] = "Contours bulge toward higher ground; a drainage line, often a stream.",
|
||||
["feature.saddle"] = "Saddle",
|
||||
["feature.saddleDesc"] = "Low ground between two peaks, shaped like a saddle.",
|
||||
["feature.cliff"] = "Cliff",
|
||||
["feature.cliffDesc"] = "Contours overlap or crowd tightly — nearly vertical.",
|
||||
["feature.slope"] = "Slope",
|
||||
|
||||
["side.rules"] = "Key rules",
|
||||
["rule.interval"] = "Contour interval: height gap between neighbours (100 m here).",
|
||||
["rule.dense"] = "Closer contours = steeper slope; wider spacing = gentler.",
|
||||
["rule.index"] = "Every 5th contour is bolded and labelled as an index contour.",
|
||||
["rule.height"] = "Elevation above sea level; relative height is the difference of two elevations.",
|
||||
|
||||
["footer.hint"] = "Drag the 3D terrain to rotate, scroll to zoom; hover to read elevation and terrain feature",
|
||||
["footer.readout"] = "Elevation {0} m Relative height {1} m",
|
||||
["legend.title"] = "Hypsometric tint",
|
||||
["profile.title"] = "Terrain profile",
|
||||
["slope.gentle"] = "Gentle (sparse lines)",
|
||||
["slope.steep"] = "Steep (dense lines)",
|
||||
|
||||
["hint.play"] = "Press start to watch contour cutting and line formation",
|
||||
["hint.playing"] = "Experiment playing",
|
||||
["hint.paused"] = "Experiment paused · press to resume",
|
||||
["hint.done"] = "Experiment finished · replay or choose a landform"
|
||||
};
|
||||
}
|
||||
@@ -1,194 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GC.ContourMap;
|
||||
|
||||
/// <summary>地形部位识别结果:课本要求学生能在等高线图上认出这五种部位。</summary>
|
||||
public enum TerrainFeature
|
||||
{
|
||||
None,
|
||||
Peak, // 山峰
|
||||
Ridge, // 山脊
|
||||
Valley, // 山谷
|
||||
Saddle, // 鞍部
|
||||
Cliff, // 陡崖
|
||||
Slope // 一般坡面
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 地形判读:根据高度场与坡度,判断任意一点属于哪种地形部位。
|
||||
/// 这是课本「等高线判读」的核心,直接从数据算出来,不靠预设标记。
|
||||
/// </summary>
|
||||
public static class TerrainAnalyzer
|
||||
{
|
||||
/// <summary>
|
||||
/// 判断某点的地形部位。
|
||||
///
|
||||
/// 先查地形自带的**标注区**(教学用,位置由地形定义保证准确),
|
||||
/// 未命中再按坡度/曲率自动推断。纯几何推断对「鞍部」「陡崖」这类
|
||||
/// 特征容易误判,教学场景必须精确。
|
||||
/// </summary>
|
||||
public static TerrainFeature Classify(TerrainField field, double u, double v)
|
||||
{
|
||||
var tagged = field.TagAt(u, v);
|
||||
if (tagged != TerrainFeature.None) return tagged;
|
||||
|
||||
var h = field.SampleSmooth(u, v);
|
||||
var grad = Gradient(field, u, v);
|
||||
var slope = Math.Sqrt(grad.dU * grad.dU + grad.dV * grad.dV); // 归一化坡度
|
||||
|
||||
// 陡崖:坡度极大(等高线挤成一团)。
|
||||
// 阈值按本图实际坡度分布标定(中位≈2.5、p99≈6.8),
|
||||
// 取 p99 以上一小段,否则大片坡面会被误判成陡崖。
|
||||
if (slope > 8.5) return TerrainFeature.Cliff;
|
||||
|
||||
// 山峰:比四周都高
|
||||
if (IsLocalMax(field, u, v)) return TerrainFeature.Peak;
|
||||
|
||||
// 鞍部:两个方向上高、另两个方向上低
|
||||
if (IsSaddle(field, u, v)) return TerrainFeature.Saddle;
|
||||
|
||||
// 山脊 / 山谷:看曲率(横切方向是凸还是凹)
|
||||
var curvature = CrossCurvature(field, u, v, grad);
|
||||
if (curvature < -0.55) return TerrainFeature.Ridge; // 向外凸
|
||||
if (curvature > 0.55) return TerrainFeature.Valley; // 向内凹
|
||||
|
||||
return TerrainFeature.Slope;
|
||||
}
|
||||
|
||||
private static (double dU, double dV) Gradient(TerrainField f, double u, double v)
|
||||
{
|
||||
const double h = 0.004;
|
||||
var du = (f.SampleSmooth(u + h, v) - f.SampleSmooth(u - h, v)) / (2 * h);
|
||||
var dv = (f.SampleSmooth(u, v + h) - f.SampleSmooth(u, v - h)) / (2 * h);
|
||||
return (du / 1000.0, dv / 1000.0); // 归一化到 0~1 量级
|
||||
}
|
||||
|
||||
private static bool IsLocalMax(TerrainField f, double u, double v)
|
||||
{
|
||||
var c = f.SampleSmooth(u, v);
|
||||
const double r = 0.035;
|
||||
foreach (var (du, dv) in new[] { (r, 0.0), (-r, 0.0), (0.0, r), (0.0, -r) })
|
||||
{
|
||||
// 越近边界越不判为峰
|
||||
if (u + du < 0 || u + du > 1 || v + dv < 0 || v + dv > 1) return false;
|
||||
if (f.SampleSmooth(u + du, v + dv) >= c - 4) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static bool IsSaddle(TerrainField f, double u, double v)
|
||||
{
|
||||
const double r = 0.05;
|
||||
if (u - r < 0 || u + r > 1 || v - r < 0 || v + r > 1) return false;
|
||||
var c = f.SampleSmooth(u, v);
|
||||
var l = f.SampleSmooth(u - r, v); var rt = f.SampleSmooth(u + r, v);
|
||||
var t = f.SampleSmooth(u, v - r); var b = f.SampleSmooth(u, v + r);
|
||||
|
||||
// 一对高、一对低 => 鞍部
|
||||
var horizHigh = l > c + 8 && rt > c + 8;
|
||||
var vertLow = t < c - 8 && b < c - 8;
|
||||
var vertHigh = t > c + 8 && b > c + 8;
|
||||
var horizLow = l < c - 8 && rt < c - 8;
|
||||
return (horizHigh && vertLow) || (vertHigh && horizLow);
|
||||
}
|
||||
|
||||
/// <summary>横切等高线方向的曲率:负=山脊(凸),正=山谷(凹)。</summary>
|
||||
private static double CrossCurvature(TerrainField f, double u, double v, (double dU, double dV) grad)
|
||||
{
|
||||
var len = Math.Sqrt(grad.dU * grad.dU + grad.dV * grad.dV);
|
||||
if (len < 1e-6) return 0;
|
||||
// 垂直于梯度方向(即沿等高线方向)的平均法向变化
|
||||
var nU = -grad.dV / len;
|
||||
var nV = grad.dU / len;
|
||||
const double r = 0.045;
|
||||
|
||||
var c = f.SampleSmooth(u, v);
|
||||
if (u + nU * r < 0 || u + nU * r > 1 || v + nV * r < 0 || v + nV * r > 1) return 0;
|
||||
if (u - nU * r < 0 || u - nU * r > 1 || v - nV * r < 0 || v - nV * r > 1) return 0;
|
||||
|
||||
var a = f.SampleSmooth(u + nU * r, v + nV * r);
|
||||
var b = f.SampleSmooth(u - nU * r, v - nV * r);
|
||||
return (a + b - 2 * c) / 1000.0 * 6.0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 教科书规范的**分层设色**:按固定高程档位套用固定色阶,档内不渐变。
|
||||
///
|
||||
/// 人教版七年级上册的分层设色色阶(自低到高):
|
||||
/// 深绿 → 浅绿 → 绿 → 浅黄 → 黄 → 浅褐 → 褐 → 浅棕 → 白
|
||||
/// 关键点:**同一档内颜色完全相同**,这样一眼就能数出有几层,
|
||||
/// 而不是课本上那种连续渐变。
|
||||
/// </summary>
|
||||
public static (byte R, byte G, byte B) ColorForHeight(float h, float min, float max)
|
||||
{
|
||||
// 按绝对高程分档(教科书就是这么画的:看的是海拔,不是相对高度)
|
||||
if (h < 200) return (0x1E, 0x6B, 0x3A); // 深绿:< 200 m
|
||||
if (h < 400) return (0x4E, 0x9E, 0x4E); // 浅绿:200–400
|
||||
if (h < 600) return (0xA8, 0xC6, 0x66); // 绿:400–600
|
||||
if (h < 800) return (0xE8, 0xE2, 0x9A); // 浅黄:600–800
|
||||
if (h < 1000) return (0xE0, 0xC0, 0x5A); // 黄:800–1000
|
||||
if (h < 1200) return (0xC8, 0x9A, 0x63); // 浅褐:1000–1200
|
||||
if (h < 1400) return (0xA8, 0x74, 0x4C); // 褐:1200–1400
|
||||
if (h < 1600) return (0xC8, 0xA8, 0x8C); // 浅棕:1400–1600
|
||||
return (0xF5, 0xF5, 0xF0); // 白:> 1600(雪线以上)
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 3D 立体视图用的连续设色:与俯视图同一套色阶,但在档位之间线性过渡。
|
||||
/// 立体视图的色块是按三角面着色的,直接用硬分档会在坡面上切出锯齿状阶梯,
|
||||
/// 看起来像渲染错误;俯视图仍保留硬分档(那才是课本的分层设色)。
|
||||
/// </summary>
|
||||
public static (byte R, byte G, byte B) ColorForHeightSmooth(float h)
|
||||
{
|
||||
// 每个档位的代表色(取档位中心),由低到高
|
||||
(byte R, byte G, byte B)[] stops =
|
||||
{
|
||||
(0x1E, 0x6B, 0x3A), (0x4E, 0x9E, 0x4E), (0xA8, 0xC6, 0x66),
|
||||
(0xE8, 0xE2, 0x9A), (0xE0, 0xC0, 0x5A), (0xC8, 0x9A, 0x63),
|
||||
(0xA8, 0x74, 0x4C), (0xC8, 0xA8, 0x8C), (0xF5, 0xF5, 0xF0)
|
||||
};
|
||||
const float band = 200f;
|
||||
var p = Math.Clamp(h / band - 0.5f, 0f, stops.Length - 1f); // 0 => 档心 100 m
|
||||
var i = (int)Math.Floor(p);
|
||||
var j = Math.Min(i + 1, stops.Length - 1);
|
||||
var t = p - i;
|
||||
t = t * t * (3 - 2 * t); // 平滑过渡
|
||||
return (Lerp(stops[i].R, stops[j].R, t),
|
||||
Lerp(stops[i].G, stops[j].G, t),
|
||||
Lerp(stops[i].B, stops[j].B, t));
|
||||
}
|
||||
|
||||
/// <summary>该颜色属于哪一档(用于图例)。</summary>
|
||||
public static string BandName(float h) => h switch
|
||||
{
|
||||
< 200 => "200 米以下",
|
||||
< 400 => "200–400 米",
|
||||
< 600 => "400–600 米",
|
||||
< 800 => "600–800 米",
|
||||
< 1000 => "800–1000 米",
|
||||
< 1200 => "1000–1200 米",
|
||||
< 1400 => "1200–1400 米",
|
||||
< 1600 => "1400–1600 米",
|
||||
_ => "1600 米以上"
|
||||
};
|
||||
|
||||
private static byte Lerp(byte a, byte b, double k)
|
||||
=> (byte)Math.Clamp(a + (b - a) * k, 0, 255);
|
||||
|
||||
/// <summary>
|
||||
/// 归一化坡度(0=平缓,1=陡峭),用于「疏密判坡」着色。
|
||||
/// 以有限差分求地面梯度,并按本模型的典型高差归一化。
|
||||
/// </summary>
|
||||
public static double SlopeAt(TerrainField f, double u, double v)
|
||||
{
|
||||
const double h = 0.006;
|
||||
var du = (f.SampleSmooth(u + h, v) - f.SampleSmooth(u - h, v)) / (2 * h);
|
||||
var dv = (f.SampleSmooth(u, v + h) - f.SampleSmooth(u, v - h)) / (2 * h);
|
||||
var grad = Math.Sqrt(du * du + dv * dv); // 米 / 归一化距离
|
||||
// 按本图实际起伏自适应归一化:跨度越大,同样的相对坡度对应更大的梯度。
|
||||
// 固定阈值会让所有山体都顶到 1(整片红),判坡就失效了。
|
||||
var span = Math.Max(60f, f.MaxHeight - f.MinHeight);
|
||||
return Math.Clamp(grad / (span * 2.6), 0, 1);
|
||||
}
|
||||
}
|
||||
@@ -1,707 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace GC.ContourMap;
|
||||
|
||||
/// <summary>五种基本地形类型(教科书分类)。</summary>
|
||||
public enum TerrainKind
|
||||
{
|
||||
Mountain, // 山地:海拔高、起伏大
|
||||
Basin, // 盆地:四周高、中间低
|
||||
Hill, // 丘陵:海拔较低、坡度和缓
|
||||
Plateau, // 高原:海拔高、地面平坦、边缘陡
|
||||
Plain // 平原:海拔低、地面平坦
|
||||
}
|
||||
|
||||
/// <summary>五大典型地貌部位(等高线判读的核心对象)。</summary>
|
||||
public enum LandformKind
|
||||
{
|
||||
Peak, // 山峰:闭合等高线,数值向内升高
|
||||
Ridge, // 山脊:等高线凸向低处,凸低为脊
|
||||
Valley, // 山谷:等高线凸向高处,凸高为谷
|
||||
Saddle, // 鞍部:两峰之间相对低洼,俯视收腰
|
||||
Cliff // 陡崖:等高线密集甚至重合
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 地形高度场(等高线模型的数据核心)。
|
||||
///
|
||||
/// 用叠加的高斯山包合成一块教学用地形:
|
||||
/// · 主峰(山峰)
|
||||
/// · 次峰(与主峰之间形成鞍部)
|
||||
/// · 两条山脊、一条山谷
|
||||
/// · 一侧陡坡(等高线密集)、一侧缓坡(等高线稀疏)
|
||||
/// · 一处陡崖
|
||||
///
|
||||
/// 高度单位米,网格分辨率可配置;网格值一次性算好,等高线由它切线得出。
|
||||
/// </summary>
|
||||
public sealed class TerrainField
|
||||
{
|
||||
/// <summary>网格边长(正方形,便于俯视与等高线处理)。</summary>
|
||||
public int Size { get; }
|
||||
|
||||
/// <summary>海拔网格,[y, x],单位米。</summary>
|
||||
public float[,] Heights { get; }
|
||||
|
||||
public float MinHeight { get; private set; }
|
||||
public float MaxHeight { get; private set; }
|
||||
|
||||
/// <summary>等高距(米):教学图常用 100 m。</summary>
|
||||
public float Interval { get; init; } = 100f;
|
||||
|
||||
/// <summary>这块地形属于哪种类型(决定界面标题与说明)。</summary>
|
||||
public TerrainKind Kind { get; }
|
||||
|
||||
/// <summary>若这块地形是某个典型地貌部位,则记录其种类;否则为 null。</summary>
|
||||
public LandformKind? Landform { get; }
|
||||
|
||||
public TerrainField(TerrainKind kind = TerrainKind.Mountain, int size = 192)
|
||||
{
|
||||
Size = size;
|
||||
Kind = kind;
|
||||
Heights = new float[size, size];
|
||||
Build(kind);
|
||||
}
|
||||
|
||||
/// <summary>创建一个专门表现某种典型地貌部位的高度场。</summary>
|
||||
public TerrainField(LandformKind landform, int size = 192)
|
||||
{
|
||||
Size = size;
|
||||
Kind = TerrainKind.Mountain;
|
||||
Landform = landform;
|
||||
Heights = new float[size, size];
|
||||
BuildLandform(landform);
|
||||
}
|
||||
|
||||
/// <summary>该类型对应的等高距:平原/丘陵用 20m,山地/高原/盆地用 100m。</summary>
|
||||
public float EffectiveInterval => Kind switch
|
||||
{
|
||||
TerrainKind.Plain => 5f, // 平原起伏小,等高距要小才画得出线
|
||||
TerrainKind.Hill => 10f, // 丘陵相对高差约 150 m
|
||||
_ => 100f
|
||||
};
|
||||
|
||||
/// <summary>归一化坐标 (u,v) ∈ [0,1],u 向东、v 向南。</summary>
|
||||
public float HeightAt(double u, double v)
|
||||
{
|
||||
var x = (int)Math.Round(u * (Size - 1));
|
||||
var y = (int)Math.Round(v * (Size - 1));
|
||||
x = Math.Clamp(x, 0, Size - 1);
|
||||
y = Math.Clamp(y, 0, Size - 1);
|
||||
return Heights[y, x];
|
||||
}
|
||||
|
||||
/// <summary>双线性插值取值,画平滑曲线时用。</summary>
|
||||
public float SampleSmooth(double u, double v)
|
||||
{
|
||||
var fx = Math.Clamp(u, 0, 1) * (Size - 1);
|
||||
var fy = Math.Clamp(v, 0, 1) * (Size - 1);
|
||||
var x0 = (int)Math.Floor(fx); var y0 = (int)Math.Floor(fy);
|
||||
var x1 = Math.Min(x0 + 1, Size - 1); var y1 = Math.Min(y0 + 1, Size - 1);
|
||||
var tx = fx - x0; var ty = fy - y0;
|
||||
var h00 = Heights[y0, x0]; var h10 = Heights[y0, x1];
|
||||
var h01 = Heights[y1, x0]; var h11 = Heights[y1, x1];
|
||||
var a = h00 * (1 - tx) + h10 * tx;
|
||||
var b = h01 * (1 - tx) + h11 * tx;
|
||||
return (float)(a * (1 - ty) + b * ty);
|
||||
}
|
||||
|
||||
private void Build(TerrainKind kind)
|
||||
{
|
||||
switch (kind)
|
||||
{
|
||||
case TerrainKind.Mountain: BuildMountain(); break;
|
||||
case TerrainKind.Basin: BuildBasin(); break;
|
||||
case TerrainKind.Hill: BuildHill(); break;
|
||||
case TerrainKind.Plateau: BuildPlateau(); break;
|
||||
case TerrainKind.Plain: BuildPlain(); break;
|
||||
}
|
||||
BuildFeatures();
|
||||
}
|
||||
|
||||
private void BuildMountain()
|
||||
{
|
||||
// 各山体参数:中心(u,v)、高度、东西半径、南北半径
|
||||
var peaks = new (double U, double V, double H, double RU, double RV)[]
|
||||
{
|
||||
(0.34, 0.34, 1180, 0.26, 0.24), // 主峰
|
||||
(0.66, 0.52, 860, 0.20, 0.20), // 次峰(与主峰间成鞍部)
|
||||
(0.22, 0.70, 520, 0.18, 0.16), // 西侧小山
|
||||
(0.78, 0.22, 430, 0.16, 0.15), // 东北小山
|
||||
};
|
||||
|
||||
var min = float.MaxValue; var max = float.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
{
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
|
||||
double h = 120; // 基准面(平原 / 河谷)
|
||||
|
||||
foreach (var p in peaks)
|
||||
{
|
||||
var du = (u - p.U) / p.RU;
|
||||
var dv = (v - p.V) / p.RV;
|
||||
var d2 = du * du + dv * dv;
|
||||
h += p.H * Math.Exp(-d2 * 1.15);
|
||||
}
|
||||
|
||||
// 山谷:沿一条斜线切下去,形成 V 形谷地
|
||||
var valley = Math.Abs((u - 0.5) * 0.8 + (v - 0.44) * 0.6);
|
||||
h -= 210 * Math.Exp(-valley * valley / 0.0016);
|
||||
|
||||
// 陡崖:东侧一条近乎垂直的高差带(等高线会挤在一起)
|
||||
if (u > 0.86 && v > 0.18 && v < 0.72)
|
||||
h += 260 * SmoothStep(0.86, 0.895, u);
|
||||
|
||||
// 缓坡:西南角缓慢抬升
|
||||
if (u < 0.3 && v > 0.6)
|
||||
h += 90 * (1 - u / 0.3) * ((v - 0.6) / 0.4);
|
||||
|
||||
// 轻微起伏,避免地形过于几何化
|
||||
h += 18 * Math.Sin(u * 9.1) * Math.Cos(v * 7.7);
|
||||
|
||||
var hf = (float)Math.Max(0, h);
|
||||
Heights[y, x] = hf;
|
||||
if (hf < min) min = hf;
|
||||
if (hf > max) max = hf;
|
||||
}
|
||||
}
|
||||
|
||||
MinHeight = min;
|
||||
MaxHeight = max;
|
||||
}
|
||||
|
||||
/// <summary>盆地:四周高、中间低,等高线由外向内递减。</summary>
|
||||
private void BuildBasin()
|
||||
{
|
||||
var min = float.MaxValue; var max = float.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
{
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
var du = u - 0.5; var dv = v - 0.5;
|
||||
var r = Math.Sqrt(du * du + dv * dv) * 2.0; // 0=中心 1=边缘
|
||||
|
||||
// 边缘抬升、中心下沉,形成碗状
|
||||
double h = 160 + 900 * Math.Pow(Math.Clamp(r, 0, 1.15), 2.2);
|
||||
// 一侧开个缺口(山口),让盆地向外开口
|
||||
var gap = Math.Abs(Math.Atan2(dv, du) - 0.5);
|
||||
if (gap < 0.35) h -= 320 * (1 - gap / 0.35) * (1 - Math.Clamp(r, 0, 1));
|
||||
|
||||
h += 14 * Math.Sin(u * 7.3) * Math.Cos(v * 6.1);
|
||||
var hf = (float)Math.Max(0, h);
|
||||
Heights[y, x] = hf;
|
||||
if (hf < min) min = hf;
|
||||
if (hf > max) max = hf;
|
||||
}
|
||||
}
|
||||
MinHeight = min; MaxHeight = max;
|
||||
}
|
||||
|
||||
/// <summary>丘陵:起伏和缓、海拔不高,等高线稀疏而弯曲。</summary>
|
||||
private void BuildHill()
|
||||
{
|
||||
var bumps = new (double U, double V, double H, double R)[]
|
||||
{
|
||||
(0.30, 0.32, 120, 0.20), (0.66, 0.28, 95, 0.17),
|
||||
(0.52, 0.62, 140, 0.22), (0.22, 0.72, 88, 0.16),
|
||||
(0.78, 0.70, 110, 0.18),
|
||||
};
|
||||
var min = float.MaxValue; var max = float.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
{
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
double h = 60;
|
||||
foreach (var b in bumps)
|
||||
{
|
||||
var du = (u - b.U) / b.R; var dv = (v - b.V) / b.R;
|
||||
h += b.H * Math.Exp(-(du * du + dv * dv) * 1.3);
|
||||
}
|
||||
h += 10 * Math.Sin(u * 11.0) * Math.Cos(v * 9.0);
|
||||
var hf = (float)Math.Max(0, h);
|
||||
Heights[y, x] = hf;
|
||||
if (hf < min) min = hf;
|
||||
if (hf > max) max = hf;
|
||||
}
|
||||
}
|
||||
MinHeight = min; MaxHeight = max;
|
||||
}
|
||||
|
||||
/// <summary>高原:顶部平坦、边缘陡峭(等高线在中部稀疏、边缘密集)。</summary>
|
||||
private void BuildPlateau()
|
||||
{
|
||||
var min = float.MaxValue; var max = float.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
{
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
|
||||
// 到中心的距离,做平台 + 陡降
|
||||
var du = (u - 0.5); var dv = (v - 0.5);
|
||||
var r = Math.Sqrt(du * du + dv * dv) * 2.0;
|
||||
|
||||
double h;
|
||||
if (r < 0.55) h = 1250; // 平坦顶面
|
||||
else h = 1250 - 1000 * SmoothStep(0.55, 0.78, r); // 边缘陡降
|
||||
|
||||
// 顶面轻微起伏,避免完全平板
|
||||
if (r < 0.55) h += 25 * Math.Sin(u * 6.0) * Math.Cos(v * 5.0);
|
||||
h += 30 * (1 - Math.Clamp(r, 0, 1));
|
||||
|
||||
var hf = (float)Math.Max(0, h);
|
||||
Heights[y, x] = hf;
|
||||
if (hf < min) min = hf;
|
||||
if (hf > max) max = hf;
|
||||
}
|
||||
}
|
||||
MinHeight = min; MaxHeight = max;
|
||||
}
|
||||
|
||||
/// <summary>平原:地面低平、起伏极小,等高线很少。</summary>
|
||||
private void BuildPlain()
|
||||
{
|
||||
var min = float.MaxValue; var max = float.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
{
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
|
||||
// 整体微倾 + 极缓的起伏
|
||||
double h = 40 + 22 * u + 8 * Math.Sin(u * 4.2) * Math.Cos(v * 3.6);
|
||||
var hf = (float)Math.Max(0, h);
|
||||
Heights[y, x] = hf;
|
||||
if (hf < min) min = hf;
|
||||
if (hf > max) max = hf;
|
||||
}
|
||||
}
|
||||
MinHeight = min; MaxHeight = max;
|
||||
}
|
||||
|
||||
// ───────────────────────── 五大典型地貌部位 ─────────────────────────
|
||||
|
||||
private void BuildLandform(LandformKind landform)
|
||||
{
|
||||
switch (landform)
|
||||
{
|
||||
case LandformKind.Peak: BuildPeak(); break;
|
||||
case LandformKind.Ridge: BuildRidge(); break;
|
||||
case LandformKind.Valley: BuildValley(); break;
|
||||
case LandformKind.Saddle: BuildSaddle(); break;
|
||||
case LandformKind.Cliff: BuildCliff(); break;
|
||||
}
|
||||
BuildFeatures();
|
||||
}
|
||||
|
||||
/// <summary>山峰:单峰为主、层层闭合,四周逐级降低。配一处小侧峰便于读鞍部。</summary>
|
||||
private void BuildPeak()
|
||||
{
|
||||
var min = double.MaxValue; var max = double.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
var du = u - 0.5; var dv = (v - 0.5) / 0.94;
|
||||
var r = Math.Sqrt(du * du + dv * dv);
|
||||
// 主峰:宽阔的穹状山体(半径约占图幅 8 成),层层闭合的等高线才画得开
|
||||
var core = Math.Exp(-Math.Pow(r / 0.36, 1.85));
|
||||
var rim = 1 - SmoothStep(0.36, 0.50, r); // 山脚过渡到平地
|
||||
var h = 65 + 1230 * core * rim;
|
||||
// 东南侧肩部,让等高线不是完美同心圆
|
||||
h += 120 * Math.Exp(-(Math.Pow(u - 0.72, 2) + Math.Pow(v - 0.66, 2)) / 0.016) * rim;
|
||||
h += 22 * Math.Sin(u * 7.3 + 0.6) * Math.Cos(v * 6.1) * rim;
|
||||
h = Math.Max(0, h);
|
||||
Heights[y, x] = (float)h;
|
||||
if (h < min) min = h; if (h > max) max = h;
|
||||
}
|
||||
MinHeight = (float)min; MaxHeight = (float)max;
|
||||
Tags.Clear();
|
||||
Tags.Add((0.5, 0.5, 0.09, TerrainFeature.Peak));
|
||||
}
|
||||
|
||||
/// <summary>山脊:一条狭长高地,等高线沿脊线向低处凸出(凸低为脊)。</summary>
|
||||
private void BuildRidge()
|
||||
{
|
||||
var min = double.MaxValue; var max = double.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
// 脊线沿 u 方向(西高东低),东西向是长坡,南北向是宽缓的两翼
|
||||
var crestV = 0.5 + 0.10 * Math.Sin(u * 3.1); // 脊线轻微弯曲
|
||||
var across = v - crestV;
|
||||
var crest = 1150 - 380 * u; // 脊线海拔由西向东降低
|
||||
var lateral = Math.Exp(-Math.Pow(Math.Abs(across) / 0.27, 1.55));
|
||||
var h = 85 + (crest - 85) * lateral;
|
||||
// 两翼的次级起伏,避免像一堵直墙
|
||||
h += 60 * Math.Exp(-Math.Pow(Math.Abs(across) - 0.30, 2) / 0.010) * (0.4 + 0.6 * (1 - u));
|
||||
h += 24 * Math.Sin(u * 8.0) * Math.Cos(v * 5.6);
|
||||
h = Math.Max(0, h);
|
||||
Heights[y, x] = (float)h;
|
||||
if (h < min) min = h; if (h > max) max = h;
|
||||
}
|
||||
MinHeight = (float)min; MaxHeight = (float)max;
|
||||
Tags.Clear();
|
||||
Tags.Add((0.30, 0.50, 0.09, TerrainFeature.Ridge));
|
||||
Tags.Add((0.70, 0.50, 0.09, TerrainFeature.Ridge));
|
||||
}
|
||||
|
||||
/// <summary>山谷:两侧高地夹持的低槽,等高线形成 V 形指向上游(凸高为谷)。</summary>
|
||||
private void BuildValley()
|
||||
{
|
||||
var min = double.MaxValue; var max = double.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
// 谷轴沿 v 方向:两侧高、中间是 V 形谷底;北侧(v 小)为上游更高
|
||||
var side = Math.Pow(Math.Abs(u - 0.5) / 0.5, 1.5); // 0=谷心 1=图边
|
||||
var downhill = 1 - v; // 上游 1 → 下游 0
|
||||
var h = 90 + 760 * downhill + 585 * side * (0.30 + 0.70 * downhill);
|
||||
// 谷底再切深一点,形成明显的集水线
|
||||
h -= 70 * Math.Exp(-Math.Pow(u - 0.5, 2) / 0.0022);
|
||||
h += 18 * Math.Sin(u * 6.5) * Math.Cos(v * 7.2);
|
||||
h = Math.Max(0, h);
|
||||
Heights[y, x] = (float)h;
|
||||
if (h < min) min = h; if (h > max) max = h;
|
||||
}
|
||||
MinHeight = (float)min; MaxHeight = (float)max;
|
||||
Tags.Clear();
|
||||
Tags.Add((0.50, 0.40, 0.075, TerrainFeature.Valley));
|
||||
Tags.Add((0.50, 0.68, 0.075, TerrainFeature.Valley));
|
||||
}
|
||||
|
||||
/// <summary>鞍部:两座山峰之间相对低洼、俯视轮廓收腰的过渡地带。</summary>
|
||||
private void BuildSaddle()
|
||||
{
|
||||
var min = double.MaxValue; var max = double.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
// 两峰在 v≈0.5 的连线上,u=0.27 与 u=0.73;中间 u=0.5 为垭口
|
||||
var d1 = Math.Sqrt(Math.Pow((u - 0.27) / 0.30, 2) + Math.Pow((v - 0.52) / 0.28, 2));
|
||||
var d2 = Math.Sqrt(Math.Pow((u - 0.73) / 0.30, 2) + Math.Pow((v - 0.48) / 0.28, 2));
|
||||
var p1 = 1140 * Math.Exp(-Math.Pow(d1, 2.2));
|
||||
var p2 = 1060 * Math.Exp(-Math.Pow(d2, 2.2));
|
||||
// 南北两侧缓慢降低,把两峰连成一条东西向的山梁
|
||||
var bench = 330 * Math.Exp(-Math.Pow((v - 0.5) / 0.34, 2));
|
||||
var h = 65 + bench + Math.Max(p1, p2);
|
||||
h += 20 * Math.Sin(u * 8.0) * Math.Cos(v * 7.0);
|
||||
h = Math.Max(0, h);
|
||||
Heights[y, x] = (float)h;
|
||||
if (h < min) min = h; if (h > max) max = h;
|
||||
}
|
||||
MinHeight = (float)min; MaxHeight = (float)max;
|
||||
Tags.Clear();
|
||||
Tags.Add((0.28, 0.50, 0.05, TerrainFeature.Peak));
|
||||
Tags.Add((0.72, 0.50, 0.05, TerrainFeature.Peak));
|
||||
Tags.Add((0.50, 0.50, 0.045, TerrainFeature.Saddle));
|
||||
}
|
||||
|
||||
/// <summary>陡崖:一侧近乎直立的高差带,等高线在崖壁处高度密集甚至重合。</summary>
|
||||
private void BuildCliff()
|
||||
{
|
||||
var min = double.MaxValue; var max = double.MinValue;
|
||||
for (var y = 0; y < Size; y++)
|
||||
for (var x = 0; x < Size; x++)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
// 西侧低台地、东侧高台地,中间一条近乎直立的高差带形成陡崖
|
||||
// 崖线略呈波状(真实崖壁不是一条直线),过渡带留出 2~3 个采样格
|
||||
var edge = SmoothStep(0.50, 0.62, u + 0.045 * Math.Sin(v * 5.2) + 0.02 * Math.Sin(v * 11.0));
|
||||
var h = 210 + 940 * edge;
|
||||
// 高台地北高南低、顶面起伏,低台地保持缓坡
|
||||
h += 170 * (1 - v) * edge;
|
||||
h += 55 * Math.Sin(v * 5.4) * Math.Cos(u * 4.2) * (0.4 + 0.6 * edge);
|
||||
h += 26 * Math.Sin(u * 9.0); // 顶面轻微起伏
|
||||
h = Math.Max(0, h);
|
||||
Heights[y, x] = (float)h;
|
||||
if (h < min) min = h; if (h > max) max = h;
|
||||
}
|
||||
MinHeight = (float)min; MaxHeight = (float)max;
|
||||
Tags.Clear();
|
||||
Tags.Add((0.50, 0.50, 0.035, TerrainFeature.Cliff));
|
||||
}
|
||||
|
||||
private static double SmoothStep(double a, double b, double x)
|
||||
{
|
||||
var t = Math.Clamp((x - a) / (b - a), 0, 1);
|
||||
return t * t * (3 - 2 * t);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 教学标注区:把「典型部位」的位置固化成圆形区域,
|
||||
/// 保证判读结果与地形设计一致(纯几何推断对鞍部/陡崖易误判)。
|
||||
/// </summary>
|
||||
public List<(double U, double V, double Radius, TerrainFeature Feature)> Tags { get; } = new()
|
||||
{
|
||||
(0.34, 0.34, 0.045, TerrainFeature.Peak), // 主峰
|
||||
(0.66, 0.52, 0.042, TerrainFeature.Peak), // 次峰
|
||||
(0.50, 0.44, 0.055, TerrainFeature.Saddle), // 两峰之间的鞍部
|
||||
(0.90, 0.45, 0.035, TerrainFeature.Cliff), // 东侧陡崖
|
||||
(0.22, 0.70, 0.045, TerrainFeature.Ridge), // 西南山脊
|
||||
(0.50, 0.22, 0.040, TerrainFeature.Valley), // 河谷
|
||||
};
|
||||
|
||||
/// <summary>查询标注区;未命中返回 None。</summary>
|
||||
public TerrainFeature TagAt(double u, double v)
|
||||
{
|
||||
foreach (var (tu, tv, r, f) in Tags)
|
||||
{
|
||||
var du = u - tu; var dv = v - tv;
|
||||
if (du * du + dv * dv <= r * r) return f;
|
||||
}
|
||||
return TerrainFeature.None;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 河流:从最高点出发,沿最陡下降方向流到图边。
|
||||
/// 教科书上的河流画蓝色线,且**只在河谷(等高线凸向高处的一侧)里流**。
|
||||
/// </summary>
|
||||
public List<(double U, double V)> River { get; } = new();
|
||||
|
||||
/// <summary>山峰位置(用于画 ▲ 与高程数字):局部最高点。</summary>
|
||||
public List<(double U, double V, float Height)> Peaks { get; } = new();
|
||||
|
||||
/// <summary>陡崖位置(用于画锯齿线):坡度极大处。</summary>
|
||||
public List<(double U, double V)> Cliffs { get; } = new();
|
||||
|
||||
/// <summary>找出山峰、陡崖,并生成河流线。</summary>
|
||||
private void BuildFeatures()
|
||||
{
|
||||
// --- 山峰:局部最高且足够高 ---
|
||||
const int step = 4;
|
||||
for (var y = step; y < Size - step; y += step)
|
||||
{
|
||||
for (var x = step; x < Size - step; x += step)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
var h = Heights[y, x];
|
||||
if (h < MaxHeight * 0.55) continue;
|
||||
|
||||
var isMax = true;
|
||||
for (var dy = -step; dy <= step && isMax; dy += step)
|
||||
for (var dx = -step; dx <= step; dx += step)
|
||||
{
|
||||
if (dx == 0 && dy == 0) continue;
|
||||
if (Heights[y + dy, x + dx] > h) { isMax = false; break; }
|
||||
}
|
||||
if (isMax) Peaks.Add((u, v, h));
|
||||
}
|
||||
}
|
||||
Peaks.Sort((a, b) => b.Height.CompareTo(a.Height));
|
||||
|
||||
// --- 陡崖:坡度极大 ---
|
||||
const int cstep = 3;
|
||||
for (var y = cstep; y < Size - cstep; y += cstep)
|
||||
{
|
||||
for (var x = cstep; x < Size - cstep; x += cstep)
|
||||
{
|
||||
var u = (double)x / (Size - 1);
|
||||
var v = (double)y / (Size - 1);
|
||||
var h = 0.004;
|
||||
var du = (Heights[y, x + 2] - Heights[y, x - 2]) / (2 * h * (Size - 1)) / 1000;
|
||||
var dv = (Heights[y + 2, x] - Heights[y - 2, x]) / (2 * h * (Size - 1)) / 1000;
|
||||
var slope = Math.Sqrt(du * du + dv * dv);
|
||||
if (slope > 0.11) Cliffs.Add((u, v));
|
||||
}
|
||||
}
|
||||
|
||||
// --- 河流:从最高点沿最陡下降方向走 ---
|
||||
if (Peaks.Count > 0)
|
||||
{
|
||||
var start = Peaks[0];
|
||||
var cu = start.U; var cv = start.V;
|
||||
River.Add((cu, cv));
|
||||
const int maxSteps = 400;
|
||||
for (var step2 = 0; step2 < maxSteps; step2++)
|
||||
{
|
||||
var h0 = SampleSmooth(cu, cv);
|
||||
double bestU = 0, bestV = 0; var bestH = h0; var found = false;
|
||||
for (var a = 0; a < 16; a++)
|
||||
{
|
||||
var ang = a * Math.PI * 2 / 16;
|
||||
var nu = cu + Math.Cos(ang) * 0.012;
|
||||
var nv = cv + Math.Sin(ang) * 0.012;
|
||||
if (nu < 0 || nu > 1 || nv < 0 || nv > 1) continue;
|
||||
var nh = SampleSmooth(nu, nv);
|
||||
if (nh < bestH) { bestH = nh; bestU = nu; bestV = nv; found = true; }
|
||||
}
|
||||
if (!found) break;
|
||||
cu = bestU; cv = bestV;
|
||||
River.Add((cu, cv));
|
||||
if (cu <= 0.001 || cu >= 0.999 || cv <= 0.001 || cv >= 0.999) break;
|
||||
if (bestH < MinHeight + 20) break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>等高线高度列表:从 MaxHeight 往下按等高距取整。</summary>
|
||||
public List<float> ContourLevels()
|
||||
{
|
||||
var levels = new List<float>();
|
||||
var iv = EffectiveInterval;
|
||||
var top = (float)(Math.Floor(MaxHeight / iv) * iv);
|
||||
var bottom = (float)(Math.Ceiling(MinHeight / iv) * iv);
|
||||
for (var h = bottom; h <= top + 0.01f; h += iv)
|
||||
levels.Add(h);
|
||||
levels.Sort();
|
||||
return levels;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用 Marching Squares 提取某条等高线,返回若干条折线(归一化坐标)。
|
||||
/// </summary>
|
||||
public List<List<(double U, double V)>> ExtractContour(float level)
|
||||
{
|
||||
var result = new List<List<(double, double)>>();
|
||||
var size = Size;
|
||||
|
||||
// 记录每条线段,再按端点连通性串成折线
|
||||
var segments = new List<(double U1, double V1, double U2, double V2)>();
|
||||
|
||||
for (var y = 0; y < size - 1; y++)
|
||||
{
|
||||
for (var x = 0; x < size - 1; x++)
|
||||
{
|
||||
var h00 = Heights[y, x];
|
||||
var h10 = Heights[y, x + 1];
|
||||
var h11 = Heights[y + 1, x + 1];
|
||||
var h01 = Heights[y + 1, x];
|
||||
|
||||
var idx = 0;
|
||||
if (h00 >= level) idx |= 1;
|
||||
if (h10 >= level) idx |= 2;
|
||||
if (h11 >= level) idx |= 4;
|
||||
if (h01 >= level) idx |= 8;
|
||||
if (idx == 0 || idx == 15) continue;
|
||||
|
||||
double u0 = (double)x / (size - 1), u1 = (double)(x + 1) / (size - 1);
|
||||
double v0 = (double)y / (size - 1), v1 = (double)(y + 1) / (size - 1);
|
||||
|
||||
// 四条边上的交点
|
||||
(double, double) Top() => (Lerp(u0, u1, (level - h00) / (h10 - h00)), v0);
|
||||
(double, double) Bottom() => (Lerp(u0, u1, (level - h01) / (h11 - h01)), v1);
|
||||
(double, double) Left() => (u0, Lerp(v0, v1, (level - h00) / (h01 - h00)));
|
||||
(double, double) Right() => (u1, Lerp(v0, v1, (level - h10) / (h11 - h10)));
|
||||
|
||||
void Add((double U, double V) a, (double U, double V) b)
|
||||
=> segments.Add((a.Item1, a.Item2, b.Item1, b.Item2));
|
||||
|
||||
switch (idx)
|
||||
{
|
||||
case 1: case 14: Add(Left(), Top()); break;
|
||||
case 2: case 13: Add(Top(), Right()); break;
|
||||
case 3: case 12: Add(Left(), Right()); break;
|
||||
case 4: case 11: Add(Right(), Bottom()); break;
|
||||
case 6: case 9: Add(Top(), Bottom()); break;
|
||||
case 7: case 8: Add(Left(), Bottom()); break;
|
||||
case 5: Add(Left(), Top()); Add(Right(), Bottom()); break;
|
||||
case 10: Add(Top(), Right()); Add(Left(), Bottom()); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Chain(segments);
|
||||
}
|
||||
|
||||
private static double Lerp(double a, double b, double t)
|
||||
=> a + (b - a) * Math.Clamp(double.IsNaN(t) ? 0.5 : t, 0, 1);
|
||||
|
||||
/// <summary>
|
||||
/// 把零散线段按端点串成完整折线。
|
||||
/// 关键点:必须**两端都延伸**。只往尾部延伸时,起点落在曲线中段就会把一条等高线
|
||||
/// 切成许多短链,渲染出来是断续的虚线(学生看到的就是“虚线等高线”)。
|
||||
/// </summary>
|
||||
private static List<List<(double U, double V)>> Chain(
|
||||
List<(double U1, double V1, double U2, double V2)> segments)
|
||||
{
|
||||
// 量化到 1e-4 的网格做端点索引:共享边上的交点由同一次插值算出,
|
||||
// 端点值完全一致;网格远小于采样间距(1/192≈0.0052),不会误并。
|
||||
const double Q = 1e4;
|
||||
static (long, long) Key(double u, double v)
|
||||
=> ((long)Math.Round(u * Q), (long)Math.Round(v * Q));
|
||||
|
||||
var index = new Dictionary<(long, long), List<int>>();
|
||||
void Register(double u, double v, int seg)
|
||||
{
|
||||
var k = Key(u, v);
|
||||
if (!index.TryGetValue(k, out var list)) index[k] = list = new List<int>();
|
||||
list.Add(seg);
|
||||
}
|
||||
|
||||
for (var i = 0; i < segments.Count; i++)
|
||||
{
|
||||
Register(segments[i].U1, segments[i].V1, i);
|
||||
Register(segments[i].U2, segments[i].V2, i);
|
||||
}
|
||||
|
||||
var used = new bool[segments.Count];
|
||||
var result = new List<List<(double, double)>>();
|
||||
|
||||
int NextAt(double u, double v)
|
||||
{
|
||||
if (!index.TryGetValue(Key(u, v), out var list)) return -1;
|
||||
foreach (var j in list) if (!used[j]) return j;
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (var i = 0; i < segments.Count; i++)
|
||||
{
|
||||
if (used[i]) continue;
|
||||
used[i] = true;
|
||||
var line = new List<(double U, double V)>
|
||||
{
|
||||
(segments[i].U1, segments[i].V1),
|
||||
(segments[i].U2, segments[i].V2)
|
||||
};
|
||||
|
||||
// 向尾部延伸
|
||||
while (true)
|
||||
{
|
||||
var (tu, tv) = line[^1];
|
||||
var j = NextAt(tu, tv);
|
||||
if (j < 0) break;
|
||||
used[j] = true;
|
||||
var s = segments[j];
|
||||
line.Add(Key(s.U1, s.V1) == Key(tu, tv) ? (s.U2, s.V2) : (s.U1, s.V1));
|
||||
}
|
||||
|
||||
// 向头部延伸
|
||||
while (true)
|
||||
{
|
||||
var (hu, hv) = line[0];
|
||||
var j = NextAt(hu, hv);
|
||||
if (j < 0) break;
|
||||
used[j] = true;
|
||||
var s = segments[j];
|
||||
line.Insert(0, Key(s.U1, s.V1) == Key(hu, hv) ? (s.U2, s.V2) : (s.U1, s.V1));
|
||||
}
|
||||
|
||||
if (line.Count >= 3) result.Add(line);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static bool Near(double u1, double v1, double u2, double v2, double eps)
|
||||
=> Math.Abs(u1 - u2) < eps && Math.Abs(v1 - v2) < eps;
|
||||
}
|
||||
Reference in new issue
Block a user