
Merge line segments
Posted:
Feb 11, 2013 10:19 AM


Given 2 line segments (4 points), I want to return a third which consists of the the 2 points that are furthest apart. I have a function that works, but is there are way to do it more efficiently (faster) as this is a bottleneck in my code?
% Return the 2 points that are furthest apart % Input and output line indexing is l(endpoint, x/y(1/2)) function l = merge_lines(l1, l2) len_max =(l2(1,1)  l1(1,1))^2 + (l2(1,2)  l1(1,2))^2; l(1,:) = l1(1,:); l(2,:) = l2(1,:); len = (l2(1,1)  l1(2,1))^2 + (l2(1,2)  l1(2,2))^2; if len > len_max len_max = len; l(1,:) = l1(2,:); l(2,:) = l2(1,:); end len = (l2(2,1)  l1(1,1))^2 + (l2(2,2)  l1(1,2))^2; if len > len_max len_max = len; l(1,:) = l1(1,:); l(2,:) = l2(2,:); end len = (l2(2,1)  l1(2,1))^2 + (l2(2,2)  l1(2,2))^2; if len > len_max len_max = len; l(1,:) = l1(2,:); l(2,:) = l2(2,:); end len = (l1(2,1)  l1(1,1))^2 + (l1(2,2)  l1(1,2))^2; if len > len_max len_max = len; l = l1; end len = (l2(2,1)  l2(1,1))^2 + (l2(2,2)  l2(1,2))^2; if len > len_max l = l2; end

