intersection

Best way to get intersection of keys of two objects?

谁说胖子不能爱 提交于 2019-11-29 00:04:11
问题 I have two object literals like so: var firstObject = { x: 0, y: 1, z: 2, a: 10, b: 20, e: 30 } var secondObject = { x: 0, y: 1, z: 2, a: 10, c: 20, d: 30 } I want to get the intersection of the keys these two object literals have like so: var intersectionKeys = ['x', 'y', 'z', 'a'] I can obviously do a loop and see if a key with the same name exists in the other object, but I am wondering if this would be a good case for some functional programming and map / filter / reduce usage? I myself

C# fastest intersection of 2 sets of sorted numbers

大兔子大兔子 提交于 2019-11-28 23:08:40
问题 I'm calculating intersection of 2 sets of sorted numbers in a time-critical part of my application. This calculation is the biggest bottleneck of the whole application so I need to speed it up. I've tried a bunch of simple options and am currently using this: foreach (var index in firstSet) { if (secondSet.BinarySearch(index) < 0) continue; //do stuff } Both firstSet and secondSet are of type List. I've also tried using LINQ: var intersection = firstSet.Where(t => secondSet.BinarySearch(t) >=

efficiently knowing if intersection of two list is empty or not, in python [duplicate]

拟墨画扇 提交于 2019-11-28 21:13:32
This question already has an answer here: Test if lists share any items in python 9 answers Suppose I have two lists, L and M. Now I want to know if they share an element. Which would be the fastest way of asking (in python) if they share an element? I don't care which elements they share, or how many, just if they share or not. For example, in this case L = [1,2,3,4,5,6] M = [8,9,10] I should get False, and here: L = [1,2,3,4,5,6] M = [5,6,7] I should get True. I hope the question's clear. Thanks! Manuel Or more concisely if set(L) & set(M): # there is an intersection else: # no intersection

Generate new polygons from a cut polygon (2D)

帅比萌擦擦* 提交于 2019-11-28 20:49:22
I'm stuck with this little problem and my algorithm to solve this doesn't hold for all cases. Does anybody has an idea how to solve this? Here's an example polygon: example http://img148.imageshack.us/img148/8804/poly.png Formal description We have a list of points in CW order defining the polygon. We also can query whether a point is a cutting point with is_cut(p) , where p is a given point. Now we want to compute new polygons caused by this "cut". The algorithm should do this: Input: {a, c1, b, c4, c, c5, d, c6, e, c3, f, c2} Output: {a, c1, c2} , {b, c4, c3, f, c2, c1} , {d, c6, c5} , {e,

The intersection point between a spline and a line

喜欢而已 提交于 2019-11-28 20:49:14
I'm trying to find a way to calculate the intersection between a b-spline and a straight line. So far Google hasn't been much help. The most efficient algorithm that I've heard of is called Bezier clipping. Here's a book chapter on curve and spline intersection (pdf). A pure mathematical approach: Transform the spline and the line so that the line lies on the X axis. Calculate the points on the spline where Y = 0 (depends on the order of the spline). Transform these points back to your original cordinate system. If this is the way you are going I can work out the necessary formulas. Your best

Determining if two rays intersect

五迷三道 提交于 2019-11-28 18:47:51
I have two rays on a 2D plane that extend to infinity, but both have a starting point. They are both described by a starting point and a vector in the direction of the ray extending to infinity. I want to find out if the two rays intersect, but I don't need to know where they intersect (it's part of a collision detection algorithm). Everything I have looked at so far describes finding the intersection point of two lines or line segments. Is there a fast algorithm to solve this? Given: two rays a, b with starting points (origin vectors) as, bs, and direction vectors ad, bd. The two lines

Finding the intersection of two arrays [closed]

◇◆丶佛笑我妖孽 提交于 2019-11-28 13:27:55
My aim is to find out values of intersection of arrays a and b and store them into a new array c so the printout will be : 3,10,4,8. How do I assign given values to a 3rd array c ? public static void main(String[] args) { int a[] = {3, 10, 4, 2, 8}; int[] b = {10, 4, 12, 3, 23, 1, 8}; int[] c; int i=0; for(int f=0;f<a.length;f++){ for(int k=0;k<b.length;k++){ if(a[f]==b[k]){ //here should be a line that stores equal values of 2 arrays(a,b) into array c } } } for (int x=0; x<c.length; x++){ System.out.println(c[i]); } } } sakthisundar This should be an easy way to do. int a[] = {3, 10, 4, 2, 8}

Find cells in array that are crossed by a given line segment

醉酒当歌 提交于 2019-11-28 12:41:14
问题 I have got a 2D array of cells with size 10x10, and many points that are pairs of floating point values, like: (1.6, 1.54), (4.53, 3.23). The pairs (x,y) are such that x<10 and y<10 Each cell takes points whose coordinates have the same integer part as the cell coordinates. So arr[3][7] will take points with x={3...3.99(9)} and y={7... 7.99(9)}, for example (3.5, 7.1) or (3.2, 7.6). Similarly (1.6, 1.54) is in arr[1][1] , (4.53, 3.23) is in arr[4][3], etc. Each point has got a designated

What's the algorithm of 'set.intersection()' in python?

本秂侑毒 提交于 2019-11-28 12:25:05
First of all, my purpose is to randomly get only one element in both known sets. So my original method is firstly intersect two sets. And then randomly pick up a element from the intersected set. But this is foolish, because that I only need a elements but a intersected set. So I need to find the algorithm of set.intersection(). I compare the cost time between the methods of 'set.intersection()' and 'for{for{}}'. Set.intersection() is more faster than other one(100 times). So using 'for{for{}}' to pick up a randomly elements is not a wise idea. What's the algorithm behind set.intersection() in

Swift: Is there an easy way to draw shapes and detect whether they intersect? [duplicate]

做~自己de王妃 提交于 2019-11-28 11:28:36
This question is an exact duplicate of: Swift: detecting intersection from sprite kit SKShapeNode drawings 1 answer Is there an easy way to draw shapes in Swift (preferrably with Sprite-Kit) and then detect if and where they intersect? Like here's an intersecting shape: Rob If this consists of a series of line segments, one can adapt Martin R's answer to UIBezierPath intersect to not only detect intersections, but to also identify where the intersections are: func intersectionBetweenSegments(p0: CGPoint, _ p1: CGPoint, _ p2: CGPoint, _ p3: CGPoint) -> CGPoint? { var denominator = (p3.y - p2.y)