Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Merge line segments
Replies: 4   Last Post: Feb 12, 2013 11:07 AM

 Messages: [ Previous | Next ]
 Peter Bone Posts: 409 Registered: 1/25/05
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

Date Subject Author
2/11/13 Peter Bone
2/11/13 Bruno Luong
2/11/13 Peter Bone
2/11/13 Bruno Luong
2/12/13 Peter Bone