[公告]VBA精彩实例一书的更正
前天一位读者提到,VBA精彩实例一书3.10一节中,计算点到直线距离的函数PtToLine有误,经测试确实存在,在此向广大读者道歉,并且附上新的代码:' 计算两点之间的距离<BR>Function Distance(Pt1, Pt2 As Variant) As Double<BR> Distance = ((Pt1(0) - Pt2(0)) ^ 2 + (Pt1(1) - Pt2(1)) ^ 2) ^ 0.5<BR>End Function
' 点到直线的距离<BR>Private Function PtToLine(ByVal pt As Variant, ByVal ptStart As Variant, ByVal ptEnd As Variant) As Double<BR> Dim a As Double, b As Double, c As Double, l As Double ' a、b、c为三点组成的三角形边长,l为周长的一半<BR> <BR> ' 计算三角形的边长<BR> a = Distance(pt, ptStart)<BR> b = Distance(pt, ptEnd)<BR> c = Distance(ptStart, ptEnd)<BR> l = (a + b + c) / 2<BR> <BR> ' 计算三角形面积<BR> Dim area As Double<BR> area = Sqr(l * (l - a) * (l - b) * (l - c))<BR> <BR> ' 根据(面积=(底*高)/2)计算点到直线距离<BR> PtToLine = 2 * area / c<BR>End Function
写的比较着急,如果有问题请大家多多指正。 广告!!!
送你一个O
页:
[1]