How to intersect two polygons?

Arash Partow’s FastGEO library contains implementations of many interesting algorithms in computational geometry. Polygon intersection is one of them. It’s written in Pascal, but it’s only implementing math so it’s pretty readable. Note that you will certainly need to preprocess your edges a little, to get them into clockwise or counterclockwise order. ETA: But really, … Read more

Intersection of two strings in Java

Using HashSet<Character>: HashSet<Character> h1 = new HashSet<Character>(), h2 = new HashSet<Character>(); for(int i = 0; i < s1.length(); i++) { h1.add(s1.charAt(i)); } for(int i = 0; i < s2.length(); i++) { h2.add(s2.charAt(i)); } h1.retainAll(h2); Character[] res = h1.toArray(new Character[0]); This is O(m + n), which is asymptotically optimal.

Finding the intersection between two series in Pandas

Place both series in Python’s set container then use the set intersection method: s1.intersection(s2) and then transform back to list if needed. Just noticed pandas in the tag. Can translate back to that: pd.Series(list(set(s1).intersection(set(s2)))) From comments I have changed this to a more Pythonic expression, which is shorter and easier to read: Series(list(set(s1) & set(s2))) … Read more

Difference between extending and intersecting interfaces in TypeScript?

Yes there are differences which may or may not be relevant in your scenario. Perhaps the most significant is the difference in how members with the same property key are handled when present in both types. Consider: interface NumberToStringConverter { convert: (value: number) => string; } interface BidirectionalStringNumberConverter extends NumberToStringConverter { convert: (value: string) => … Read more

Finding Intersection of NSMutableArrays

Use NSMutableSet: NSMutableSet *intersection = [NSMutableSet setWithArray:array1]; [intersection intersectSet:[NSSet setWithArray:array2]]; [intersection intersectSet:[NSSet setWithArray:array3]]; NSArray *array4 = [intersection allObjects]; The only issue with this is that you lose ordering of elements, but I think (in this case) that that’s OK. As has been pointed out in the comments (thanks, Q80!), iOS 5 and OS X 10.7 … Read more