使用 D3.js SVG 进行 2D 多边形布尔运算

2024-04-19

我有 2 个使用 D3.js 创建的简单面积图,数据和代码如下 - 让我们称它们为Graph A & Graph B。我想用它们根据它们的相交方式创建 3 个新路径/多边形。

  • Path 1 = Graph A - Graph B
  • Path 2 = Graph B - Graph A
  • Path 3 = Graph B - Path 2 or Graph A and Graph B路口

大多数可视化编辑器允许您执行这些布尔运算,请参阅:https://en.wikipedia.org/wiki/Boolean_operations_on_polygons https://en.wikipedia.org/wiki/Boolean_operations_on_polygons

在 D3.js 中可以做到这一点吗?


jsfiddle:https://jsfiddle.net/jvf1utmx/ https://jsfiddle.net/jvf1utmx/

图表定义:

// data
var dataA = [
    { x: 0, y: 100, },
    { x: 100, y: 150, },
    { x: 200, y: 350, },
    { x: 300, y: 200, },
];

var dataB = [
    { x: 0, y: 200, },
    { x: 100, y: 100, },
    { x: 200, y: 250, },
    { x: 300, y: 150, },
];

// Graph shapes
    var graphA = svg.append("path")
    .datum(dataA)
    .attr("class", "area")
    .attr("d", area)
    .style({fill: '#bbbb00', opacity: 0.8});

    var graphB = svg.append("path")
    .datum(dataB)
    .attr("class", "area")
    .attr("d", area)
    .style({fill: '#666666', opacity: 0.8});

我对剪辑路径的尝试:

// Clipping attempts
    var graphBClip = svg.append("clipPath")
        .attr('id','graphBClip')

    graphBClip.append(graphB);

    graphA.attr("clip-path","url(#graphBClip)");

作为我评论的后续;我刚刚尝试过格雷纳霍曼 https://github.com/w8r/GreinerHormann我链接的图书馆。它玩得很好d3(它以相同的方式获取输入,对象数组)。

这是您的一个简单示例A - B and B - A:

<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected] /cdn-cgi/l/email-protection" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
  <script src="https://rawgit.com/w8r/GreinerHormann/master/dist/greiner-hormann.min.js"></script>
</head>

<body>
  <script>
    // data
    var dataA = [{
      x: 0,
      y: 100,
    }, {
      x: 100,
      y: 150,
    }, {
      x: 200,
      y: 350,
    }, {
      x: 300,
      y: 200,
    }, ];

    var dataB = [{
      x: 0,
      y: 200,
    }, {
      x: 100,
      y: 100,
    }, {
      x: 200,
      y: 250,
    }];
    
    var area = d3.svg.line()
      .x(function(d){
        return d.x;
      })
      .y(function(d){
        return d.y;
      });
      
    var svg = d3.select('body')
      .append('svg')
      .attr('width', 500)
      .attr('height', 500);

    // Graph shapes
    var graphA = svg.append("path")
      .datum(dataA)
      .attr("class", "area")
      .attr("d", area)
      .style({
        fill: 'none'
      });

    var graphB = svg.append("path")
      .datum(dataB)
      .attr("class", "area")
      .attr("d", area)
      .style({
        fill: 'none'
      });
      
    var AminusB = greinerHormann.diff(dataA, dataB);
    var BminusA = greinerHormann.diff(dataB, dataA);
    
    // Graph shapes
    AminusB.forEach(function(d){
      svg.append("path")
      .datum(d)
      .attr("class", "area")
      .attr("d", area)
      .style({
        fill: 'steelblue',
        opacity: 0.8
      });
    });
    
    // Graph shapes
    BminusA.forEach(function(d){
      svg.append("path")
      .datum(d)
      .attr("class", "area")
      .attr("d", area)
      .style({
        fill: 'orange',
        opacity: 0.8
      });
    });
      
  </script>

</body>

</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 D3.js SVG 进行 2D 多边形布尔运算 的相关文章

随机推荐