d3 / d3-polygon

Geometric operations for two-dimensional polygons.
https://d3js.org/d3-polygon
ISC License
97 stars 25 forks source link

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

Closed edap closed 4 years ago

edap commented 4 years ago

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.

Fil commented 4 years ago

d3.polygonHull is returning the list of vertices in the polygon hull; you can find an example here https://observablehq.com/d/afff7b4390bb4e91

edap commented 4 years ago

Exactly, polygon hull is not returning a polygon. Is there an equivalent of the previous function? actually, i need it just to get the area, d3.geom.polygon(vertices).area()

Fil commented 4 years ago

Did you try d3.polygonArea(d3.polygonHull(vertices)) ?

edap commented 4 years ago

As I have already said, d3.polygonHull is doing something different. In my case, I should be able to calculate the area of "H" shapes, like https://usercontent2.hubstatic.com/6562915.jpg for example.

Fil commented 4 years ago

A polygon is now represented as an array (ordered list) of its vertices. So, if all you need is the area of a polygon, maybe try d3.polygonArea(vertices), as there's no need to compute the hull.

My apologies if this still does not answer your question. To ask for help, please use Stack Overflow tag d3.js. Thousands of D3-related questions have been asked there, and some answers may be relevant to you.

When asking for help, please include a link to demonstrate the issue, preferably as an Observable notebook. It is often impossible to debug from code snippets alone. Isolate the issue and reduce your code as much as possible before asking for help. The less code you post, the easier it is for someone to debug, and the more likely you are to get a helpful response.

If you have a question about D3’s behavior and want to discuss it with other users, also consider the d3-js Google Group or joining the d3-js Slack.

Thank you! 🤖

edap commented 4 years ago

I see, I did not realized that now it is simply an array. Many thanks for your time and suggestion ;)