已知两条直线各自两个端点,求它们的夹角和中心线的公式

turbochen 2005-01-07 09:28:41
请提供它们的公式,给出程序算法更好。非常感谢!
...全文
3320 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzwu 2005-01-13
  • 打赏
  • 举报
回复
How do I find intersections of 2 2D line segments?

This problem can be extremely easy or extremely difficult; it
depends on your application. If all you want is the intersection
point, the following should work:

Let A,B,C,D be 2-space position vectors. Then the directed line
segments AB & CD are given by:

AB=A+r(B-A), r in [0,1]
CD=C+s(D-C), s in [0,1]

If AB & CD intersect, then

A+r(B-A)=C+s(D-C), or

Ax+r(Bx-Ax)=Cx+s(Dx-Cx)
Ay+r(By-Ay)=Cy+s(Dy-Cy) for some r,s in [0,1]

Solving the above for r and s yields

(Ay-Cy)(Dx-Cx)-(Ax-Cx)(Dy-Cy)
r = ----------------------------- (eqn 1)
(Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

(Ay-Cy)(Bx-Ax)-(Ax-Cx)(By-Ay)
s = ----------------------------- (eqn 2)
(Bx-Ax)(Dy-Cy)-(By-Ay)(Dx-Cx)

Let P be the position vector of the intersection point, then

P=A+r(B-A) or

Px=Ax+r(Bx-Ax)
Py=Ay+r(By-Ay)

By examining the values of r & s, you can also determine some
other limiting conditions:

If 0<=r<=1 & 0<=s<=1, intersection exists
r<0 or r>1 or s<0 or s>1 line segments do not intersect

If the denominator in eqn 1 is zero, AB & CD are parallel
If the numerator in eqn 1 is also zero, AB & CD are collinear.

If they are collinear, then the segments may be projected to the x-
or y-axis, and overlap of the projected intervals checked.

If the intersection point of the 2 lines are needed (lines in this
context mean infinite lines) regardless whether the two line
segments intersect, then

If r>1, P is located on extension of AB
If r<0, P is located on extension of BA
If s>1, P is located on extension of CD
If s<0, P is located on extension of DC

Also note that the denominators of eqn 1 & 2 are identical.


zzwu 2005-01-13
  • 打赏
  • 举报
回复
要将(-PI to PI)转变为(0 to 2PI),只要值为负时 +2PI 就行了。
zzwu 2005-01-13
  • 打赏
  • 举报
回复
函数 atan2 在 Turbo C 中就已经有了,但Turbo Pascal 中没有,要自己编。
syy64 2005-01-12
  • 打赏
  • 举报
回复
没研究过atan2函数,它从-PI to PI,我要求从0 to 2PI,不知道会不会有所不同。
rickone 2005-01-12
  • 打赏
  • 举报
回复
l1:(x1,y1)-(x2,y2)
l2:(x3,y3)-(x4,y4) 先考虑二维的
l1的方向数:d1=(x2-x1,y2-y1)
l2 ... :d2=(x4-x3,y4-y3)
cos<l1,l2>=cos<d1,d2>=d1*d2/|d1|*|d2| 或者它的余角

如果你说的中心线指的角平分线。
设中心线上的点M(x,y),先写出两直线的方程
l1:f1(x,y)=0
l2:f2(x,y)=0
利用点到两直线距离相等得一方程g(x,y)=0,即为所求。
寻开心 2005-01-10
  • 打赏
  • 举报
回复
自己看msdn去
atan, atan2
Calculates the arctangent of x (atan) or the arctangent of y/x (atan2).

double atan( double x );
double atan2( double y, double x );

atan returns the arctangent of x. atan2 returns the arctangent of y/x. If x is 0, atan returns 0. If both parameters of atan2 are 0, the function returns 0. You can modify error handling by using the _matherr routine. atan returns a value in the range –π/2 to π/2 radians; atan2 returns a value in the range –π to π radians, using the signs of both parameters to determine the quadrant of the return value.
mxfeng 2005-01-10
  • 打赏
  • 举报
回复
atan2 是怎么 函数?
寻开心 2005-01-10
  • 打赏
  • 举报
回复
这段程序在忙什么呢?
如果是计算角度,干嘛不直接用atan2这个函数计算呢,直接得到了2PI范围内的角度
atan做不到的, atan2可以做到啊.
syy64 2005-01-09
  • 打赏
  • 举报
回复
将一条直线置平,将另一条直线的端点变换到新坐标系中,看这个帖子:
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=202254
计算角度:
// 计算角度 2002.4.8.
double WINAPI CalAngle(double x, double y)
{
double dAngle;
if(fabs(x)>0)
{
dAngle = atan(y/x)/PI*180.0;
if(dAngle>0)
{
if(x<0)dAngle += 180.0;
}
else if(dAngle<0)
{
if(y>0)dAngle = 180+dAngle;
else dAngle = 360+dAngle;
}
else
{
if(x<0) dAngle = 180;
}
}
else
{
if(y>0)dAngle = 90.0;
else dAngle = 270.0;
}
return dAngle;
}
中心线也好求。
寻开心 2005-01-07
  • 打赏
  • 举报
回复
两个向量v1和v2,找到另外一个向量,使得v1*v = v2*v而已
对于v1,v2在这个题目下,都可以是非单位的
那么

(v.x * v1.x + v.y*v1.y)/|v1| = (v.x * v2.x + v.y*v2.y) / |v2|
于是有
v.x = -|v1|v2.y + |v2|v1.y
v.y = |v1|v2.x - |v2|v1.x
的时候上面的等式成立

因此这个平分向量就是 ( -|v1|v2.y + |v2|v1.y, |v1|v2.x - |v2|v1.x)
事实上,这个向量的垂直向量,就是v1. -v2的平分向量

对于这两个线段来说, v1,v2的平分向量以及它的垂直向量都是可能的答案

找到了这个向量后,就是找一个公共点了
这个公共点不希望以计算交点的方法来解决
因为如果两个线段平行,那么就是找不到这个交点
我们可以通过找一个到两条直线距离都相等的特殊点的办法来实现
寻开心 2005-01-07
  • 打赏
  • 举报
回复
角平分线?
那就是到这两个线段所在直线距离相等的直线
这样的直线是有可能有两条的啊

还是当作二维来理解吧
在三维情况下,可能最多的时候是有两个平面的

是角平分线吗?
turbochen 2005-01-07
  • 打赏
  • 举报
回复
我说的中心线是指穿过夹角中心并经过交点的一条直线。最好能给出这个中心线的方程。
寻开心 2005-01-07
  • 打赏
  • 举报
回复
更正一下:
夹角 = acos( (dx1*dx2+dy1*dy2)/sqrt((dx1*dx1++dy1*dy1)*(dx2*dx2+dy2*dy2)) )

原理和上面说的向量方式是一致的,不过是简化了独立的取模操作
line1(p1, p2); line2(p3, p4)
向量v1 = p2-p1
向量v2 = p4-p3;
v1*v2 = |v1| * |v2|* cos (v1,v2之间的夹角)
所以两个线段之间的夹角就是
acos ( v1*v2 / |v1|*|v2| )
v1*v2 = (p2-p1) * (p4-p3)
|v1|*|v2| = ( |p2-p1| * |p4-p3| )
简化后就是前面的公式了
在使用的时候,需要注意sqrt内部的数值不能是0,否则就除0
sqrt内部部分为0的充要条件就是 一个线段退化成为了一个点

这个计算结果返回的是两个线段之间 0到180度 之间的弧度角
mxfeng 2005-01-07
  • 打赏
  • 举报
回复
假设两条直线的两个点分别为 直线1(a1,b1),(a2,b2) 直线2(m1,n1) (m2,n2),
首先判断是不是平行,

(a2-a1)/(b2-b1)=(m2-m1)/(n2-n1) 平行和重合无焦点,

然后 列公式,联力解方程组,得交点,
能得到一个公式 即交点.x=?; 交点.y=?,
求角度,比较麻烦,得比较直线的走向,即比较x1和x2的大小,得是1,3象限的走向,还是2,4象限的走向,然后求sin值,转成(0-360)的角度
*****************************************************************************************
下面的式子,希望对你有用:
是在窗体左上为(0,0)右下为(max,max)中, 已知半径1000,原点(X1,y1),角度@(人类的坐标系), 得到圆周上的点(x2,y2)坐标值
sangleline.X2 = 1000 * Cos(2 * pi * (@ - 90) / 360) + sangleline.X1
sangleline.Y2 = 1000 * Sin(2 * pi * (@ - 90) / 360) + sangleline.Y1
寻开心 2005-01-07
  • 打赏
  • 举报
回复
对于任何一个线段, 都可以转换成为向量形式 (V, start, len)
v:表示方向向量,单位的
start表示开始点
len表示长度
这种方式表达的两个线段的夹角就是各自的方向向量的点积然后求acos数值即可

如果是二维空间的,使用顶点坐标直接计算的方法就是(三维空间也很类似的):
两个线段四个顶点坐标 line1{(x1,y1), (x2,y2)}, line2{(x3,y3), (x4,y4)}
夹角为
dx1 = x2-x1;
dx2 = x4-x3;
dy1 = y2-y1;
dy2 = y4-y3;
夹角 = acos( (dx1*dx2+dy1*dy2)/sqrt(dx1*dx1+dx2*dx2+dy1*dy1+dy2*dy2) )

线段的中心线是什么概念
两个线段的中心点的连线?那不直接就出来了
bogenfeng 2005-01-07
  • 打赏
  • 举报
回复
cosB=a^2+b^2+c^2-2cosAcosB

太繁了,应该有简单的.
mxfeng 2005-01-07
  • 打赏
  • 举报
回复
直线公式

x-x1 y-y1
-----==-----
x2-x y2-y
turbochen 2005-01-07
  • 打赏
  • 举报
回复
UPUP
寻开心 2005-01-07
  • 打赏
  • 举报
回复
v1 和 v2 两个向量, 把他们单位化,或者是模取相同的时候,把两个向量做加法, 也可以得到中心线的方向向量了
因为平行四边形的对角线是平分夹角的

即:
v1/|v1| + v2/|v2|
就是这个方向向量了

如果两个线段不平行,找到交点,和这个向量合成就是所求了
寻开心 2005-01-07
  • 打赏
  • 举报
回复
办法有很多, 但是你最好都是把公式依次推倒出来, 然后看看那些地方可以简化的
上面给出的东西都是这样来的

因为实际上, 答案都是唯一的
得到它的路径有多个
无论是那个都可以
关键是对最终的结果的表达方式,如何最简单最有效才是好的

比如,关于中心线的角度,完全可以计算出一个直线的角度,再加上夹角来生成,如果加法不对就用减法,顶多多一次测试就知道正确的角度,可是那样计算就复杂一些,得到的结果肯定是一样的
加载更多回复(2)

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