Giter Club home page Giter Club logo

d3-polygon's Introduction

d3-polygon's People

Contributors

aboorvadevarajan avatar bumbeishvili avatar dependabot[bot] avatar fil avatar kay-is avatar mbostock avatar stof avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

d3-polygon's Issues

Hull issue (for v3)

I have created a block for you to view which shows the error very clearly here:
http://bl.ocks.org/Anupheaus/f79c12c2cec45caa9dfd1e6466663ef2

Basically, the issue is that when you add a new array of points to the hull data, two of the new points seem to affect the hull before the transition is applied. This only happens if the points being added are to the right of the original point, so if the second point was to the left of the first point, it doesn't happen.

Clipping?

Is it worth having some sort of polygon clipping functionality?

Note: d3-voronoi implements its own clipping of infinite cells. And d3-geo-projection will need to have an equivalent of d3.geo.clipExtent.

Rendering?

Although it’s pretty trivial to take a polygon and render it to a Canvas 2D context or an SVG path string, I wonder if it would be beneficial to make some explicit support for rendering in this package. (Related d3/d3-hexbin#1.) An interesting use case to consider is how to render the output of d3-voronoi.

Merge into d3-geometry package?

It might be nice to have a slightly larger module containing d3-hull, d3-quadtree, d3-polygon, d3-voronoi… and whatever else we imagine in the future.

Porting old function d3.geom.polygon to its newer version

Hello, I am porting an existing code-base that is using an old version of d3 to the new version.
In this old code-base, polygons are generated using d3.geom.polygon. the package https://www.npmjs.com/package/d3-geom-polygon is being moved to d3-polygon but unfortunately I do not see an equivalent of this function, polygonHull it is accepting points as argument but it is doing something different. What I am looking at, is a function that accepts points and gives me back a polygon.

Why is Polygon.hull() result not counterclockwise ?

The test below shows that the expected result is clockwise, not counterclockwise as mention in the documentation.

test.deepEqual(polygon.polygonHull([[200, 200], [760, 300], [500, 500]]), [[760, 300], [200, 200], [500, 500]]);

Is it due to the fact that the y-axis direction of SVGs is going down the screen, and hence inverting some common computation results ?

Point-in-polygon test.

From d3.geo.clipExtent:

function insidePolygon(p) {
  var wn = 0, // the winding number counter
      n = polygon.length,
      y = p[1];

  for (var i = 0; i < n; ++i) {
    for (var j = 1, v = polygon[i], m = v.length, a = v[0], b; j < m; ++j) {
      b = v[j];
      if (a[1] <= y) {
        if (b[1] >  y && d3_cross2d(a, b, p) > 0) ++wn;
      } else {
        if (b[1] <= y && d3_cross2d(a, b, p) < 0) --wn;
      }
      a = b;
    }
  }
  return wn !== 0;
}

polygonContains should be uniform when testing the verteces of the polygon

var d3Polygon = require("d3-polygon")
var unitSquare = [[0,0], [0,1], [1,1], [1,0]];
var polygonContains = d3Polygon.polygonContains;
unitSquare.forEach( p => console.log(`[${p}] in unitSquare : ${polygonContains(unitSquare, p)}`));

>"[0,0] in unitSquare : true"
>"[0,1] in unitSquare : false"
>"[1,1] in unitSquare : false"
>"[1,0] in unitSquare : false"

In Runkit => https://runkit.com/embed/tht1jbzi6iqq

Imho, all the verteces of the polygon should be either inside or outside.

Add concaveHull (in addition to convexHull)

d3-polygon currently has d3.polygonHull(points) that returns the convex hull of the specified points. For some applications, it would be handy to calculate the concave hull of a set of points.

Either, the existing method could add a second parameter type (with currently supported values being convex and concave, or, the existing method could be renamed to d3.polygonHullConvex(points) and a new method d3.polygonHullConcave(points) could be added.

More easily import polygon points

Since the polygon points are a simple string of "x1,y1 x2,y2, ... xn,yn" in the SVG XML, I found it much simpler to modify your polygonArea function to utilize the string values rather than try to convert to your expected nested arrays. Very minor tweaks adding a few splits, but it seems much more friendly for working with polygons on the fly. Feel free to incorporate them if you find them useful.

            var svgDoc = document.getElementById("svg");
            var string = d3.event.target.id;
            var poly = d3.select(svgDoc).select("polygon#" + string);
            var points = poly.attr("points");
            var polygon = points.split(" ");            
            var i = 0,
                n = polygon.length - 1,
                a,
                b = polygon[n].split(","),
                area = 0;
            while (i++ < n)    {
                a = b;
                b = polygon[i].split(",");
                area += a[1] * b[0] - a[0] * b[1];
            }
            area /= 2;

Point x and y accessors

Hi,

I'm using d3-polygon's functions on a large amount of nodes during a force simulation.
The problem is, a point in d3-polygon is an array [x<Number>, y<Number>] while a node in d3-force is {x: <Number>, y: <Number>, ...}, hence converting nodes to [x, y] slows down the simulation because of temporary arrays being garbage collected all the time.

As an example, since polygonHull requires [​[x1, y1], [x2, y2], …] I need to do d3.polygonHull(nodes.map(node => [node.x, node.y])).

It would be useful to have x, y accessors to get node.x and node.y by reference and eventually avoiding the cost of duplication.

What do you think?

One approach would be to specify x, y accessors in all d3-polygon's functions arguments, but I'd prefer to consume an API similar to the rest of d3:

// data is an array of generic objects
d3.polygon([data])
  .x(fn)
  .y(fn)
  .vertices([data])
  .hullOf(data) // calculates the hull and stores the resulting polygon's vertices (not the input points)
  .area()
  .centroid()
  .length()
  .contains(point)

If it helps I'm willing to try a PR, basically imitating the voronoi approach.

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.