Flutter泊松圆盘采样插件fast_poisson_disk_sampling的使用
Flutter泊松圆盘采样插件fast_poisson_disk_sampling的使用
标题
Flutter泊松圆盘采样插件fast_poisson_disk_sampling的使用
内容
Dart port of the JS library https://github.com/kchapelier/fast-2d-poisson-disk-sampling
Fast 2D Poisson Disk Sampling based on a modified Bridson algorithm.
基本示例
var p = FastPoissonDiskSampling(
shape: Size(500, 200),
radius: 6,
maxTries: 20,
minDistance: 0,
rng: null
);
List<List<double>> points = p.fill();
结果以图片形式展示
构造函数
new FastPoissonDiskSampling(
options,
rng: null,
)
- options:
shape
: Size/dimensions of the grid to generate points in, required.radius
: Minimum distance between each points, required.tries
: Maximum number of tries per point, defaults to 30.rng
: A function to use as random number generator, defaults to math.Random().nextDouble().
方法
pds.fill()
Fill the grid with random points following the distance constraint. Returns the entirety of the points in the grid as an array of coordinate arrays. The points are sorted in their generation order.
print(points[0]);
// prints something like [30, 1]
pds.getAllPoints()
Get all the points present in the grid without trying to generate any new points. Returns the entirety of the points in the grid as an array of coordinate arrays. The points are sorted in their generation order.
print(points[0]);
// prints something like [30, 1]
pds.addRandomPoint()
Add a completely random point to the grid. There won’t be any check on the distance constraint with the other points already present in the grid. Returns the point as a coordinate array.
pds.addPoint(point)
- point: Point represented as a coordinate array. Add an arbitrary point to the grid. There won’t be any check on the distance constraint with the other points already present in the grid. Returns the point added to the grid.
If the given coordinate array does not have a length of 2 or doesn’t fit in the grid size, null will be returned.
pds.next()
Try to generate a new point in the grid following the distance constraint. Returns a coordinate array when a point is generated, null otherwise.
var point;
while(point = pds.next()) {
print(point); // [x, y]
}
pds.reset()
更多关于Flutter泊松圆盘采样插件fast_poisson_disk_sampling的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter泊松圆盘采样插件fast_poisson_disk_sampling的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用fast_poisson_disk_sampling
插件的一个详细代码案例。这个插件用于生成泊松圆盘采样点集,这在图形渲染、纹理生成等领域非常有用。
首先,确保你已经在pubspec.yaml
文件中添加了fast_poisson_disk_sampling
依赖:
dependencies:
flutter:
sdk: flutter
fast_poisson_disk_sampling: ^最新版本号 # 请替换为当前最新版本号
然后运行flutter pub get
来获取依赖。
接下来是一个完整的Flutter应用示例,展示如何使用fast_poisson_disk_sampling
插件生成泊松圆盘采样点,并在Canvas上进行绘制:
import 'package:flutter/material.dart';
import 'package:fast_poisson_disk_sampling/fast_poisson_disk_sampling.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Poisson Disk Sampling Example'),
),
body: CustomPaint(
size: Size(double.infinity, double.infinity),
painter: PoissonDiskPainter(),
),
),
);
}
}
class PoissonDiskPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint()
..color = Colors.blue
..style = PaintingStyle.fill;
final rect = Rect.fromLTWH(0, 0, size.width, size.height);
final sampler = PoissonDiskSampler(
rect: rect,
radius: 20.0, // 设置采样点的最小距离
maxAttempts: 100, // 最大尝试次数
seed: 42, // 随机种子,保证结果可复现
);
List<Offset> points = sampler.generate();
for (final point in points) {
canvas.drawCircle(point, 5.0, paint); // 绘制采样点,半径为5
}
}
@override
bool shouldRepaint(covariant CustomPainter oldDelegate) {
return false; // 如果采样点不需要动态更新,这里返回false
}
}
代码解释:
- 依赖添加:在
pubspec.yaml
中添加fast_poisson_disk_sampling
依赖。 - 主应用:
MyApp
是一个简单的Flutter应用,包含一个Scaffold
和一个CustomPaint
组件。 - 自定义绘制:
PoissonDiskPainter
类继承自CustomPainter
,用于在Canvas上绘制泊松圆盘采样点。- 初始化采样器:使用
PoissonDiskSampler
类,传入一个矩形区域(rect
)、采样点的最小距离(radius
)、最大尝试次数(maxAttempts
)和随机种子(seed
)。 - 生成采样点:调用
sampler.generate()
生成采样点列表。 - 绘制采样点:遍历采样点列表,使用
canvas.drawCircle
绘制每个采样点。
- 初始化采样器:使用
shouldRepaint
方法:由于采样点不需要动态更新,所以这里返回false
。
运行这个应用,你应该会在屏幕上看到一系列均匀分布的蓝色小圆点,这些点就是泊松圆盘采样生成的。
希望这个示例能帮助你理解如何在Flutter项目中使用fast_poisson_disk_sampling
插件。如果你有任何其他问题,欢迎继续提问!