Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Meeting of Line endpoints

Subject: Meeting of Line endpoints

From: audley james

Date: 17 Oct, 2010 21:02:03

Message: 1 of 5

Hi guys,

I have 3 lines , labelled accordingly as '1' , '2' and '3'.

Basically I want to have the relevant endpoints of lines '2' and '3' meet at the relevant endpoints of line '1'.

Below is my code. I have 2 blocks of the same code, with one showing what I currently have and what I'd like to achieve in my Line drawing.

Basically, for the portion of code with What I'd like to Achieve , I manually modified the 'Lines' matrix accordingly to suit.

I'd like to ask for you guys' help on automatically determining where to ensure lines 2 and 3 meet at the relevant endpoints of line 1(whose coordinates are always fixed).

 I'd like to prevent stuff happening where for example Line's 2 endpoint ends up at the end point where ideally Line 3 is supposed to connect to that specific endpoint of Line 1.

I know I'm asking a bit. But help if you can.

Cheers
audley


%----------------------- Currently what I have ------------------------------------
clc;
clear all;
close all;

Lines = [ 8.199 9.128 24.2 191.48
                        11 9.2977 66 12.625
                        10 191.22 106 193.03 ]
 

  X1 = Lines(:,1)
  Y1 = Lines(:,2)
  X2=Lines(:,3)
  Y2= Lines(:,4)
           K=[ X1 Y1 X2 Y2] ;
     K_pts = reshape(K.',2,[]).';
    midpt =[ (X1+X2)/2 (Y1+Y2)/2 ] ;
              midx = midpt(:,1);
              midy = midpt(:,2);

for i = 1:length(X1)
 draw_lines(i)=line([ X1(i) X2(i)], [Y1(i) Y2(i)] ,'Color', 'r','LineStyle','-','Linewidth' ,1.5 );
  text(midx(i),midy(i),num2str(i),'FontSize',11,'Color','b');
end

%---------------------------------------------------------------------------------------------------------


%----------------------- What I'd like to get ---------------------------------------------------
clc;
clear all;
% close all;


 
Lines = [ 8.199 9.128 24.2 191.48
                         8.199 9.128 66 12.625
                       24.2 191.48 106 193.03 ]
                    
                    
  X1 = Lines(:,1)
  Y1 = Lines(:,2)
  X2=Lines(:,3)
  Y2= Lines(:,4)
           K=[ X1 Y1 X2 Y2] ;
     K_pts = reshape(K.',2,[]).';
    midpt =[ (X1+X2)/2 (Y1+Y2)/2 ] ;
              midx = midpt(:,1);
              midy = midpt(:,2);
figure,
for i = 1:length(X1)
 draw_lines(i)=line([ X1(i) X2(i)], [Y1(i) Y2(i)] ,'Color', 'r','LineStyle','-','Linewidth' ,1.5 );
  text(midx(i),midy(i),num2str(i),'FontSize',11,'Color','b');
end

%-------------------------------------------------------------------------------------------------

Subject: Meeting of Line endpoints

From: ImageAnalyst

Date: 18 Oct, 2010 00:30:31

Message: 2 of 5

I'm not sure I know what you're asking. Why can't you just assign the
values?

Starting with
Lines = [ 8.199 9.128 24.2
191.48;...
                        11
9.2977 66 12.625;...
                        10
191.22 106 193.03 ]


newLines = Lines;
newLines(2,1) = Lines(1,1);
newLines(2,2) = Lines(1,2);
newLines(3,1) = Lines(1,3);
newLines(3,1) = Lines(1,4);
newLines % Display in command window


Results:

Lines =

    8.1990 9.1280 24.2000 191.4800
   11.0000 9.2977 66.0000 12.6250
   10.0000 191.2200 106.0000 193.0300

newLines =

    8.1990 9.1280 24.2000 191.4800
    8.1990 9.1280 66.0000 12.6250
   24.2000 191.4800 106.0000 193.0300

Any reason why it has to be harder than this?

Subject: Meeting of Line endpoints

From: audley james

Date: 18 Oct, 2010 01:16:03

Message: 3 of 5

thanks for the post man,

I have clarified what i want to achieve. It has to be more complicated than simply assigning the values since in my application it may not always work out that way, i.e. an endpoint in line 1 may connect with the 2nd point line 2 and so on depending on the distance between the points.

So my problem can be defined as:

I have matrix:

[ x1 y1 x2 y2;
  x3 y3 x4 y4;
  x5 y5 x6 y6]

where the first row is Line 1 , the 2nd row is Line 2, and the 3rd row is Line 3.

I'm trying to to do following:

 The Euclidean distance is computed for the following combinations.

         Euclid_dist_of_P1L1_&_P1L2 = sqrt ((x3 - x1)^2 + (y3-y1)^2)
         Euclid_dist1_of_P1L1_&_P2L2 = sqrt ((x4 - x1)^2 + (y4-y1)^2)

where if Euclid_dist_of_P1L1_&_P1L2 <(less than) Euclid_dist1_of_P1L1_&_P2L2 , make the following replacement change where (x3 y3) is replaced by (x1 y1) and update the original input matrix:

[ x1 y1 x2 y2;
   x1 y1 x4 y4;
  x5 y5 x6 y6]

If this is not the case then and the situation is Euclid_dist_of_P1L1_&_P1L2 >(greater than) Euclid_dist1_of_P1L1_&_P2L2, then the following change is made to the original update matrix:

[ x1 y1 x2 y2;
  x3 y3 x1 y1;
  x5 y5 x6 y6]

which means thats Point 1 of Line1 is closer to Point 2 of Line 2 and should be connected to it.

Then, I wish to then repeat the process for Point 2 of Line1 (i.e. x2 y2) and the Two points in Line 3 (i.e. x5 y5 and x6 y6).

          Euclid_dist_of_P2L1_&_P1L3 = sqrt ((x5 - x2)^2 + (y5-y2)^2)
          Euclid_dist1_of_P2L1_&_P2L3 = sqrt ((x6 - x2)^2 + (y6-y2)^2)

where if Euclid_dist_of_P2L1_&_P1L3 < Euclid_dist1_of_P2L1_&_P2L3

(x5 y5) is replaced with (x2 y2)

otherwise, if Euclid_dist_of_P2L1_&_P1L3 > Euclid_dist1_of_P2L1_&_P2L3 ,instead the following happens:

(x6 y6) is replaced with (x2 y2)


Using my original input matrix:

Lines =

                     8.199 9.128 24.2 191.48
                        11 9.2977 66 12.625
                        10 191.22 106 193.03

The result I'm working on is:

Lines =

                     8.199 9.128 24.2 191.48
                     8.199 9.128 66 12.625
                      24.2 191.48 106 193.03

I'm struggling to make progress with the coding as I'm posting this
So ,if anyone can help with this complicated coding , please do.

take care
audley

Subject: Meeting of Line endpoints

From: ImageAnalyst

Date: 18 Oct, 2010 01:42:47

Message: 4 of 5

Okaaaay . . . . so why can't you just do this with a bunch of if
statements and assignments? Maybe hypot() will make it easier for
you.

Subject: Meeting of Line endpoints

From: audley james

Date: 18 Oct, 2010 05:20:04

Message: 5 of 5

thanks, I did it.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us