gurkesaft Posted August 9, 2021 Share Posted August 9, 2021 Hi all, I've been poking around the web and documentation for some time now and feel I'm missing something basic, and cannot figure this out: Suppose I have a "master" container with a bunch of objects (containers) in it, and one of those objects (also a container) has within it a Graphics object in which I've drawn a polygon. Is there an easy way to use the Graphics.containsPoint() to test whether a coordinate from the master container is within the Polygon? I'm open to other (especially *faster*) solutions, e.g., if Graphics.containsPoint() is slow compared to using a Polygon or Rectangle object. Thanks! Jack PS- Sidequest: is there an efficient way to test whether things like Polygons and Graphics in different containers overlap? Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted August 9, 2021 Share Posted August 9, 2021 > to test whether a coordinate from the master container is within the Polygon? The most effective is: graphics._recursivePostUpdateTransform(); const tempPoint = new Point(); graphics.containsPoint(graphics.transform.localTransform.applyInverse(myPoint, tempPoint)); you may omit updateTransform or call how it works internally if you are sure that matrix is already updated. otherwise, use "grahics.toLocal(point, container, tempPoint)", it takes care of everything but it requires extra "apply" > PS- Sidequest No. Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 9, 2021 Author Share Posted August 9, 2021 Wow that was fast and insane, thank you. I'll give this a shot. > otherwise, use "grahics.toLocal(point, container, tempPoint)", it takes care of everything but it requires extra "apply" Is this slower? If not, how would the code look? I'm a little concerned that the deep code you sent above will change in future implementations of Pixi, so if there's a "cleaner" implementation I'm for it. Another option I thought of would be to translate all the polygon vertices into the master container's coordinates once, THEN loop over my many, many objects in the master, so that I don't have to waste cpu on every object's transformation. Let me know if you have thoughts on this approach. Thanks again; I hope you're paid for your prolific work. ivan.popelyshev 1 Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 9, 2021 Author Share Posted August 9, 2021 ... So for the last idea, I guess the question becomes, "how does one most easily convert container.x and container.y to the x and y of the parent container?" Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 9, 2021 Author Share Posted August 9, 2021 To which I will reply that I'm going to try container.localTransform.applyInverse(myPoint) I will post back if it works out. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted August 9, 2021 Share Posted August 9, 2021 > Thanks again; I hope you're paid for your prolific work. My main job pays the bill, I rarely have debts, so I'm in much better position than many others. I support the idea that knowledge has to be free, like https://en.wikipedia.org/wiki/Sci-Hub > Let me know if you have thoughts on this approach. Its still the same number of vertices that have to be transformed. My old fork of pixijs had lazy calculation of local, world and camera vertices. I think it actually depends on whole algorithm that you want to apply and on bottlenecks. Tell me more and maybe i can tell what you have to do Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 10, 2021 Author Share Posted August 10, 2021 I've got hundreds of points in the master container, and I need to check if they're all in a few-point polygon living in the child container's coordinates. My current plan is to do one inverse transform of the polygon and then loop over the hundreds of points to see if they're within it. -- I agree about free knowledge, and, luckily, scientific grant agencies are moving toward the requirement of open access and open data (it's part of the budget in the applications now). Arxiv is helping with that, and I think combining arxiv with some kind of stack-exchange-like upvoting and peer-review system with tags for sorting would replace journals. But that's a separate discussion... But don't burn out. Most of the answers I see about pixi seem to come from you and your sticks of dynamite. Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 10, 2021 Author Share Posted August 10, 2021 Okay, so it turns out to convert child container coordinates (x,y) to parent container coordinates, one takes child_container.localTransform.apply( new PIXI.Point(x,y) ); This returns a PIXI.Point in the parent container coordinates. applyInverse() does some pretty wonky stuff. Thanks again!! Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted August 10, 2021 Share Posted August 10, 2021 (edited) You asked > Is there an easy way to use the Graphics.containsPoint() to test whether a coordinate from the master container is within the Polygon? Ist applyInverse. For graphics -> parent its "apply" Dont forget about updateTransform fun ) Edited August 10, 2021 by ivan.popelyshev Quote Link to comment Share on other sites More sharing options...
gurkesaft Posted August 10, 2021 Author Share Posted August 10, 2021 Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.