Swift - how to fill a path (opaque path) -
i have path should have shape of isometric tile:
let isometricpath = cgpathcreatemutable() cgpathmovetopoint(isometricpath, nil, 0, -(tilesize.height / 2)) cgpathaddlinetopoint(isometricpath, nil, (tilesize.width / 2), 0) cgpathaddlinetopoint(isometricpath, nil, 0, (tilesize.height / 2)) cgpathaddlinetopoint(isometricpath, nil, -(tilesize.width / 2), 0) cgpathclosesubpath(isometricpath)
and try make opaque path line of code:
let isometricpathref = isometricpath cgpathref
but if want check if cgpoint inside path this:
cgpathcontainspoint(isometricpathref, nil, locationinnode, true)
it detect point on path not inside.
how make possible?
thanks
your code correct should @ origins of scene. sure can see shape?
by default, scene’s origin placed in lower-left corner of view. so, default scene initialized height of 1024 , width of 768, has origin (0,0) in lower-left corner, , (1024,768) coordinate in upper-right corner. frame property holds (0,0)-(1024,768).
suppose have example using question code:
let tilesize = cgsizemake(150.0,150.0) // code: let isometricpath = cgpathcreatemutable() cgpathmovetopoint(isometricpath, nil, 0, -(tilesize.height / 2)) cgpathaddlinetopoint(isometricpath, nil, (tilesize.width / 2), 0) cgpathaddlinetopoint(isometricpath, nil, 0, (tilesize.height / 2)) cgpathaddlinetopoint(isometricpath, nil, -(tilesize.width / 2), 0) cgpathclosesubpath(isometricpath)
this path represent rhombus shape.
now if want draw shape can do:
let shape = skshapenode.init(path: isometricpath) shape.strokecolor = skcolor.yellowcolor() shape.fillcolor = skcolor.bluecolor() self.addchild(shape)
but nothing showed because scene origin start 0.0 , shape have negative values.
to show rhombus can change scene anchorpoint to:
self.anchorpoint = cgpointmake(0.5,0.5)
this centers scene’s origin in middle of view (apple docs) , can see tile:
now suppose want know if point inside shape:
let locationinnode = cgpointmake(tilesize.width/15,tilesize.height/15) // point inside rhombus
i want show point debug:
let circle = skshapenode.init(circleofradius: 5) circle.strokecolor = skcolor.whitecolor() circle.fillcolor = skcolor.whitecolor() self.addchild(circle) circle.position = locationinnode
now can check if point inside rhombus:
let isometricpathref = isometricpath cgpathref print(cgpathcontainspoint(isometricpathref, nil, locationinnode, true))
output:
Comments
Post a Comment