搜索插件、代码仓与 APP 分类

插件详情v0.0.4

sweepline_intersections

一个小型且快速的模块,使用扫描线算法来检测多边形和/或折线之间的相交。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
17万
喜欢数
7
Pub 分数
150

安装插件

flutter pub add sweepline_intersections

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个轻量级且高性能的 Dart 包,使用扫描线算法高效检测多边形和折线之间的交点。专为二维空间中的几何计算而设计,提供快速的交点检测,非常适合需要实时或批量处理复杂形状的应用程序。该包具有平台无关性,支持所有主要的 Flutter 平台。它在准确性和速度方面进行了优化,适用于 GIS、CAD 和可视化工具。

适合场景

  • 实时多边形碰撞检测
  • GIS空间分析
  • CAD图纸验证
  • 交互式地图功能
  • 用于渲染的几何预处理
  • 游戏中的碰撞检测

主要特点

  • 高速交点检测
  • 扫描线算法实现
  • 支持多边形和折线
  • 跨平台兼容性
  • 最小内存占用
  • 精确的几何结果

适合对象

  • 开发交互式地图的开发者
  • 需要几何形状分析的应用程序
  • 用于CAD或设计软件的工具
  • 具有复杂碰撞系统的游戏
  • 性能关键的几何操作

使用建议

  • 最适合二维几何
  • 需要正确闭合的多边形才能获得准确结果
  • 未针对无分批处理的超大数据集进行优化
  • 仅限线段和多边形相交

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Ported from: rowanwins/sweepline-intersections

sweepline-intersections

A small and fast module using a sweepline algorithm to detect intersections between polygons and/or polylines.

Documentation

Install
dart pub add sweepline_intersections
Basic Use

Valid inputs: Geojson Feature or Geometry including Polygon, LineString, MultiPolygon, MultiLineString, as well as FeatureCollection.

Returns a List of intersection Points eg, [Point(coordinates:Position(lng:x1, lat:y1)), Point(coordinates:Position(lng:x2, lat:y2)]

    var box = Feature(geometry: Polygon(coordinates: [[Position.of([0, 0]), Position.of([1, 0]), Position.of([1, 1]), Position.of([0, 1]), Position.of([0, 0])]]));
    var intersections = sweeplineIntersections(box);
    // returns a List of self-intersection Points

Also accepts an optional boolean argument second which when set to true means the module won't detect self-intersections and will only report intersections between different features. This defaults to false. eg

    var intersectionsBetweenFeature = sweeplineIntersections(featureCollection, true);
    // returns a List of intersection Points between Features
Complex Use

This library also provide a class-based approach which is helpful if you want to check multiple geometries against a single geometry. This allows you to save the state of the initial event queue with the primary geometry.

    import 'package:sweepline_intersections/sweepline_intersections.dart';
    main(){
    // create the base instance
    var sl = SweeplineIntersections();
    sl.addData(aGeom);
    // clone the event queue in the original state so you can reuse it
    var origQueue = sl.cloneEventQueue();
    List<Position> positions = [
    Position.of([21.93869948387146, 49.99897434944081]),
    Position.of([21.93869948387100, 49.99897434944032]),
    ];
    var f = FeatureCollection(
    features: [
      Feature(
        geometry: LineString(coordinates: positions),
      ),
    ],
    );    // now you can iterate through some other set of features saving
    // the overhead of having to populate the complete queue multiple times
    for (var feature in f.features) {
    // add another feature to test against your original data
    sl.addData(feature, alternateEventQueue: origQueue);
    // check if those two features intersect
    // add an optional boolean argument to ignore self-intersections
    }
    var intersectionPoints = sl.getIntersections(true);
    }
API

SweeplineIntersectionsClass() - creates a new instance

.addData(geojson, existingQueue) - adds geojson to the event queue. The second argument for an existingQueue is optional, and takes a queue generated from .cloneEventQueue()

.cloneEventQueue() - clones the state of the existing event queue that's been populated with geojson. Returns a queue that you can pass to the addData method

.getIntersections(ignoreSelfIntersections) - Checks for segment intersections. Accepts an optional boolean argument to ignore self intersections are only report intersections between features.

Contributing

Algorithm notes

The basic concept of this algorithm is based on a sweepline. Where this algorithm differs from the bentley-ottmann algorithm is that there is no use of a tree data structure to store the segments. The reason for the modification is because if you are dealing with polygons or polylines (rather than a random group of line segments) there is a reasonable assumption that there are going to be very few segments that lie on the same x plane.

Removing the tree structure greatly simplifies the code. The tree structure is replaced with a priority queue of segments which is sorted by the x vertex of the right endpoint of the segments. A priority queue is already used to sort the vertices which means only 1 data structure is required.

Algorithm Steps
  • Vertices are entered into a priority queue sorted from left to right
  • An empty priority queue is created to store segments encountered
  • An item is removed from the priority queue
    • If the vertex is the left endpoint of a segment, we test it against every other segment in the segment queue for intersections with any intersection recorded. We then add the vertex (and it's associated right endpoint) to the segment queue.
    • When we encounter a right endpoint we remove the first item from the segment queue.

Each pair of segments are only tested once. And only segments that overlap on the x plane are tested against each other.

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目