c++ - Return pairs of points from N 2D points where each two points define a line -


i have list of 2d points: (x1, y1), (x2, y2) … (xn, yn) - n 2d points. each 2 points define 2d line.

return list of unique 2d lines, can build using pairs of points list.

how can implement using hash table/map - keep unique lines (there infinite lines)

i trying find slope , intercept point of intersection.

slope = y2 -y1 / x2 - x1 intercept = y1 - slope * x1;

(trying in c++)

you didn't specify language, use python here sake of easiness. joke python:

import itertools   def slope(p1, p2):     return (p1[1]-p2[1]) / (p1[0]-p2[0])   def slp_intrcpt(p1, p2):     """     return tuple of slope , intercept     """     slp = slope(p1, p2)     return slp, p1[1] - slp * p1[0]   def uniq_lines(points):     return set(slp_intrcpt(p1, p2) p1, p2 in itertools.combinations(points, 2)) 

since (slope, intercept) pair enough determine line, finished requirement.

if want keep track of pairs produce lines, may

import collections  def uniq_lines(points):     d = collections.defaultdict(lambda: [])     p1, p2 in itertools.combinations(points, 2):         d[slp_intrcpt(p1, p2)].append((p1, p2))     return d 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -