趣味绘制 蒲丰投针试验

This commit is contained in:
toly
2022-04-30 23:49:27 +08:00
parent 44043da198
commit f73570af29
8 changed files with 217 additions and 19 deletions

View File

@@ -25,10 +25,10 @@
> 当前Flutter 版本
```
Flutter 2.10.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 7e9793dee1 (4 weeks ago) • 2022-03-02 11:23:12 -0600
Engine • revision bd539267b4
Tools • Dart 2.16.1 • DevTools 2.9.2
Flutter 2.10.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 5464c5bac7 (10 days ago) • 2022-04-18 09:55:37 -0700
Engine • revision 57d3bac3dd
Tools • Dart 2.16.2 • DevTools 2.9.2
```
---

View File

@@ -42,17 +42,13 @@ class _BlocWrapperState extends State<BlocWrapper> {
providers: [
BlocProvider<GlobalBloc>(create: (_) => GlobalBloc(storage)..add(const EventInitApp())),
BlocProvider<WidgetsBloc>(create: (_) => WidgetsBloc(repository: repository)),
BlocProvider<WidgetDetailBloc>(
create: (_) => WidgetDetailBloc(repository: repository)),
BlocProvider<CategoryBloc>(
create: (_) => categoryBloc),
BlocProvider<LikeWidgetBloc>(
create: (_) => LikeWidgetBloc(repository: repository)),
BlocProvider<WidgetDetailBloc>(create: (_) => WidgetDetailBloc(repository: repository)),
BlocProvider<CategoryBloc>(create: (_) => categoryBloc),
BlocProvider<LikeWidgetBloc>(create: (_) => LikeWidgetBloc(repository: repository)),
BlocProvider<RegisterBloc>(create: (_) => RegisterBloc()),
BlocProvider<LoginBloc>(create: (_) => LoginBloc(authenticBloc: authBloc)),
BlocProvider<AuthenticBloc>(create: (_) => authBloc),
BlocProvider<CategoryWidgetBloc>(
create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)),
BlocProvider<CategoryWidgetBloc>(create: (_) => CategoryWidgetBloc(categoryBloc: categoryBloc)),
BlocProvider<PointBloc>(create: (_) => PointBloc()),
BlocProvider<UpdateBloc>(create: (_) => UpdateBloc()),
BlocProvider<GalleryUnitBloc>(create: (_) => GalleryUnitBloc()..loadGalleryInfo()),

View File

@@ -108,7 +108,6 @@ class _UnitNavigationState extends State<UnitNavigation> {
// 点击底部按钮事件,切换页面
void _onTapBottomNav(int index) {
// _controller.animateToPage(index, duration: const Duration(milliseconds: 200), curve: Curves.linear);
_controller.jumpToPage(index);
if(!isDark){

View File

@@ -0,0 +1,65 @@
import 'package:flutter/material.dart';
import 'painter.dart';
import 'config.dart';
class BufengPanel extends StatefulWidget {
const BufengPanel({Key? key}) : super(key: key);
@override
State<BufengPanel> createState() => _BufengPanelState();
}
class _BufengPanelState extends State<BufengPanel> {
final Config config = Config(
size: const Size(200, 200),
);
void addNeedle({int count = 1}) {
for (int i = 0; i < count; i++) {
config.addNeedle();
}
}
@override
Widget build(BuildContext context) {
return Stack(
alignment: Alignment.topCenter,
children: [
Padding(
padding: EdgeInsets.only(top: 10),
child: CustomPaint(
painter: PiPainter(config),
size: config.size,
),
),
Positioned(
right: 0,
top: 10,
child: Row(
children: [
IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)),
IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)),
IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)),
],
),
)
],
);
// Scaffold(
// appBar: AppBar(
// title: const Text('蒲丰投针试验'),
// actions:
// [
// IconButton(onPressed: addNeedle, icon: const Icon(Icons.add)),
// IconButton(onPressed: () => addNeedle(count: 100), icon: const Icon(Icons.add_chart)),
// IconButton(onPressed: config.clear, icon: const Icon(Icons.refresh)),
// ],
// ),
// body: Center(
// child: ,
// ),
// );
}
}

View File

