明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4033|回复: 5

样条曲线。。。。。。。。。。

[复制链接]
发表于 2003-10-19 10:54:00 | 显示全部楼层 |阅读模式

  1. Private Sub Form_Load()
  2. Const NC = 30, EE = 1E-30
  3. Dim Y0(NC) '数组存放型值点的Y值
  4. Dim Z0(NC) '数组存放型值点的Z值
  5. Dim QY(NC), QZ(NC) '控制多边形上的点的坐标
  6. Dim YTEM(NC), ZTEM(NC) '临时数组
  7. Dim pathf As String '路径
  8. Dim NY As Integer '型值点的个数
  9. Dim YG As Single '给定的Y值
  10. Dim ZQ As Single '待求的Z值
  11. Dim ZG As Single '给定的Z值
  12. Dim YQ As Single '待求的Y值
  13. Dim I, J As Integer


  14. pathf = App.Path
  15. Open pathf & "\XING.TXT " For Input As #1 '已知型值点的输入文件
  16. Open pathf & "\OUTPUT.TXT " For Append As #2
  17. Input #1, NY
  18. For I = 2 To NY + 1
  19. Input #1, Y0(I), Z0(I)
  20. 'Print #2, Y0(I), Z0(I)
  21. Next
  22. Y0(1) = 0#
  23. Z0(1) = 0#
  24. Y0(NY + 2) = 0#
  25. Z0(NY + 2) = 0#
  26. '--------------------------------------------
  27. Call FSQ(NY, Y0(), Z0(), QY(), QZ())
  28. '-------------------------------------------
  29. For I = 1 To 7
  30. Input #1, YG
  31. Call YTOZ(NY, YG, ZQ, Y0(), Z0(), QY(), QZ(), EE)
  32. Print #2, YG, ",", ZQ
  33. Next
  34. '-------------------------------------------
  35. For J = 1 To 10
  36. Input #1, ZG
  37. Call ZTOY(NY, ZG, YQ, Y0(), Z0(), QY(), QZ(), EE)
  38. Print #2, YQ, ",", ZG
  39. Next
  40. Close #1
  41. Close #2
  42. End Sub

  43. '反算由曲线上的点求控制多边形上的点
  44. Sub FSQ(NY, Y0(), Z0(), QY(), QZ())
  45. Const NC = 30
  46. Dim AA(NC), BB(NC), CC(NC) '矩阵的系数

  47. For I = 1 To NY + 2
  48. Print #2, Y0(I), Z0(I)
  49. Next
  50. For I = 2 To NY + 1
  51. AA(I) = 1#
  52. BB(I) = 4#
  53. CC(I) = 1#
  54. Next
  55. AA(NY + 2) = 6#
  56. BB(1) = 6#
  57. BB(NY + 2) = -6#
  58. CC(1) = -6#
  59. 'For I = 1 To NY + 2
  60. 'Print #2, AA(I), BB(I), CC(I) '
  61. 'Next
  62. For I = 1 To NY + 2
  63. Y0(I) = Y0(I) * 6#
  64. Z0(I) = Z0(I) * 6#
  65. Next

  66. Call TRIDAG(AA(), BB(), CC(), Y0(), QY(), (NY + 2))  '调用追赶法子程序
  67. Call TRIDAG(AA(), BB(), CC(), Z0(), QZ(), (NY + 2))  '求控制多边形上的点坐标
  68. 'For I = 1 To (NY + 2)
  69. 'Print #2, QY(I), QZ(I)
  70. 'Next
  71. For I = 1 To NY + 2
  72. Y0(I) = Y0(I) / 6#
  73. Z0(I) = Z0(I) / 6#
  74. Next
  75. End Sub

  76. Sub YZT(YZG, YZ0(), DT, EE) '迭代求T
  77. Dim XX1, XX2, XX, FF1, FF2, FF As Single
  78. XX1 = 0#
  79. XX2 = 1#

  80. FF1 = ((-XX1 ^ 3 + 3 * XX1 ^ 2 - 3 * XX1 + 1) * YZ0(1) + (3 * XX1 ^ 3 - 6 * XX1 ^ 2 + 4) * YZ0(2) + (-3 * XX1 ^ 3 + 3 * XX1 ^ 2 + 3 * XX1 + 1) * YZ0(3) + XX1 ^ 3 * YZ0(4)) / 6 - YZG
  81. FF2 = ((-XX2 ^ 3 + 3 * XX2 ^ 2 - 3 * XX2 + 1) * YZ0(1) + (3 * XX2 ^ 3 - 6 * XX2 ^ 2 + 4) * YZ0(2) + (-3 * XX2 ^ 3 + 3 * XX2 ^ 2 + 3 * XX2 + 1) * YZ0(3) + XX2 ^ 3 * YZ0(4)) / 6 - YZG

  82. If (Sgn(FF2) <> Sgn(FF1)) Then
  83. Do

  84. XX = (XX1 + XX2) / 2#
  85. FF = ((-XX ^ 3 + 3 * XX ^ 2 - 3 * XX + 1) * YZ0(1) + (3 * XX ^ 3 - 6 * XX ^ 2 + 4) * YZ0(2) + (-3 * XX ^ 3 + 3 * XX ^ 2 + 3 * XX + 1) * YZ0(3) + XX ^ 3 * YZ0(4)) / 6 - YZG
  86. If FF = 0# Then
  87. DT = XX
  88. End If
  89. If (Sgn(FF1) = Sgn(FF)) Then
  90. XX1 = XX
  91. FF1 = FF
  92. End If
  93. If (Sgn(FF2) = Sgn(FF)) Then
  94. XX2 = XX
  95. FF2 = FF
  96. End If
  97. Loop While (Abs(XX2 - XX1) > EE And (FF > EE))

  98. End If

  99. DT = (XX1 + XX2) / 2#
  100. End Sub
  101. Sub TRIDAG(A(), B(), C(), R(), U(), N) '解矩阵
  102.     NMAX = 100
  103.     Dim GAM(100)
  104.     If B(1) = 0# Then Exit Sub
  105.     BET = B(1)
  106.     U(1) = R(1) / BET
  107.     For J = 2 To N
  108.         GAM(J) = C(J - 1) / BET
  109.         BET = B(J) - A(J) * GAM(J)
  110.         If BET = 0# Then Exit Sub
  111.         U(J) = (R(J) - A(J) * U(J - 1)) / BET
  112.     Next J
  113.     For J = N - 1 To 1 Step -1
  114.         U(J) = U(J) - GAM(J + 1) * U(J + 1)
  115.     Next J
  116. End Sub
  117. '正算用矩阵法,给定Y求Z值
  118. Sub YTOZ(NY, YG, ZQ, Y0(), Z0(), QY(), QZ(), EE)
  119. Const NC = 30
  120. Dim YZ1(4) '用于计算T值
  121. Dim NI, II, J As Integer '迭待点所在的段
  122. Dim XT As Single



  123. Call LOCATE(Y0(), NY + 1, YG, J%)

  124. NI = J

  125. 'Print #2, YG, NI '待求点在NI段上



  126. '求T值
  127. YZ1(1) = QY(NI - 1)
  128. YZ1(2) = QY(NI)
  129. YZ1(3) = QY(NI + 1)
  130. YZ1(4) = QY(NI + 2)
  131. Call YZT(YG, YZ1(), XT, EE)

  132. 'Print #2, T
  133. ZQ = ((-XT ^ 3 + 3 * XT ^ 2 - 3 * XT + 1) * QZ(NI - 1) + (3 * XT ^ 3 - 6 * XT ^ 2 + 4) * QZ(NI) + (-3 * XT ^ 3 + 3 * XT ^ 2 + 3 * XT + 1) * QZ(NI + 1) + XT ^ 3 * QZ(NI + 2)) / 6
  134. For II = 2 To NY + 1
  135. If YG = Y0(II) Then
  136. ZQ = Z0(II)
  137. End If
  138. Next
  139. End Sub
  140. Sub ZTOY(NY, ZG, YQ, Y0(), Z0(), QY(), QZ(), EE)
  141. '正算用矩阵法,给定Z求Y值
  142. Const NC = 30
  143. Dim YZ2(4) '用于计算T值
  144. Dim NI As Integer '迭待点所在的段数
  145. Dim YT As Single


  146. Call LOCATE(Z0(), NY + 1, ZG, J%)
  147. NI = J
  148. 'Print #2, ZG, NI '待求点在NI段上

  149. YZ2(1) = QZ(NI - 1)
  150. YZ2(2) = QZ(NI)
  151. YZ2(3) = QZ(NI + 1)
  152. YZ2(4) = QZ(NI + 2)
  153. Call YZT(ZG, YZ2(), YT, EE)

  154. YQ = ((-YT ^ 3 + 3 * YT ^ 2 - 3 * YT + 1) * QY(NI - 1) + (3 * YT ^ 3 - 6 * YT ^ 2 + 4) * QY(NI) + (-3 * YT ^ 3 + 3 * YT ^ 2 + 3 * YT + 1) * QY(NI + 1) + YT ^ 3 * QY(NI + 2)) / 6
  155. For II = 2 To NY + 1
  156. If ZG = Z0(II) Then
  157. YQ = Y0(II)
  158. End If
  159. Next
  160. End Sub
  161. Sub LOCATE(XX(), N, X, J%) '判断待求点所在段号
  162.     JL = 0
  163.     JU = N + 1
  164. 10  If JU - JL > 1 Then
  165.         JM = (JU + JL) / 2
  166.         If XX(N) > XX(1) Eqv X > XX(JM) Then
  167.             JL = JM
  168.         Else
  169.             JU = JM
  170.         End If
  171.         GoTo 10
  172.     End If
  173.     J% = JL
  174. End Sub

  175. ===================================
  176. 比如输入文件xing.text如下

  177. 13
  178. 2.9193 0.12086
  179. 4.500 0.3233
  180. 5.117 0.50
  181. 6.000 0.8965
  182. 6.8289 1.50
  183. 7.6308 2.50
  184. 8.1327 3.50
  185. 8.4930 4.50
  186. 8.6820 5.20
  187. 8.7508 5.50
  188. 8.9382 6.50
  189. 9.0758 7.50
  190. 9.1773 8.50
  191. 3
  192. 4
  193. 5
  194. 6
  195. 7
  196. 8
  197. 9
  198. 0.2
  199. 0.8
  200. 1
  201. 2
  202. 3
  203. 4
  204. 5
  205. 6
  206. 7
  207. 8
发表于 2003-10-19 18:20:00 | 显示全部楼层
又有人往上贴了,这好象是继明总之后的关于VBA的第一个。
不过freshairMM,这叫源码共享,你这好象还不够“源”啊,现在这里人气不旺,你就全部拿出来吧。
 楼主| 发表于 2003-10-19 21:55:00 | 显示全部楼层
呵呵,已经比较源了,我没有保留啊。今晚来不及了,如有问题,我明天再补上。
发表于 2003-11-21 13:31:00 | 显示全部楼层
[求助]freshair好象和我是同行,我是做船厂船舶设计的,请问有没有曲线拟合的程序?
就是给出一组数据把需要的比如三次多项式或者乘幂的曲线得出来。
发表于 2003-12-22 06:50:00 | 显示全部楼层
楼主的程序用在什么地方呢?
发表于 2005-11-1 14:00:00 | 显示全部楼层

要是可以有象CorelDRAW 畫的曲線多好啊!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 16:02 , Processed in 0.176436 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表