thelonious / kld-intersections

A library of intersection algorithms covering all SVG shape types
BSD 3-Clause "New" or "Revised" License
343 stars 55 forks source link

[Feature request] Check if a point is inside a shape #16

Closed marcospassos closed 6 years ago

marcospassos commented 7 years ago

It would be nice to have a method that checks if a 2DPoint is inside or outside the boundaries of a given shape.

thelonious commented 7 years ago

This is implemented in some of the intersection routines. However, my current thinking is to pull this functionality out into a separate IntersectionQuery class. The current intersection code could be streamlined a bit (and made a little faster as a result) if I remove this kind of logic. Likewise, these types of queries often involve simpler math than calculating intersections.

Out of curiosity, do you need this for any shapes in particular?

marcospassos commented 7 years ago

For now, we're using a suboptimal solution that involves creating a line with as a point Shapes.line(x-0.5, y-0.5, x, y) (once 0.5 is less than one pixel, no collateral effects here). However, a more robust solution would be very appreciated :).

Out of curiosity, are you looking for this to supported for any shapes in particular?

As the shape is configurable, we're not looking for a form in particular.

However, my current thinking is to pull this functionality out into a separate IntersectionQuery class.

Please, let me know if decide to pull out such feature.

thelonious commented 7 years ago

Are you looking for something like this? Please note that these have not been tested in any way, but it should give you an idea of what I'm thinking.

marcospassos commented 7 years ago

Yeah, but I would like to suggest an API more close to the Intersection:

const shape = Shapes.circle(10, 10, 50);
const point = new Point2D(10, 20);

IntersectionQuery.containsPoint(shape, point);

What do you think?

thelonious commented 7 years ago

Agreed, something like that would be nicer. My thinking is that I would approach this like Intersection where you could call the low-level API directly (which is what I'm using in my example) and then there would be a convenience method that figures out what method to invoke for you. The low-level methods use kld-affine types and Numbers only. The convenience function would use types like you're showing in your example: i.e., IntersectionArgs.

marcospassos commented 7 years ago

Perfect! It definitely gets the job done. Looking forward to testing it on our app.

Btw, as this new class is completely isolated, it looks like a good point to start with unit tests :)

marcospassos commented 7 years ago

@thelonious, any plans to release the InteserctionQuery?

thelonious commented 7 years ago

I started a new full-time job this month which has limited my time considerably. Is it useful to you in its current form? There's not much there.

Grepsy commented 7 years ago

It would be very useful to me, even in it's current form. I'm mainly looking for the pointInPolygon functionality. Would it be possible to release the current IntersectionQuery stuff?

thelonious commented 6 years ago

Thanks @Grepsy. I went ahead and pushed v0.4.0. I incremented the minor revision since this is an API change, but really all this does is expose IntersectionQuery. I've also added the start of a new section on that class in the README

Grepsy commented 6 years ago

Great, thanks a lot!