Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


Math Forum » Discussions » Software » comp.soft-sys.matlab

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

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Peter Bone

Posts: 409
Registered: 1/25/05
Merge line segments
Posted: Feb 11, 2013 10:19 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.