algorithm - How to find minimal and maximal angle determined by three points selected from n points? -
given n points in 2-d plane, (0,0),(1,1), ... can select 3 points them construct angle. example, choose a(0, 0), b(1, 1), c(1, 0), angle abc = 45 degree, acb = 90 degree , cab = 45 degree.
my question how calculate max or min angle determined 3 points selected n points.
obviously, can use brute-force algorithm - calculate angels , find maximal , minimal value, using law of cosines calculate angles , pythagorean theorem calculate distances. efficient algorithm exist?
if i'm correct, brute-force runs in o(n^3): take every triplet, compute 3 angles, , store overall max.
you can improve o(n^2 * log(n)) it's trickier:
best_angle = 0 each point p1: each point p2: compute vector (p1, p2), , signed angle makes x-axis store in array sort array # o(n * log(n)) # traverse find best choice: each alpha in a: element beta in closest alpha+pi # takes o(log n) because it's sorted. don't forget take account fact represents circle: both ends touch... best_angle = max(best_angle, abs(beta - alpha)) the complexity o(n * (n + nlog(n) + n * log(n))) = o(n^2 * log(n))
of course can retrieve pt1, pt2 obtained best angle during loops.
there still better, feels doing work overall, if re-use previous computations of pt1 pt2, ..., ptn ...
Comments
Post a Comment