@@ -0,0 +1,67 @@
import 'dart:math';
import 'package:flutter/cupertino.dart';
class Line {
final Offset p0;
final Offset p1;
Line({
required this.p0,
required this.p1,
});
bool isActive(double gap) {
int gapIndex0 = p0.dy ~/ gap;
int gapIndex1 = p1.dy < 0 ? -1 : p1.dy ~/ gap;
return gapIndex0 != gapIndex1 ||
p0.dy % gap < 0.000000001 ||
p1.dy % gap < 0.000000001;
}
}
class Config extends ChangeNotifier {
int lineCount;
Size size;
List<Line> lines = [];
Config({
this.lineCount = 8,
this.size = const Size(200, 200),
});
double get gap => size.height / lineCount;
@override
String toString() {
int n = lines.length;
int m = lines.where((line) => line.isActive(gap)).length;
double parserPi = m == 0 ? 0 : 2 * n / m;
return '投针个数: ${lines.length}\n命中个数: $m \n'
'估算圆周率 : $parserPi';
}
void addNeedle() {
Line line = _createNeedle();
lines.add(line);
notifyListeners();
}
final Random _random = Random();
Line _createNeedle() {
double x = _random.nextDouble() * size.width;
double y = _random.nextDouble() * size.height;
double rad = 2 * pi * _random.nextDouble();
double dx = gap * cos(rad);
double dy = gap * sin(rad);
Line line = Line(p0: Offset(x, y), p1: Offset(x + dx, y + dy));
return line;
}
void clear() {
lines.clear();
notifyListeners();
}
}

View File

@@ -0,0 +1,70 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'config.dart';
class PiPainter extends CustomPainter {
final Config config;
PiPainter(this.config) : super(repaint: config);
@override
void paint(Canvas canvas, Size size) {
print(size);
Paint paint = Paint();
paint.style = PaintingStyle.stroke;
final double span = config.gap;
drawHelpText(
'$config',
canvas,
Offset.zero,
);
canvas.translate(0, size.height*0.3);
canvas.save();
for (int i = 0; i <= config.lineCount; i++) {
canvas.drawLine(Offset.zero, Offset(size.width, 0), paint);
print(span*i);
canvas.translate(0, span);
}
canvas.restore();
Paint needlePaint = Paint();
needlePaint.style = PaintingStyle.stroke;
needlePaint.strokeWidth = 1;
for (Line line in config.lines) {
if (line.isActive(config.gap)) {
needlePaint.color = Colors.red;
} else {
needlePaint.color = Colors.green;
}
canvas.drawLine(line.p0, line.p1, needlePaint);
}
}
@override
bool shouldRepaint(covariant PiPainter oldDelegate) {
return config != oldDelegate.config;
}
final TextPainter textPainter = TextPainter(
textAlign: TextAlign.start,
textDirection: TextDirection.ltr,
);
void drawHelpText(
String text,
Canvas canvas,
Offset offset, {
Color color = Colors.lightBlue,
}) {
textPainter.text = TextSpan(
text: text,
style: TextStyle(fontSize: 14, color: color),
);
textPainter.layout(maxWidth: 250);
textPainter.paint(canvas, offset);
}
}

View File

@@ -22,6 +22,7 @@ import 'base/draw_picture.dart';
import 'base/n_side/n_side_page.dart';
import 'base/polar/polar_painter_widget.dart';
import 'fun/bufeng/bufeng_panel.dart';
import 'fun/dundun_view.dart';
import 'fun/random_portrait.dart';
import 'fun/stemp/stamp_paper.dart';
@@ -168,6 +169,12 @@ class GalleryFactory {
srcUrl: '/fun/dundun_view.dart',
info: "本样例是绘制 2022 年北京冬奥会吉祥物冰墩墩的形体,从中可以学到路径绘制、渐变色等知识。",
content: DunDunView()),
FrameShower(
title: "蒲丰投针试验",
author: "张风捷特烈",
srcUrl: '/fun/bufeng',
info: "本样实现蒲丰投针试验的测试过程,根据概率来估算圆周率。其中可以学习到一些绘制小技巧已经数据的逻辑处理。",
content: BufengPanel()),
FrameShower(
title: "井字棋",
author: "张风捷特烈",

View File

@@ -35,12 +35,6 @@ class _TabAppBarState extends State<TabAppBar>
super.dispose();
}
// decoration: const BoxDecoration(
// image: DecorationImage(
// image: AssetImage(
// "assets/images/sabar.webp",
// ),
// fit: BoxFit.cover))
@override
Widget build(BuildContext context) {
return SizedBox(