home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / RCONFIG.CLS < prev    next >
Encoding:
Text File  |  1996-02-27  |  9.3 KB  |  291 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "RobotConfig"
  6. Attribute VB_Creatable = False
  7. Attribute VB_Exposed = False
  8. ' ************************************************
  9. ' A robot configuration.
  10. ' ************************************************
  11. Option Explicit
  12.  
  13. Public Cx As Integer    ' Location of top of head.
  14. Public Cy As Integer
  15. Public LShoulderAngle As Single
  16. Public RShoulderAngle As Single
  17. Public LElbowAngle As Single
  18. Public RElbowAngle As Single
  19. Public LHipAngle As Single
  20. Public RHipAngle As Single
  21. Public LKneeAngle As Single
  22. Public RKneeAngle As Single
  23. ' ************************************************
  24. ' Fill in this configuration's parameters by
  25. ' copying from another.
  26. ' ************************************************
  27. Public Sub CopyFrame(from_me As RobotConfig)
  28.     With from_me
  29.         Cx = .Cx
  30.         Cy = .Cy
  31.         LShoulderAngle = .LShoulderAngle
  32.         RShoulderAngle = .RShoulderAngle
  33.         LElbowAngle = .LElbowAngle
  34.         RElbowAngle = .RElbowAngle
  35.         LHipAngle = .LHipAngle
  36.         RHipAngle = .RHipAngle
  37.         LKneeAngle = .LKneeAngle
  38.         RKneeAngle = .RKneeAngle
  39.     End With
  40. End Sub
  41.  
  42.  
  43. ' ************************************************
  44. ' Return the position of part of the robot.
  45. ' ************************************************
  46. Public Sub Position(part As Integer, x As Integer, y As Integer)
  47.     Select Case part
  48.         Case PART_HEAD
  49.             x = Cx
  50.             y = Cy
  51.         Case PART_NECK
  52.             x = Cx
  53.             y = Cy + 2 * HEAD_RAD
  54.         Case PART_SHOULDERS
  55.             x = Cx
  56.             y = Cy + 2 * HEAD_RAD + NECK_LEN
  57.         Case PART_LELBOW
  58.             x = Cx + _
  59.                 UARM_LEN * Cos(LShoulderAngle)
  60.             y = Cy + 2 * HEAD_RAD + NECK_LEN - _
  61.                 UARM_LEN * Sin(LShoulderAngle)
  62.         Case PART_RELBOW
  63.             x = Cx + _
  64.                 UARM_LEN * Cos(RShoulderAngle)
  65.             y = Cy + 2 * HEAD_RAD + NECK_LEN - _
  66.                 UARM_LEN * Sin(RShoulderAngle)
  67.         Case PART_LHAND
  68.             x = Cx + _
  69.                 UARM_LEN * Cos(LShoulderAngle) + _
  70.                 LARM_LEN * Cos(LElbowAngle)
  71.             y = Cy + 2 * HEAD_RAD + NECK_LEN - _
  72.                 UARM_LEN * Sin(LShoulderAngle) - _
  73.                 LARM_LEN * Sin(LElbowAngle)
  74.         Case PART_RHAND
  75.             x = Cx + _
  76.                 UARM_LEN * Cos(RShoulderAngle) + _
  77.                 LARM_LEN * Cos(RElbowAngle)
  78.             y = Cy + 2 * HEAD_RAD + NECK_LEN - _
  79.                 UARM_LEN * Sin(RShoulderAngle) - _
  80.                 LARM_LEN * Sin(RElbowAngle)
  81.         Case PART_HIPS
  82.             x = Cx
  83.             y = Cy + 2 * HEAD_RAD + BODY_LEN
  84.         Case PART_LKNEE
  85.             x = Cx + _
  86.                 ULEG_LEN * Cos(LHipAngle)
  87.             y = Cy + 2 * HEAD_RAD + BODY_LEN - _
  88.                 ULEG_LEN * Sin(LHipAngle)
  89.         Case PART_RKNEE
  90.             x = Cx + _
  91.                 ULEG_LEN * Cos(RHipAngle)
  92.             y = Cy + 2 * HEAD_RAD + BODY_LEN - _
  93.                 ULEG_LEN * Sin(RHipAngle)
  94.         Case PART_LFOOT
  95.             x = Cx + _
  96.                 ULEG_LEN * Cos(LHipAngle) + _
  97.                 LLEG_LEN * Cos(LKneeAngle)
  98.             y = Cy + 2 * HEAD_RAD + BODY_LEN - _
  99.                 ULEG_LEN * Sin(LHipAngle) - _
  100.                 LLEG_LEN * Sin(LKneeAngle)
  101.         Case PART_RFOOT
  102.             x = Cx + _
  103.                 ULEG_LEN * Cos(RHipAngle) + _
  104.                 LLEG_LEN * Cos(RKneeAngle)
  105.             y = Cy + 2 * HEAD_RAD + BODY_LEN - _
  106.                 ULEG_LEN * Sin(RHipAngle) - _
  107.                 LLEG_LEN * Sin(RKneeAngle)
  108.     End Select
  109. End Sub
  110.  
  111.  
  112.  
  113. ' ************************************************
  114. ' Draw the robot.
  115. ' ************************************************
  116. Public Sub Draw(pic As PictureBox, handles As Boolean)
  117. Dim x1 As Integer
  118. Dim y1 As Integer
  119. Dim x2 As Integer
  120. Dim y2 As Integer
  121. Dim x3 As Integer
  122. Dim y3 As Integer
  123.  
  124.     ' Draw the head.
  125.     x1 = Cx
  126.     y1 = Cy + HEAD_RAD
  127.     pic.Circle (x1, y1), HEAD_RAD
  128.     If handles Then _
  129.         pic.Line (Cx - NEAR, Cy - NEAR)- _
  130.                 Step(NEAR2, NEAR2), , BF
  131.     
  132.     ' Draw the body.
  133.     y1 = y1 + HEAD_RAD
  134.     pic.Line (x1, y1)-Step(0, BODY_LEN)
  135.     
  136.     ' Draw the left arm.
  137.     y1 = y1 + NECK_LEN
  138.     x2 = x1 + UARM_LEN * Cos(LShoulderAngle)
  139.     y2 = y1 - UARM_LEN * Sin(LShoulderAngle)
  140.     pic.Line (x1, y1)-(x2, y2)
  141.     x3 = x2 + LARM_LEN * Cos(LElbowAngle)
  142.     y3 = y2 - LARM_LEN * Sin(LElbowAngle)
  143.     pic.Line -(x3, y3)
  144.     If handles Then _
  145.         pic.Line (x2 - NEAR, y2 - NEAR)- _
  146.                 Step(NEAR2, NEAR2), , BF
  147.     If handles Then _
  148.         pic.Line (x3 - NEAR, y3 - NEAR)- _
  149.                 Step(NEAR2, NEAR2), , BF
  150.  
  151.     ' Draw the right arm.
  152.     x2 = x1 + UARM_LEN * Cos(RShoulderAngle)
  153.     y2 = y1 - UARM_LEN * Sin(RShoulderAngle)
  154.     pic.Line (x1, y1)-(x2, y2)
  155.     x3 = x2 + LARM_LEN * Cos(RElbowAngle)
  156.     y3 = y2 - LARM_LEN * Sin(RElbowAngle)
  157.     pic.Line -(x3, y3)
  158.     If handles Then _
  159.         pic.Line (x2 - NEAR, y2 - NEAR)- _
  160.                 Step(NEAR2, NEAR2), , BF
  161.     If handles Then _
  162.         pic.Line (x3 - NEAR, y3 - NEAR)- _
  163.                 Step(NEAR2, NEAR2), , BF
  164.  
  165.     ' Draw the left leg.
  166.     y1 = y1 + TRUNK_LEN
  167.     x2 = x1 + ULEG_LEN * Cos(LHipAngle)
  168.     y2 = y1 - ULEG_LEN * Sin(LHipAngle)
  169.     pic.Line (x1, y1)-(x2, y2)
  170.     x3 = x2 + LLEG_LEN * Cos(LKneeAngle)
  171.     y3 = y2 - LLEG_LEN * Sin(LKneeAngle)
  172.     pic.Line -(x3, y3)
  173.     If handles Then _
  174.         pic.Line (x2 - NEAR, y2 - NEAR)- _
  175.                 Step(NEAR2, NEAR2), , BF
  176.     If handles Then _
  177.         pic.Line (x3 - NEAR, y3 - NEAR)- _
  178.                 Step(NEAR2, NEAR2), , BF
  179.  
  180.     ' Draw the right leg.
  181.     x2 = x1 + ULEG_LEN * Cos(RHipAngle)
  182.     y2 = y1 - ULEG_LEN * Sin(RHipAngle)
  183.     pic.Line (x1, y1)-(x2, y2)
  184.     x3 = x2 + LLEG_LEN * Cos(RKneeAngle)
  185.     y3 = y2 - LLEG_LEN * Sin(RKneeAngle)
  186.     pic.Line -(x3, y3)
  187.     If handles Then _
  188.         pic.Line (x2 - NEAR, y2 - NEAR)- _
  189.                 Step(NEAR2, NEAR2), , BF
  190.     If handles Then _
  191.         pic.Line (x3 - NEAR, y3 - NEAR)- _
  192.                 Step(NEAR2, NEAR2), , BF
  193. End Sub
  194.  
  195.  
  196.  
  197. ' ************************************************
  198. ' Move the control point to this location.
  199. ' ************************************************
  200. Public Sub MoveControlPoint(part As Integer, Ax As Integer, Ay As Integer, x As Integer, y As Integer)
  201.     Select Case part
  202.         Case PART_HEAD
  203.             Cx = x
  204.             Cy = y
  205.         Case PART_LELBOW
  206.             LShoulderAngle = Arctan2(x - Ax, Ay - y)
  207.         Case PART_RELBOW
  208.             RShoulderAngle = Arctan2(x - Ax, Ay - y)
  209.         Case PART_LHAND
  210.             LElbowAngle = Arctan2(x - Ax, Ay - y)
  211.         Case PART_RHAND
  212.             RElbowAngle = Arctan2(x - Ax, Ay - y)
  213.         Case PART_LKNEE
  214.             LHipAngle = Arctan2(x - Ax, Ay - y)
  215.         Case PART_RKNEE
  216.             RHipAngle = Arctan2(x - Ax, Ay - y)
  217.         Case PART_LFOOT
  218.             LKneeAngle = Arctan2(x - Ax, Ay - y)
  219.         Case PART_RFOOT
  220.             RKneeAngle = Arctan2(x - Ax, Ay - y)
  221.     End Select
  222. End Sub
  223.  
  224. ' ************************************************
  225. ' Initialize the robot's parameters.
  226. ' ************************************************
  227. Public Sub SetParameters(x As Integer, y As Integer, ls As Single, rs As Single, le As Single, re As Single, lh As Single, rh As Single, lk As Single, rk As Single)
  228. Const PI = 3.14159
  229. Const DEG_TO_RAD = PI / 180#
  230.  
  231.     Cx = x
  232.     Cy = y
  233.     LShoulderAngle = ls * DEG_TO_RAD
  234.     RShoulderAngle = rs * DEG_TO_RAD
  235.     LElbowAngle = le * DEG_TO_RAD
  236.     RElbowAngle = re * DEG_TO_RAD
  237.     LHipAngle = lh * DEG_TO_RAD
  238.     RHipAngle = rh * DEG_TO_RAD
  239.     LKneeAngle = lk * DEG_TO_RAD
  240.     RKneeAngle = rk * DEG_TO_RAD
  241. End Sub
  242.  
  243. ' ************************************************
  244. ' Return the distance between the top of the head
  245. ' and the top of the robot when its height is as
  246. ' large as possible.
  247. ' ************************************************
  248. Property Get HeadRoom()
  249.     HeadRoom = (UARM_LEN + LARM_LEN) - _
  250.         (2 * HEAD_RAD + NECK_LEN)
  251. End Property
  252. ' ************************************************
  253. ' Return the maximum possible height the robot
  254. ' can have.
  255. ' ************************************************
  256. Property Get MaxHeight()
  257.     MaxHeight = TRUNK_LEN + UARM_LEN + LARM_LEN + _
  258.                 ULEG_LEN + LLEG_LEN
  259. End Property
  260.  
  261. ' ************************************************
  262. ' Return the maximum possible width the robot
  263. ' can have.
  264. ' ************************************************
  265. Property Get MaxWidth()
  266.     MaxWidth = 2 * (UARM_LEN + LARM_LEN)
  267. End Property
  268.  
  269.  
  270. ' ************************************************
  271. ' Read the configuration data from a file.
  272. ' ************************************************
  273. Sub FileInput(fnum)
  274.     Input #fnum, Cx, Cy, _
  275.         LShoulderAngle, RShoulderAngle, _
  276.         LElbowAngle, RElbowAngle, _
  277.         LHipAngle, RHipAngle, _
  278.         LKneeAngle, RKneeAngle
  279. End Sub
  280. ' ************************************************
  281. ' Write the configuration data to a file.
  282. ' ************************************************
  283. Sub FileWrite(fnum)
  284.     Write #fnum, Cx, Cy, _
  285.         LShoulderAngle, RShoulderAngle, _
  286.         LElbowAngle, RElbowAngle, _
  287.         LHipAngle, RHipAngle, _
  288.         LKneeAngle, RKneeAngle
  289. End Sub
  290.  
  291.