0 Replies - 627 Views - Last Post: 27 January 2009 - 02:47 AM Rate Topic: -----

#1 Maritime  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-October 08

How to use Function RotateKoordinates(....

Posted 27 January 2009 - 02:47 AM

Hello,

Is anyone able to explain me how to use this function with this example:

A vector (0, 3, 0) rotates 90 degrees counter clockwise
around the z-axis as seen from the positive end of the z-axis:
(z, 90, 0, 3, 0) => (-3, 0, 0)

Thank's

 
''==================================================================
	' Function rotateKoordinates(axis, rotation, xyz) As xyzType
	''
	'' 20080512 Jens
	'' Returns the coordinates of an object that has
	'' been rotated around one axis in a coordinate system.
	'' Returns (0, 0, 0) when things go wrong.
	'' The function assumes that you rotate the object counter clockwise
	'' as viewed from the positive end of the axis you rotate around.
	'' This function is for VB6 and it does need one Type to be declared:
		  
	'' cos(resulting_angle) < 10^-15 => cos(resulting_angle) = 0
	'' sin(resulting_angle) < 10^-15 => sin(resulting_angle) = 0
	'' This means that results are rounded so the vector "snaps" to a
	'' close axis.
	''
	'' 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 as seen from the positive end of the z-axis:
	'' (z, 90, 0, 3, 0) => (-3, 0, 0)
	''------------------------------------------------------------------
   
   Public Type xyzType
		x As Double
		y As Double
		z As Double
	End Type 


 Public Function rotateKoordinates(ByVal axis As String, ByVal rotation As Double, ByRef xyz0 As xyzType) As xyzType
		Dim xyz1 As xyzType
		Dim pi As Double: pi = 3.14159265358979
		Dim radianer As Double, length As Double, tmpSin As Double, tmpCos As Double
		Dim alfa1 As Double, SinTmp As Double, CosTmp As Double

		'In case somthing goes wrong at least return (0, 0, 0)
		'This is questionable, but a philosophical question.
		xyz1.x = 0
		xyz1.y = 0
		xyz1.z = 0
		rotateKoordinates = xyz1

		'Since 375 degrees will give the same result as 15 degrees...
		While (rotation > 360): rotation = rotation - 360: Wend

		'If no rotation or a complete rotation,
		'don't waste time computing the obvious.
		'Just return what was sent to the function.
		If (rotation Mod 360) = 0 Then
		  rotateKoordinates = xyz0
		  Exit Function
		End If
  
		'If you rotate n*90 degrees the absolute values
		'should be kept and just moved to different axis
		'and in some cases with different sign.
		If (rotation Mod 90 = 0) Then
			Select Case axis
			Case "x", "X"
			  'Since we are rotating around the x-axis
			  'the x-coordinate won't change.
			  xyz1.x = xyz0.x
			  Select Case rotation
			  Case 90
				xyz1.y = -xyz0.z
				xyz1.z = xyz0.y
			  Case 180
				xyz1.y = -xyz0.y
				xyz1.z = -xyz0.z
			  Case 270
				xyz1.y = xyz0.z
				xyz1.z = -xyz0.y
			  End Select
			Case "y", "Y"
			  xyz1.y = xyz0.y
			  Select Case rotation
			  Case 90
				xyz1.x = xyz0.z
				xyz1.z = -xyz0.x
			  Case 180
				xyz1.x = -xyz0.x
				xyz1.z = -xyz0.z
			  Case 270
				xyz1.x = -xyz0.z
				xyz1.z = xyz0.x
			  End Select
			Case "z", "Z"
			  xyz1.z = xyz0.z
			  Select Case rotation
			  Case 90
				xyz1.x = -xyz0.y
				xyz1.y = xyz0.x
			  Case 180
				xyz1.x = -xyz0.x
				xyz1.y = -xyz0.y
			  Case 270
				xyz1.x = xyz0.y
				xyz1.y = -xyz0.x
			  End Select
			Case Else 'axis
			   Err.Clear
			   Err.Number = 5
			   Err.Description = "You can only rotate around x, y or z -axis"
			   Err.Raise (5)
			   Exit Function
			End Select 'axis
			rotateKoordinates = xyz1
			Exit Function
		End If

		Select Case axis
			Case ("x"), ("X")
				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 = CDec(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 = pi * (IIf(xyz0.z > 0, ((90 + rotation) / 180), ((270 + rotation) / 180)))
				Else
					alfa1 = Atn(xyz0.z / xyz0.y) + (rotation * (pi / 180))
				End If
				CosTmp = Cos(alfa1)
				SinTmp = Sin(alfa1)
				'If i.e. sin(alfa1) is very small I assume that we are in fact
				'on the y-axis and that alfa1 is really 0 => sin(alfa1) = 0.
				'Same kind of reasoning behind cos(alfa1)
				'
				'One could reason that I should also check for sin(alfa1)
				'close to 1 and cos(alfa1) close to 1 but the error is in
				'this case much smaller and thus less important.
				'sin(E-10) is just about E-10 while 1-cos(E-10) is about E-20
				'which is so small you can ignore it.
				xyz1.y = length * IIf(Abs(CosTmp) < 0.000000000000001, 0, CosTmp)
				xyz1.z = length * IIf(Abs(SinTmp) < 0.000000000000001, 0, SinTmp)
			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 = pi * (IIf(xyz0.x > 0, ((90 + rotation) / 180), ((270 + rotation) / 180)))
				Else
					alfa1 = Atn(xyz0.x / xyz0.z) + (rotation * (pi / 180))
				End If
				CosTmp = Cos(alfa1)
				SinTmp = Sin(alfa1)
				xyz1.z = length * IIf(Abs(CosTmp) < 0.000000000000001, 0, CosTmp)
				xyz1.x = length * IIf(Abs(SinTmp) < 0.000000000000001, 0, SinTmp)
			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 = pi * (IIf(xyz0.y > 0, ((90 + rotation) / 180), ((270 + rotation) / 180)))
				Else
					alfa1 = Atn(xyz0.y / xyz0.x) + (rotation * (pi / 180))
				End If
				CosTmp = Cos(alfa1)
				SinTmp = Sin(alfa1)
				xyz1.x = length * IIf(Abs(CosTmp) < 0.000000000000001, 0, CosTmp)
				xyz1.y = length * IIf(Abs(SinTmp) < 0.000000000000001, 0, SinTmp)
			Case Else
				Err.Clear
				Err.Number = 5
				Err.Description = "You can only rotate around x, y or z -axis"
				Err.Raise (5)
				Exit Function
		End Select
		rotateKoordinates = xyz1
		Exit Function
	End Function




Is This A Good Question/Topic? 0
  • +

Page 1 of 1