.NET编程求出四个端点组成的两条直线的相交点
本帖最后由 作者 于 2006-8-29 21:08:28 编辑 <br /><br /> <P> '''求出两直线的交点<BR> Public Function inters(ByVal p1 As Point3d, ByVal p2 As Point3d, ByVal p3 As Point3d, ByVal p4 As Point3d, Optional ByVal type As Boolean = True) As Point3d<BR> If Math.IEEERemainder(Math.Abs(getangle(p1, p2) - getangle(p3, p4)), Math.PI) = 0 Then<BR> Return Nothing<BR> End If<BR> Dim a1, a2, b1, b2, c1, c2, c3, x1, x2, y1, y2, x, y, z As Double<BR> Dim lp As Point3d<BR> x1 = p1.X<BR> y1 = p1.Y<BR> x2 = p3.X<BR> y2 = p3.Y<BR> a1 = p2.Y - p1.Y<BR> b1 = p2.X - p1.X<BR> a2 = p4.Y - p3.Y<BR> b2 = p4.X - p3.X<BR> c1 = a1 / b1<BR> c2 = a2 / b2<BR> c3 = (p2.Z - p1.Z) / b1<BR> x = (y2 - c2 * x2 + c1 * x1 - y1) / (c1 - c2)<BR> If b1 = 0 Then<BR> x = p1.X<BR> c1 = 1.0E+20<BR> c3 = c1<BR> End If<BR> If b2 = 0 Then<BR> x = p3.X<BR> c2 = 1.0E+20<BR> End If<BR> y = c1 * (x - x1) + y1<BR> z = c3 * (x - x1) - p1.Z<BR> If x - x1 = 0 Then<BR> y = c2 * (x - x2) + y2<BR> End If<BR> lp = New Point3d(x, y, z)<BR> Dim d1, d2, d3, d4, od1, od2, od3, od4 As Double<BR> d1 = p1.DistanceTo(p2)<BR> d3 = p3.DistanceTo(p4)<BR> od1 = lp.DistanceTo(p1)<BR> od2 = lp.DistanceTo(p2)<BR> od3 = lp.DistanceTo(p3)<BR> od4 = lp.DistanceTo(p4)<BR> If type Then<BR> Return lp<BR> ElseIf od1 <= d1 And od2 <= d1 And od3 <= d3 And od4 <= d3 Then<BR> Return lp<BR> Else<BR> Return Nothing<BR> End If<BR> End Function</P><P>望各位多指点。我没发现只好自已编了一个,但我怕出毛病。</P> <P> '''求出两直线的交点<BR> Public Function inters(ByVal p1 As Point3d, ByVal p2 As Point3d, ByVal p3 As Point3d, ByVal p4 As Point3d, Optional ByVal type As Boolean = True) As Point3d<BR> If Math.IEEERemainder(Math.Abs(getangle(p1, p2) - getangle(p3, p4)), Math.PI) = 0 Then<BR> Return Nothing<BR> End If<BR> Dim a1, a2, b1, b2, c1, c2, c3, x1, x2, y1, y2, x, y, z As Double<BR> Dim lp As Point3d<BR> x1 = p1.X<BR> y1 = p1.Y<BR> x2 = p3.X<BR> y2 = p3.Y<BR> a1 = p2.Y - p1.Y<BR> b1 = p2.X - p1.X<BR> a2 = p4.Y - p3.Y<BR> b2 = p4.X - p3.X<BR> c1 = a1 / b1<BR> c2 = a2 / b2<BR> c3 = (p2.Z - p1.Z) / b1<BR> x = (y2 - c2 * x2 + c1 * x1 - y1) / (c1 - c2)<BR> If b1 = 0 Then<BR> x = p1.X<BR> c1 = 1.0E+20<BR> c3 = c1<BR> End If<BR> If b2 = 0 Then<BR> x = p3.X<BR> c2 = 1.0E+20<BR> End If<BR> y = c1 * (x - x1) + y1<BR> z = c3 * (x - x1) - p1.Z<BR> If x - x1 = 0 Then<BR> y = c2 * (x - x2) + y2<BR> End If<BR> lp = New Point3d(x, y, z)<BR> Dim d1, d2, d3, d4, od1, od2, od3, od4 As Double<BR> d1 = p1.DistanceTo(p2)<BR> d3 = p3.DistanceTo(p4)<BR> od1 = lp.DistanceTo(p1)<BR> od2 = lp.DistanceTo(p2)<BR> od3 = lp.DistanceTo(p3)<BR> od4 = lp.DistanceTo(p4)<BR> If type Then<BR> Return lp<BR> ElseIf od1 <= d1 And od2 <= d1 And od3 <= d3 And od4 <= d3 Then<BR> Return lp<BR> Else<BR> Return Nothing<BR> End If<BR> End Function</P>
<P> </P>
<P>自己编的,请大家评评。</P> 望大侠指点。我对自己编的这个东东不放心。呵……如果程序有就太好了。 单一直线还不错,但是遇到复杂的多段线呢?
页:
[1]