- 积分
- 5966
- 明经币
- 个
- 注册时间
- 2003-1-7
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
前天一位读者提到,VBA精彩实例一书3.10一节中,计算点到直线距离的函数PtToLine有误,经测试确实存在,在此向广大读者道歉,并且附上新的代码:
' 计算两点之间的距离 Function Distance(Pt1, Pt2 As Variant) As Double Distance = ((Pt1(0) - Pt2(0)) ^ 2 + (Pt1(1) - Pt2(1)) ^ 2) ^ 0.5 End Function
' 点到直线的距离 Private Function PtToLine(ByVal pt As Variant, ByVal ptStart As Variant, ByVal ptEnd As Variant) As Double Dim a As Double, b As Double, c As Double, l As Double ' a、b、c为三点组成的三角形边长,l为周长的一半 ' 计算三角形的边长 a = Distance(pt, ptStart) b = Distance(pt, ptEnd) c = Distance(ptStart, ptEnd) l = (a + b + c) / 2 ' 计算三角形面积 Dim area As Double area = Sqr(l * (l - a) * (l - b) * (l - c)) ' 根据(面积=(底*高)/2)计算点到直线距离 PtToLine = 2 * area / c End Function
写的比较着急,如果有问题请大家多多指正。 |
|