# 花花Ĳ

## matlab求两直线的夹角

(2011-05-31 20:56:38)

lyqmath

# 1 根据节点计算直线方程并求交点

## 代码

% By lyqmath

% DLUT School of Mathematical Sciences

% BLOG：http://blog.csdn.net/lyqmath

clc; clear all; close all;

%% 符号变量

syms x1 y1 x2 y2 x3 y3 x4 y4

syms C

syms y

%% 计算A1，B1，A2，B2

eq1 = A*x1 + B*y1 + C;

eq2 = A*x2 + B*y2 + C;

sov1 = solve(eq1, eq2, A, B);

eq3 = A*x3 + B*y3 + C;

eq4 = A*x4 + B*y4 + C;

sov2 = solve(eq3, eq4, A, B);

A1 = simplify(sov1.A / C);

B1 = simplify(sov1.B / C);

A2 = simplify(sov2.A / C);

B2 = simplify(sov2.B / C);

% A1 = (y1 - y2)/(x1*y2 - x2*y1);

% B1 = (-x1 + x2)/(x1*y2 - x2*y1);

% A2 = (y3 - y4)/(x3*y4 - x4*y3);

% B2 = (-x3 + x4)/(x3*y4 - x4*y3);

%% 求交点

eq1 = A1*x + B1*y + 1;

eq2 = A2*x + B2*y + 1;

sov = solve(eq1, eq2, x, y);

pretty(eq1)

pretty(eq2)

pretty(sov.x)

pretty(sov.y)

## 结果

% By lyqmath

% DLUT School of Mathematical Sciences

% BLOG：http://blog.csdn.net/lyqmath

function [px, py] = comput_intersec(line1, line2)

p1 = line1.point1; p2 = line1.point2; 第1条边的两点

p3 = line2.point1; p4 = line2.point2; 第2条边的两点

x1 = p1(1); y1 = p1(2);

x2 = p2(1); y2 = p2(2);

x3 = p3(1); y3 = p3(2);

x4 = p4(1); y4 = p4(2);

px = (x1*x3*y2 - x2*x3*y1 - x1*x4*y2 + x2*x4*y1 - x1*x3*y4 + x1*x4*y3 + x2*x3*y4 - x2*x4*y3 )/...

(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);

py =  (x1*y2*y3 - x2*y1*y3 - x1*y2*y4 + x2*y1*y4 - x3*y1*y4 + x4*y1*y3 + x3*y2*y4 - x4*y2*y3 )/...

(x1*y3 - x3*y1 - x1*y4 - x2*y3 + x3*y2 + x4*y1 + x2*y4 - x4*y2);

# 2 计算直线夹角

theta1 = acosd(dot([x1-x2,y1-y2],[x3-x2,y3-y2])/(norm([x1-x2,y1-y2])*norm([x1-x2,y3-y2])));

# 4 结论

• 评论加载中，请稍候...

验证码： 请点击后输入验证码 收听验证码

发评论

以上网友发言只代表其个人观点，不代表新浪网的观点或立场。

新浪BLOG意见反馈留言板　不良信息反馈　电话：4006900000 提示音后按1键（按当地市话标准计费）　欢迎批评指正

新浪公司 版权所有