Creating a new MKPolygon from two intersecting polygons - ios

I know that there is exactly one specific question that talks about this but its a bit old and I wanted to get a little more technical about it if possible.
First of all take a look at this screenshot: https://www.dropbox.com/s/f94q3qaxrog0ec9/intersections.png
Want I want to have happen is say "I see where they both intersect and where that happens I do not want to draw those 'parts' of the polygon". In this case i would like to draw both polygons but not draw the 'path' of the yellow polygon that is inside the orange polygon. This include not only the line but the fill color as well.
Obviously this gets tricky because you aren't always going to have exact points along the path of each polygon that intersect exactly with other points along another polygon path. As you can see from this screenshot, in order to make this possible, I think, I need to get the points back where they do intersect.
After that and assuming that I can do this, I do not know if there is a way to use the CGPathRef class to do what i need to do. Obviously I am going to have re-create a polygon but according to the docs you cant fill it in unless it is a closed path. So how do you fill in the polygon that is not interesecting without closing the path??
The only thing that i can think of would be to "hide" the parts of the polygon that intersect but unsure how to do this. Can anyone offer any help/insight to this problem??

Here you go. I wrote an Objective-C wrapper around Alan's GPC library. Check out MKPolygon-GPC

Ok guys! well i went off and created my own MKPolygon category to solve what i needed to solve. I hope it is also useful to others!
the github link is: https://github.com/geeksweep/MKPolygon-GSPolygonIntersections

After days of investigation, here is the solution I found for Swift 4:
1) Go here and clone the project
2) Drag and drop the following in your project:
- MKPolygon+GPC.m
- MKPolygon+GPC.h
- gpc232 (folder)
3) Create a bridging header (here is a tutorial)
4) Open your bridging header file and add the following :
#import "MKPolygon+GPC.h"
5) In your View Controller , use this method to union your two Polygons :
let mergedPolygon = polygon1.fromUnion(with:polygon2)
NOTE : there is a crash in the current library, I fixed it but I am waiting my pull request to be reviewed. Thanks to SunGard-Labs for the framework !

Related

How to use globalCompositeOperation in Konva for multiple shapes with scaling

I'm trying to create polygons with an inner border in Konva.
I found this example of doing this with globalCompositeOperation which works well in Konva, as long as there is only one shape. As soon as I try to add a second shape, this obviously doesn't work anymore and the first shape disappears.
It would work if I were to use a different layer for every shape, but of course that's not a solution that scales well.
I tried using a temporary layer as is done in the example but couldn't get it to work.
So I found this example of using group.cache(), which works fine ... until I try to scale the stage, at which point I would have to refresh the cache, otherwise I only get the scaled up cache, which looks bad.
This codesandbox illustrates the problem. (Please note that this uses simple triangles, in reality I work with arbitray polygons)
So is there a way to use cache with scaling? Or alternatively a better way to use globalCompositeOperation with multiple shapes in the same layer? Or some alternative solution?
I found a solution: calling group.cache({pixelRatio: scaleFactor}). I updated the sandbox.
No idea, if this is the best solution, but it works.

How to get the enclosed subpaths from a CGPath

I am working on a drawing app. I have implemented the drawing string feature using CGPath. The next feature is that I can click on the view. If where I click includes an enclosed area, I can fill it with other colors. I am thinking that I need to get the enclosed subpath first, then fill color to it. Very much appreciate it if anyone can give me any idea. Thanks.
For you to understand it, I post a picture.
Okay, finally I found what I want is the "Flood Fill" Algorithm. If anyone wants to look for the referring article, please see here

Keeping track of an object and painting behind

I have been struggling with this problem for a time and being unable to solve it led me here. I'm recently new to Actionscript (2.0). I want to do something similar to:
http://gnarshmallow.com/
Were i want something to be painted behind a moving object in real time.
I would like some advice on how to approach the problem.
You need to use line drawing to do this. You will need two points, and it will draw a line from one to the next. I recommend having it run on every movement call. Have it draw the line between the racer's location in the previous frame, and his location in the current frame. For further reference, check out this page.
http://www.actionscript.org/resources/articles/730/1/Drawing-lines-with-AS2/Page1.html

Drawing lines in cocos2d

I'm trying to draw lines in Cocos2d using touches.
I had a system where it would just add a small sprite where you touched, but it's working terribly. So I've been trying to find a way to draw actual lines using a method like ccDrawLine, but every tutorial I find seems to leave out something, and I just can't figure it out.
I've found this tutorial, Drawing line on touches moved in COCOS2D but I don't understand a few things about that.
It seems to reference the same variable from two different files, so I don't understand how it's doing that. (The naughtyTouchArray variable)
I can't find a complete guide on drawing lines, so sorry for the codeless question, but I'm getting frustrated.
Thanks.
The answer you've linked in your question provides good solution to your problem. There is no "two different files". Just two different methods of one layer. One method (ccTouchesMoved:withEvent:) handles touches and fill the array of points to be connected to each other one-by-one with lines. From cocos2d documentation, all drawing must be placed in the draw method of the node. So, another (draw) method just draws lines according to the given array. Cocos2d is based on OpenGL and it fully redraws scene every tick, so you cannot just draw new line. You had to draw all of them.
Or any other node can draw your array in it's draw method, so you can simply pass stored array of points from the layer, that detects touches, to this node.

How to get MKPolylines follow the roads properly in MKMapView (iOS)?

)
I'm trying to put a direction polyline on a MKMapView in my iOS project, using Google Directions API. I used the code found in this blog : http://iosguy.com/2012/05/22/tracing-routes-with-mapkit/ , which works quite well.
But the problem I find is that the polyline do not follow the roads, instead it draws lines straight from one point to another :-(
Can anyone please help me to make it follow the roads ? Thanks :-)
Instead of using the coordinates of the route steps, you can use the *overview_polyline* element of the Google Directions response to draw a smooth line following the roads. You can use this code to decode the polyline string: https://gist.github.com/3770235
edit: I erroneously made the assumption you where using the steps coordinates, as that is usually the cause of these straight lines, but the link you post already uses the *overview_polyline*. In that case my answer is probably not solving your problem. Decoding the polyline as a category on MKPolyline could be a nice addition to your code though.

Resources