How to intersect these lines?

1 view (last 30 days)
Emmanuel
Emmanuel on 29 May 2014
Commented: Emmanuel on 29 May 2014
Hi all! Given below is the code for plottong three lines given any two arbitrary points per line. I have to find the vertices of the intersection points of these lines. I am getting the plot , but they are not intersecting. I have attached the image below, please help!!
%For Line 1
[x1] = input('Enter the value of 1st coord');
[y1] = input('Enter the value of 1st coord');
t = linspace(0,1);
xa = x1(1)+ (y1(1)-x1(1))*t;
ya = y1(1) + (y1(2)-x1(1))*t;
%For Line 2
[x2] = input('Enter the value of 1st coord');
[y2] = input('Enter the value of 1st coord');
u = linspace(0,1);
xb = x2(1)+ (y2(1)-x2(1))*u;
yb = y2(1) + (y2(2)-x2(1))*u;
%For Line 3
[x3] = input('Enter the value of 1st coord');
[y3] = input('Enter the value of 1st coord');
v = linspace(0,1);
xc = x3(1)+(y3(1)-x3(1))*v;
yc = y3(1)+(y3(2)-x3(1))*v;
plot(xa,ya,xb,yb,xc,yc);

Answers (1)

David Sanchez
David Sanchez on 29 May 2014
I give you the code for two lines, you extend it to three (do it by pairs
if true
% code
end):
%For Line 1
[x1] = [2 3];%input('Enter the value of 1st coord');
[y1] = [3 4];%input('Enter the value of 1st coord');
ma = (y1(2)-y1(1))/(x1(2)-x1(1));
na = y1(1) - ma*x1(1);
%For Line 2
[x2] = [2 4];%input('Enter the value of 1st coord');
[y2] = [2 -3];%input('Enter the value of 1st coord');
mb = (y2(2)-y2(1))/(x2(2)-x2(1));
nb = y2(1) - mb*x2(1);
syms x
solve(x*ma + na == x*mb + nb)
ans =
12/7
  2 Comments
David Sanchez
David Sanchez on 29 May 2014
syms x
x=solve(x*ma + na == x*mb + nb)
x=
12/7
y=x*ma+na
y =
19/7
Emmanuel
Emmanuel on 29 May 2014
Hi! Thankyou for your reply. With regard to your reply for the previous post, the code is here:
for i=1:1:3
[A] = input('enter the 1st co-ordinate of the line');
[B] = input('enter the 2nd co-ordinate of the line');
xlim = [-10 10];
m = (B(2)-B(1))/(A(2)-A(1));
n = B(2)*m - A(2);
y1 = m*xlim(1) + n;
y2 = m*xlim(2) + n;
hold on
line([xlim(1) xlim(2)],[y1 y2])
plot(y1,y2)
hold off
end
%SAMPLE OUTPUT
%enter the 1st co-ordinate of the line[1,5]
%enter the 2nd co-ordinate of the line[3,2.5]
%enter the 1st co-ordinate of the line[3,1]
%enter the 2nd co-ordinate of the line[4,1]
%enter the 1st co-ordinate of the line[4,1]
%enter the 2nd co-ordinate of the line[4.5,5.8]
When I plot this on a paper and the output graph by matlab varies a lot. if i change the xlim to [0 20] , the lines do not intersect at all. Why does the output from matlab varies from that of the paper graph even if I use the same scale? Ihave attached the output plot. Can we like rectify it?

Sign in to comment.

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!