Find Nodes inside Path - path

I want the player to draw on the screen, creating a path. If he closes the encircling path, I want to do stuff to the nodes inside the path.
How do I know which nodes are inside the path?

I assume you store the coordinates of all your nodes or have a reference to them in a variable.
You can make use of CGRectContainsPoint to check for nodes inside the closed path by making use of the node's coordinate.
To get the rect from a CGPath,from the documentation
func CGPathGetBoundingBox(_ path: CGPath!) -> CGRect

Related

How to detect a sknode hovers over another?

I am making a game where I drag pieces over containers and if they match it can be placed if not, then returns to the bin, How can I detect if a sknode is hovering over the container, or how can I compare the 2 sprites? Thank you! any help would be great! :)
To see if they are hovering over one another, you could use the intersects function of CGRect using each nodes frame property (which is a CGRect). If the frames intersect, then they are overlapping to some extent.
To identify if the nodes are the same shape, then I’d restrict yourself to a specific range of shapes and have the nodes name property reflect what shape it is.
Edit: You could of course also use Sprite-kit's contact detection. Set it up correctly and you;ll only get notified when the correct shapes are placed over the correct containers.

How can I determine whether a point is inside the path after the clipping path has been applied?

I'm currently drawing an angular gradient using a method similar to the one shown by Rob's answer here. I have added an extension to CGContext to draw an angular gradient and would like it to handle the clipping path, so that callers can treat it similar to a method like fillPath(using:).
I have tried using func pathContains(CGPoint, mode: CGPathDrawingMode) on CGContext and func contains(CGPoint, using: CGPathFillRule, transform: CGAffineTransform) on the CGPath returned from the context.
Both of these appear to not use the clipping path, so my drawing goes outside the clipping area.
Is there a way to get either the path clipped already or a copy of the current clipping path so I can also check whether the pixel is contained inside that path also?
You should be able to check if the point is inside your unclipped path and check if the point is inside the clipping path. The clipped path is the intersection fo the clipping path and your path, so a point is inside the clipped path only it its inside both your unclipped path and inside the clipping path.

How do you place an SKSpriteNode at a given length of a path?

I would like to animate sprites along the one path but have them each start from different locations along that path.
Does anyone know if there is a time offset function anywhere? I have looked but cant find one.
Thanks,
Macro

Bezier path by filling path?

I have a bezier path that comes into my program having been created from an SVG and the problem is it is just an outline. I need to perform hit detection on it, anywhere on the interior of the shape. Is there an equivalent of CGPathCreateCopyByStrokingPath for filling it?
CGContextFillPath will fill a path within a context when it is drawn.
CGPathCreateCopyByStrokingPath takes a path and returns a new path describing the outline of the shape that would be formed when stroking that path. It returns a CGPathRef. So if you can perform hit detection on the results of that, then you can perform them directly on your original path. You don't need a conversion.
Supposing I've misunderstood the question, you can use CGPathContainsPoint to test whether a point is inside a path. Use that directly on what you get from your SVG.

Calculate UIBezierPath Frame in iOS

I implement a free hand drawing module using UIBezierPath, but i want to get the all frame of the curve after i finish drawing, how can i do it.
And if there a way to get the path point for a connected path, it will be great.
Your question is not entirely clear, but if you want the bounding rectangle of a UIBezierPath, there is a bounds property.
If you want to know the position of the last point to connect another path, there is the currentPoint property.
If you want to examine the different parts of the path, you'll have to go down to the underlying CGPath, and the CGPathApply function.
If you want to test if a point is inside the path (would be filled), you can use the containsPoint: method, and if you want to test if a point is part of the path, you will have to do the math yourself using the Bézier formulas...

Resources