Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 86,260 VB Programmers. There are 1,944 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Coordinate transformation

 
Reply to this topicStart new topic

Coordinate transformation, How to transform coordinates when rotating an object in space

jens
post 9 May, 2008 - 07:23 AM
Post #1


New D.I.C Head

*
Joined: 9 May, 2008
Posts: 9



Hello!

I need to rotate an object within a coordinate system. I figured It could be done the following way. Please tell me if you find anything wrong in my code or if you see a smarter way of doing this. Please also tell if you think the code is ok and / or useful.

I would especially like some comment about the way I treat the object err at the end. Is it ok to do it this way?

Feel free to use the code in any way you want.

CODE


'/==================================================================
'/ function rotatedKoordinates(axis as char, rotation as double, xyz as xyzType) as xyzType
'/ Returnerar (0, 0, 0) vid fel.
'/
'/ 20080509 Jens
'/ This function returns the new coordinates of an object that has
'/ been rotated around one axis in a coordinate system.
'/ The function assumes that you rotate the object counter clockwise
'/ wieved from the positive end of the axis you rotate around.
'/ This function is for VB6 and it does need one Type to be declared:
'/ Public Type xyzType
'/   x As Double
'/   y As Double
'/   z As Double
'/ End Type
'/
'/ You provide information about what axis you want to rotate around,
'/ how many degrees you want to rotate and the x-, y- and z-coordinates before
'/ the rotation.
'/ The function returns the new coordinates for the object.
'/ This can also be used if you let the object be fixed and
'/ instead rotate the coordinate system clockwise around the provided axis.
'/
'/ Example; A vector (0, 3, 0) rotates 90 degrees counter clockwise
'/ around the z-axis seen from the positive end of the z-axis:
'/ (z, 90, 0, 3, 0) => (-3, 0, 0)
'/------------------------------------------------------------------
Public Function rotateKoordinates(ByVal axis As Char, ByVal rotation As Double, ByVal xyz As xyzType) As xyzType
  Dim xyz0 As xyzType, xyz1 As xyzType
  Dim pi  As Double: pi = 3.14159265358979
  Dim radianer As Double, length As Double
  Dim alfa0 As Double, alfa1 As Double
  
  xyz1.x = 0
  xyz1.y = 0
  xyz1.z = 0
  
  'In case somthing goes wrong at least return (0, 0, 0)
  'This is questionable, but a philosophical question.
  rotateKoordinates = xyz1
  
  'If no rotation or any whole number of complete
  'rotations, don't waste time computing the
  'obvious. Return what was sent to the function.
  If (rotation Mod 360) = 0 Then
    rotateKoordinates = xyz0
    Exit Function
  End If
  
  'Calculate radians from degrees since
  'the trigonometric functions use radians.
  radianer = rotation * pi / 180
  
  Select Case axis
  Case "x", "X"
    'Since we are rotating around the x-axis
    'the x-coordinate won't change.
    xyz1.x = xyz0.x
    
    'Calculate the length of the vector from origo to the coordinates given
    'NB: We only calculate the length in the yz-plane since we don't
    'do anything with the x-coordinate
    length = Sqr(xyz0.y ^ 2 + xyz0.z ^ 2)
    
    'IF y=0 we are on the z-axis, i.e: alfa0 = pi/2 eller alfa0=pi*3/2
    'If z=0 too won't be a problem
    If xyz0.y = 0 Then 'On the z-axis
      alfa1 = IIf(xyz0.z > 0, radianer + pi / 2, pi * 3 / 2 + radianer)
    Else
      alfa1 = Atn(xyz0.z / xyz0.y) + radianer
    End If
    xyz1.y = length * Cos(alfa1)
    xyz1.z = length * Sin(alfa1)
  Case "y", "Y"
    xyz1.y = xyz0.y
    length = Sqr(xyz0.x ^ 2 + xyz0.z ^ 2)
    If xyz0.z = 0 Then ' On the x-axis
      alfa1 = IIf(xyz0.x > 0, radianer + pi / 2, pi * 3 / 2 + radianer)
    Else
      alfa1 = Atn(xyz0.x / xyz0.z) + radianer
    End If
    xyz1.z = length * Cos(alfa1)
    xyz1.x = length * Sin(alfa1)
Case "z", "Z"
    xyz1.z = xyz0.z
    length = Sqr(xyz0.x ^ 2 + xyz0.y ^ 2)
    If xyz0.x = 0 Then 'On the y-axis
      alfa1 = IIf(xyz0.y > 0, radianer + pi / 2, pi * 3 / 2 + radianer)
    Else
      alfa1 = Atn(xyz0.y / xyz0.x) + radianer
    End If
    xyz1.x = length * Cos(alfa1)
    xyz1.y = length * Sin(alfa1)
  Case Else
    Err.Clear
    Err.Number = 5
    Err.Description = "You can only rotate around x, y or z -axis"
    Err.Raise
    Exit Function
  End Select
  rotateKoordinates = xyz1
  Exit Function
End Function



Edit: Would this have qualified as a snippet? Should I copy it to "snippets"?

This post has been edited by jens: 9 May, 2008 - 12:37 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
Time is now: 5/16/08 10:08AM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month