Matrices and Line Intersection

11 次查看(过去 30 天)
Ping
Ping 2012-10-18
I am having difficulty rewriting my function. want to write a function that determines whether or not 2 lines intersect. I know I have to use the backslash to solve for the intersection of 2 equations, but I don't really know where to go from there so I tried my best.
The equations of the lines are :
ax + by = c
px + qy = r
  • The inputs, a, b, c, p, q, and r correspond to the coefficients of the equations above. The outputs, xc, yc, flag will be determined as follows:
  • If an intersection point exists, the value of flag is 1 and LineIntersect returns the intersection points: xc and yc.
*
  • If many intersection points exist, the value of flag is 2, and LineIntersect returns xc and yc as an empty vector.
*
  • If no intersection point exists, the value of flag is 3, and LineIntersect returns xc and yc as an empty vector.
This is the code I tried to write. Sorry for the many errors, I tried my best.
function [xc yc flag] = LineIntersect(a,b,c,p,q,r)
a.*x + b.*y = c
p.*x + q.*y = r
if x*c/y*c
flag=1
disp ('xc' 'yc')
elseif x*c/y*c
flag=2
('xc' 'yc')
elseif x*c/y*c
flag=3
disp('xc' 'yc')
flag= yc ./xc
end
Here test cases if it makes understanding the problem easier:
href="http://s1308.photobucket.com/albums/s617/pingchu93/?action=view&current=Problem2TestCases_zpse6318f5e.jpg" target="_blank" rel="nofollow noopener noreferrer"<img src="http://i1308.photobucket.com/albums/s617/pingchu93/Problem2TestCases_zpse6318f5e.jpg" border="0" alt="Problem 2 Test Cases"></a>
thanks.

回答(1 个)

Matt J
Matt J 2012-10-18
Hints:
Consider the quantity
rank([a b c; p q r]) - rank([a b; p q])
What does it tell you if it is greater than 0? Also, what does it tell you if rank([a b ; p q])<2 ?

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by