井井井1314 发表于 2019-7-4 23:03:04

三点画圆函数

路过的老师,下边的XY、XYSM、XYSE是设的什么为未知数?是什么方程,我好像没学过。
'三点创建圆
Public Function AddCircle3P(ByVal pt1 As Variant, ByVal pt2 As Variant, ByVal pt3 As Variant) As AcadCircle

    Dim xysm, xyse, xy As Double
    Dim ptCen(0 To 2) As Double
    Dim radius As Double
    Dim objCir As AcadCircle

    xy = pt1(0) ^ 2 + pt1(1) ^ 2
    xyse = xy - pt3(0) ^ 2 - pt3(1) ^ 2
    xysm = xy - pt2(0) ^ 2 - pt2(1) ^ 2
    xy = (pt1(0) - pt2(0)) * (pt1(1) - pt3(1)) - (pt1(0) - pt3(0)) * (pt1(1) - pt2(1))

    '判断参数有效性
    If Abs(xy) < 0.000001 Then
      MsgBox "所输入的参数无法创建圆形"
      Exit Function
    End If

    '获得圆心和半径
    ptCen(0) = (xysm * (pt1(1) - pt3(1)) - xyse * (pt1(1) - pt2(1))) / (2 * xy)
    ptCen(1) = (xyse * (pt1(0) - pt2(0)) - xysm * (pt1(0) - pt3(0))) / (2 * xy)
    ptCen(2) = 0
    radius = Sqr((pt1(0) - ptCen(0)) ^ 2 + (pt1(1) - ptCen(1)) ^ 2)

    '判断参数有效性
    If Abs(xy) < 0.000001 Then
      MsgBox "所输入的参数无法创建圆形"
      Exit Function
    End If

    Set objCir = ThisDrawing.ModelSpace.AddCircle(ptCen, radius)

    '由于返回值是对象,必须加上set
    Set AddCircle3P = objCir
End Function

satan421 发表于 2019-7-5 14:36:03

xyse=xy-pt3(0)^2-pt3(1)^2,即 (pt1(0) ^ 2 + pt1(1) ^ 2) - (pt3(0) ^ 2 + pt3(1) ^ 2),为啥很接近0?
已经推导出:X=(gb-cf)/(eb-af)Y=(ag-ce)/(af-be)
以Y为例:
=(ag-ce) / (af-be)
=((2x3-2x1)*(x22-x12+y22-y12) - (2x2-2x1)*(x32-x12+y32-y12)) / ((2x3-2x1)*(2y2-2y1) - (2y3-2y1)*(2x2-2x1))
=(((x22+ y22) - ( x12+ y12))*(2x3-2x1)) - ((x32+ y32) - ( x12+ y12))*(2x2-2x1) / 2*((x3-x1)*(y2-y1) - (y3-y1)*(x2-x1))
=(-xysm * (2x3-2x1)) - (-xyse * (2x2-2x1)) / 2*((x3-x1)*(y2-y1) - (y3-y1)*(x2-x1))
=2*(xyse * (x2-x1) - xysm * (x3-x1)) / 2*((x3-x1)*(y2-y1) - (y3-y1)*(x2-x1))
=(xyse * (pt2(0)-pt1(0)) - xysm * (pt3(0)-pt1(0))) / ((pt3(0) - pt1(0))*(pt2(1) - pt1(1)) - ((pt3(1) - pt1(1))*(pt2(0) - pt1(0))))
分子分母同时乘以-1
=(xyse * (pt1(0)-pt2(0)) - xysm * (pt1(0)-pt3(0))) / (((pt3(1) - pt1(1))*(pt2(0) - pt1(0))) - (pt3(0) - pt1(0))*(pt2(1) - pt1(1)))
=(xyse * (pt1(0)-pt2(0)) - xysm * (pt1(0)-pt3(0))) / ((pt1(1) - pt3(1)) * (pt1(0) - pt2(0))) - ((pt1(1) - pt2(1))*(pt1(0) - pt3(0)))
=(xyse * (pt1(0)-pt2(0)) - xysm * (pt1(0)-pt3(0))) / xy
再除以2,不就是ptCen(1)吗?
X同理。





satan421 发表于 2019-7-5 08:44:56

本帖最后由 satan421 于 2019-7-5 14:37 编辑

pt1(0) ^ 2 + pt1(1) ^ 2表示pt1到原点的距离的平方(记为var1)
pt2(0) ^ 2 + pt2(1) ^ 2表示pt2到原点的距离的平方(记为var2)
pt3(0) ^ 2 + pt3(1) ^ 2表示pt3到原点的距离的平方(记为var3)
xyse是var1减去var3,xysm是var1减去var2,var参数求圆心和半径时候会用到


下面的公式成立时表示pt1、pt2、pt3三点共线,三点共线不能构成圆。把公式变形成(pt1(0) - pt2(0)) * (pt1(1) - pt3(1)) -(pt1(0) - pt3(0)) * (pt1(1) - pt2(1))的形式,当差值小于0.000001,就近似的认为三点共线,不能构成圆。

井井井1314 发表于 2019-7-5 11:47:32

牛皮,明白了,谢谢老师

井井井1314 发表于 2019-7-5 11:53:29

本帖最后由 井井井1314 于 2019-7-5 13:38 编辑

satan421 发表于 2019-7-5 08:44
pt1(0) ^ 2 + pt1(1) ^ 2表示pt1到原点的距离的平方(记为var1)
pt2(0) ^ 2 + pt2(1) ^ 2表示pt2到原点的 ...
老师那ptCen的计算方法能给讲讲吗
xyse=xy-pt3(0)^2-pt3(1)^2 很接近等于0了呀

井井井1314 发表于 2019-7-5 11:58:40

@mikewolf2k 好的

井井井1314 发表于 2019-7-5 23:24:23

satan421 发表于 2019-7-5 14:36
xyse=xy-pt3(0)^2-pt3(1)^2,即 (pt1(0) ^ 2 + pt1(1) ^ 2) - (pt3(0) ^ 2 + pt3(1) ^ 2),为啥很接近0?
...

太难了:(:(:(:(:(
页: [1]
查看完整版本: 三点画圆函数