home *** CD-ROM | disk | FTP | other *** search
/ BUG 11 / BUGCD1998_02.ISO / aplic / turbocad / tcw.z / fern23.bas < prev    next >
BASIC Source File  |  1997-05-05  |  9KB  |  408 lines

  1. '******************************************************************
  2. '*                                                                *
  3. '*                      TurboCAD for Windows                      *
  4. '*                   Copyright (c) 1993 - 1996                    *
  5. '*             International Microcomputer Software, Inc.         *
  6. '*                            (IMSI)                              *
  7. '*                      All rights reserved.                      *
  8. '*                                                                *
  9. '******************************************************************
  10. '
  11. ' Filename: FERN23.BAS
  12. '
  13. ' Author:    Pat Garner
  14. '
  15. ' Date:        1/13/97
  16. '
  17. '
  18. ' Scriptname:    Fractal Fern Generator
  19. '
  20. ' Version:        2.3
  21. '
  22. ' Description:    Adaptation of the Fern Fractal Generator
  23. '                 that uses functions from the TurboCAD 
  24. '                 scripting API to construct a new graphic
  25. '                 object 
  26. '
  27. '
  28. ' New to version 2.3:    Add property loops from ATS script
  29. '                         series to be used immediately to
  30. '                         test the TCWGraphicPropertyGet/Set
  31. '                         API functions scripting functions.
  32. '                        
  33. '                        The long term goal would be to hook
  34. '                         the functions into a property 
  35. '                         page/wizard dialog to give the 
  36. '                         user more depth of control on how
  37. '                         the graphic is created.  
  38. '
  39. '                        To take this a step further, the script 
  40. '                         could check to see if a selected graphic
  41. '                         is a fern graphic.  If so, the script
  42. '                         could bring up it's 'property page' 
  43. '                         dialog and allow the user to modify it's
  44. '                         attributes ala smart shape action. 
  45. '
  46. '                                                -pg 2/7/97
  47. '
  48. '        
  49. '
  50. '
  51. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  52.  
  53.  
  54.   
  55.  
  56.  
  57. ''SCRIPT VARIABLES'''''''''''''''''''''''''''''''''''''''''''''''''
  58. '
  59. ' - Script Constants
  60. '
  61. Global Const MAX_POINTS = 100000        ' * Total number of points to be created
  62. Global Const MY_TRUE = 1                ' * For use with TCWPenDown
  63. Global Const MY_FALSE = 0                ' * For use with TCWPenDown
  64. Global Const GK_GRAPHIC = &H0B            ' * TurboCAD graphic type 
  65. Global Const NULL = 0                    ' * Null value equivalent 
  66. Global Const LOGFILE = "C:\FERN.LOG"    ' * Name of log file
  67. Global Const METHOD = 0                    ' * Method used to generate the fern graphic
  68. '
  69. '
  70. ' - Error Reporting Constants
  71. '
  72. Global Const DEBUG = 0
  73. Global Const SILENT = 1                        ' * Write err message info to error file
  74. Global Const MSG_BOX = 2                    ' * Diplay err message info in a message box
  75. Global Const ALL = 3                        ' * Use error both methods 
  76. Global Const NONE = 0                        ' * Don't do any error reporting
  77. Global Const ERR_FILE = "C:\FERN.ERR"       ' * Error file name
  78. Global Const SCRIPT_FILE = "FERN.BAS"        ' * Script file name
  79. Global Const ERR_SL = 10                    ' * Length of "ERR_SET = " for DoErrSetValue
  80. Global Const ERR_SET = 1                    ' * Error file does not exit if 0
  81. Global Const ERR_METHOD = NONE                ' * Set ERR_METHOD behavior
  82. Global Const ERR_TRUE = 1                    ' * For ERR_STOP ending script execution
  83. Global Const ERR_FALSE = 0                    ' * For ERR_STOP not ending script exection
  84. Global Const ERR_STOP = ERR_TRUE            ' * Set ERR_STOP behavior
  85. '
  86. '
  87. ' - Arrays to hold the x/y values generated 
  88. '    by the fern fractal algorthm.  
  89. '
  90. dim Xarray(MAX_POINTS) as double
  91. dim Yarray(MAX_POINTS) as double
  92. dim numPoints As Long
  93. '
  94. '
  95. ' - Script effects variables
  96. '
  97. dim smear_eff    as integer
  98. dim smear_val    as double
  99. '
  100. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  101.  
  102.  
  103.  
  104.  
  105.  
  106. ''SUBROUTINE: MAIN'''''''''''''''''''''''''''''''''''''''''''''''''
  107. '
  108. ' * Parameters: None
  109. ' *
  110. ' * Return Value: None
  111. ' * 
  112. ' * Description:    
  113. ' * 
  114. ' *        Main is the conductor of the program
  115. ' *         and like a music conductor, tells
  116. ' *         the other parts of the program when
  117. ' *         it's time to do their thing.
  118. ' * 
  119. ' * 
  120. Sub main ()
  121.  
  122.     InitializeScript
  123.     DoFernUI
  124.  
  125.     LoadVertexCoordinateArray
  126.     GenerateFernGraphic
  127.  
  128. End Sub
  129. '
  130. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  131.  
  132.  
  133.  
  134.  
  135.  
  136. ''SUBROUTINE: InitializeScript'''''''''''''''''''''''''''''''''''''
  137. '
  138. ' * Parameters: None
  139. ' *
  140. ' * Return Value: None
  141. ' * 
  142. ' * Description:    
  143. ' * 
  144. ' *        Script Setup Stuff
  145. ' *
  146. ' * 
  147. Sub InitializeScript ()
  148.  
  149.     smear_eff = 0
  150.     smear_val = 0
  151.  
  152.     TCWClearError    ' * Clear any error out of the error buffer.
  153.  
  154.     ' * ADD YOUR CODE HERE *
  155.  
  156. End Sub
  157. '
  158. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  159.  
  160.  
  161.  
  162.  
  163.  
  164. ''SUBROUTINE: LoadVertexCoordinateArray''''''''''''''''''''''''''''
  165. '
  166. ' * Parameters: None
  167. ' * 
  168. ' * Return Value: None
  169. ' * 
  170. ' * Description:    
  171. ' * 
  172. ' *        This subroutine uses a fern fractal algorithm
  173. ' *         to generate a set of x/y coordinates based on
  174. ' *         a randomly generated seed value and the previous
  175. ' *         iterations x/y output.  Each iterations
  176. ' *         results are loaded into an array which is 
  177. ' *         then used to generate the fractal graphic itself.
  178. ' * 
  179. ' * 
  180. Sub LoadVertexCoordinateArray ()
  181.  
  182.     dim counter as long
  183.     dim a        as double
  184.     dim b        as double
  185.     dim c        as double
  186.     dim d        as double
  187.     dim e        as double
  188.     dim f        as double
  189.     dim r        as double
  190.     dim x        as double
  191.     dim y        as double
  192.  
  193.     for counter = 0 To (numPoints-1)
  194.  
  195.         r = rnd
  196.  
  197.         if r <= .01 then
  198.             a = 0
  199.             b = 0
  200.             c = 0 
  201.             d = .16
  202.             e = 0
  203.             f = 0
  204.  
  205.         elseif r > .01 and r <= .86 then
  206.             a = .85
  207.             b = .04
  208.             c = -.04
  209.             d = .85 
  210.             e = 0
  211.             f = 1.6
  212.  
  213.         elseif r > .86 and r <= .93 then
  214.             a = .2
  215.             b = -.26
  216.             c = .23
  217.             d = .22
  218.             e = 0
  219.             f = 1.6
  220.  
  221.         else
  222.             a = -.15
  223.             b = .28
  224.             c = .26
  225.             d = .24
  226.             e = 0
  227.             f = .44
  228.  
  229.         end if
  230.  
  231.         x = (a * x) + (b * y) + e
  232.         y = (c * x) + (d * y) + f
  233.  
  234.         Xarray(counter) = x
  235.         Yarray(counter) = y
  236.  
  237.     next
  238.  
  239. End Sub
  240. '
  241. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  242.  
  243.  
  244.  
  245.  
  246.  
  247. ''SUBROUTINE: GenerateFernGraphic''''''''''''''''''''''''''''''''''
  248. '
  249. ' * Parameters: None
  250. ' *
  251. ' * Return Value: None
  252. ' * 
  253. ' * Description:    
  254. ' * 
  255. ' *        This subroutine creates the fern 
  256. ' *         graphic.
  257. ' * 
  258. ' * 
  259. Sub GenerateFernGraphic ()
  260.     
  261.     dim gfgCounter    as long
  262.     dim hGraphic    as long
  263.     dim hDrawing    as long
  264.     dim offset        as double
  265.  
  266.     if smear_eff = 1 then
  267.         
  268.         offset = smear_val
  269.  
  270.     else
  271.         
  272.         offset = .01
  273.  
  274.     end if
  275.  
  276.     hDrawing = TCWDrawingActive
  277.     hGraphic = TCWGraphicCreate (GK_GRAPHIC, "")
  278.     
  279.     for gfgCounter=0 to (numPoints-1)
  280.  
  281.         VertexCreateMethod hGraphic, Xarray(gfgCounter), Yarray(gfgCounter), offset
  282.         
  283.     next
  284.  
  285.     TCWGraphicAppend NULL, hGraphic
  286.     TCWGraphicDraw hGraphic, 0
  287.  
  288.     TCWUndoRecordStart hDrawing, "fern"
  289.  
  290.         TCWUndoRecordAddGraphic hDrawing, hGraphic
  291.  
  292.     TCWUndoRecordEnd hDrawing
  293.             
  294. End Sub
  295. '
  296. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  297.  
  298.  
  299.  
  300.  
  301.  
  302. ''SUBROUTINE: VertexCreateMethod'''''''''''''''''''''''''''''''''''
  303. '
  304. ' * Parameters: None
  305. ' *
  306. ' * Return Value: None
  307. ' * 
  308. ' * Description:    
  309. ' * 
  310. ' *        This subroutine creates a fern
  311. ' *         graphic using TCWVertexAppend
  312. ' *         
  313. ' * 
  314. ' * 
  315. Sub VertexCreateMethod (ByVal hGraphic As Long, ByVal x As Double, ByVal y As Double, ByVal os As Double)
  316.  
  317.     dim hVertex1    as long
  318.     dim hVertex2    as long
  319.  
  320.     hVertex1 = TCWVertexCreate(x#, y#, 0#)
  321.     hVertex2 = TCWVertexCreate(x#+os#, y#+os#, 0#)
  322.     
  323.     TCWPenDown hVertex1, MY_FALSE
  324.     TCWPenDown hVertex2, MY_TRUE
  325.  
  326.     TCWGraphicVertexAdd hGraphic, hVertex1
  327.     TCWGraphicVertexAdd hGraphic, hVertex2
  328.  
  329. End Sub
  330. '
  331. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  332.  
  333.  
  334.  
  335.  
  336.  
  337. ''SUBROUTINE: DoFernUI'''''''''''''''''''''''''''''''''''''''''''''
  338. '
  339. ' * Parameters: None
  340. ' *
  341. ' * Return Value: None
  342. ' * 
  343. ' * Description:    
  344. ' * 
  345. ' *        This is the fern fractal script's user interface. 
  346. ' *         It's actually a dialog box definition that serves
  347. ' *         as a "template" for later creating a variable of
  348. ' *         of this type and them using the enable function
  349. ' *        'Dialog' to display it and return values.  The  
  350. ' *         dialog definition is done in a manner very similar 
  351. ' *         to creating user defined variables with the type 
  352. ' *         function in basic or the struct function in C.
  353. ' *         By utilizing several of the UI objects available
  354. ' *         you can actually create quite a useful interface 
  355. ' *         for setting script values and options as well as
  356. ' *         guiding the user through script setup with a 
  357. ' *         "wizard" like interface.  
  358. ' *  
  359. Sub DoFernUI ()
  360.  
  361.     Begin Dialog FernUI 60, 60, 240, 184, "Fern Fractal Generator"
  362.  
  363.         Text 30,    30,        200,    200,    "This script demonstrates the use of TurboCAD scripting to interact with the user with a 'wizard' style interface and then generate a new graphic type generated by the script drive certain properties of the graphic." 
  364.         Text 30,    140,    140,    20,        "How many Points would you like?"
  365.  
  366.         TextBox 150, 140, 40, 16, .numPointText
  367.  
  368.         OKbutton     80, 170, 40, 12
  369.         CancelButton 120, 170, 40, 12
  370.     
  371.     End Dialog
  372.     
  373.  
  374.     Dim FrnDlg As FernUI
  375.     FrnDlg.numPointText = "500"
  376.     button = Dialog(FrnDlg)
  377.  
  378.  
  379.     if button = 0 then
  380.  
  381.         END
  382.  
  383.     else 
  384.  
  385.         numPoints = FrnDlg.numPointText
  386.         
  387.         if numPoints > 100000 then
  388.  
  389.             MsgBox "Too many points!  Please enter a value smaller than 100000."
  390.             DoFernUI
  391.  
  392.         elseif numPoints < 1 then
  393.  
  394.             MsgBox "Not enough points!  Please enter a value larger than 0."
  395.             DoFernUI
  396.  
  397.         end if
  398.  
  399.     end if
  400.  
  401. End Sub
  402. '
  403. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  404.     
  405.  
  406.  
  407.  
  408.