Git Product home page Git Product logo

pathintersection's Introduction

Intersection of CGPathRefs

CGPaths can contain arcs, lines, rectangles, etc. These can then be stroked or filled on a CGContext, given a certain line color, thickness, and cap. In the case of this project, two paths are stroked on two separate contexts, and then any pixel based collisions are detected. This allows an application to programmatically check if two CGPaths intersect, and find where they intersect. This could be useful, for instance, for having a user work their finger around a maze, and checking if they hit a wall.

Although pixel-by-pixel collision detection is not the most efficient method, it is straight forward, and it gets the job done, possibly even better than raw mathematical detection would, since it emulates what the user would actually be seeing if the paths were drawn on screen.

Usage

Given two CGPathRef variables, lastPath and currentPath, you could detect if and where they intersect as follows:

ANPathBitmap * bm1 = [[ANPathBitmap alloc] initWithPath:lastPath];
ANPathBitmap * bm2 = [[ANPathBitmap alloc] initWithPath:currentPath];
bm1.lineCap = kCGLineCapRound;
bm2.lineCap = kCGLineCapRound;
bm1.lineThickness = 4;
bm2.lineThickness = 4;
[bm1 generateBitmap];
[bm2 generateBitmap];
CGPoint intPoint;
ANPathIntersection * intersection = [[ANPathIntersection alloc] initWithPathBitmap:bm1
                                                                       anotherPath:bm2];
if ([intersection pathLinesIntersect:&intPoint]) {
    NSLog(@"Point: %@", NSStringFromCGPoint(intPoint));
} else {
    NSLog(@"No intersection");
}

Since both paths are virtually stroked onto a CGContext, both the line thickness and the line cap must be explicitly specified for the detection to be accurate.

An Example on Youtube

I have posted an example of this in the workings on Youtube so that all can see. Check it out.

pathintersection's People

Contributors

unixpickle avatar

Watchers

Adam Wulf avatar  avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.