Coordinates, Vectors and 3D volumes-VBForums
Results 1 to 10 of 10

Thread: Coordinates, Vectors and 3D volumes

  1. #1

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Coordinates, Vectors and 3D volumes

    Some code for handling coordinates and vectors in VB.Net. Coded in v2003. You need to import System.Math for the code to work.


    Last updates: 21 April 2006


    Current contents:

    Coordinate Systems: Cartesian, Spherical Polar and Cylindrical Polar. Each can be defined with offsets and rotations relative to the global cartesian coordinate system.

    Coordinate Transforms: From one local type to another, from any defined system to any other defined system, cartesian rotations (global and local).

    Vectors: Addition, subtraction, translation, scaling, dot product, cross product.

    Solids: Coordinate generation on the surface of spheres, ellipsoids, cuboids and cylinders.

    Usage:

    VB Code:
    1. Dim Global As New CoordinateSystem.Cartesian()
    2.  
    3.         Dim mycartsystem As New CoordinateSystem.Cartesian(10, 10, 0, 0, 0, 0, 1)
    4.  
    5.         Dim mycartcoord As New Coordinate.Cartesian(mycartsystem, 1, 0, 0)
    6.  
    7.         Dim mycartcoord2 As New Coordinate.Cartesian(mycartsystem, 0, 1, 0)
    8.        
    9.         Dim mycartcoordorigin As New Coordinate.Cartesian(mycartsystem, 0, 0, 0)
    10.  
    11.         Dim myvector1 As New Vector.Cartesian
    12.         myvector1.StartPoint = mycartcoordorigin
    13.         myvector1.EndPoint = mycartcoord
    14.  
    15.         Dim myvector2 As New Vector.Cartesian
    16.         myvector2.StartPoint = mycartcoordorigin
    17.         myvector2.EndPoint = mycartcoord2
    18.  
    19.         Dim subvector As New Vector.Cartesian
    20.  
    21.         subvector = Operations.VectorOps.Subtract(myvector1, myvector2)
    22.  
    23.         MsgBox(subvector.X2 & ", " & subvector.Y2 & ", " & subvector.Z2)

    The above code does the following:
    -Creates a new cartesian coordinate system located at (10,10,0) with respect to the global, but with parallel axes.
    -Generates three new coordinates within this system, (0,0,0), (1,0,0) and (0,1,0).
    -Defines two new vectors within the system, one running along the x-axis and one along the y-axis.
    -Defines a new vector by subtracting the y-axis vector from the x-axis vector. The new vector runs from (0,0,0) to (1,-1,0).
    -Returns the endpoint of the new vector.



    The picture illustrates how the offset and rotation properties are used: offsets are cartesian with respect to a global system centred at (0,0,0). The Theta rotation direction rotates the x axis onto the y axis, the Phi direction rotates the x axis onto the z axis and the Psi rotation angle rotates the y axis onto the z axis.
    The same applies to the theta and phi coordinates in spherical and cylindrical polar coordinates.

    There will be further additions to this code as necessary.


    zaza
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by zaza; Apr 21st, 2006 at 05:56 PM.

  2. #2

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates and Vectors

    Usage Guidelines: 19/03/06

    Create a coordinate system:

    Code:
    Dim mycartsystem As New CoordinateSystem.Cartesian(A, B, C, D, E, F, G)
    A: The offset in the X direction
    B: The offset in the Y direction
    C: The offset in the Z direction
    D: The rotation in the theta direction (about Z axis).
    E: The rotation in the phi direction (about Y axis).
    F: The rotation in the psi direction (about X axis)
    G: The scale factor compared to the global. Scale factor of 2 means that 1 new unit = 2 global units.

    All systems use these offsets, rotations and scales to define their position relative to the global cartesian system.



    Create a coordinate:

    Code:
    Dim mycartcoord As New Coordinate.Cartesian(S, A, B, C)
    S: The coordinate system in which to create the coordinate.

    S=Cartesian / Spherical Polar / Cylindrical Polar
    A: The X position / Radius R / Radius R
    B: The Y position / Angle Theta / Angle Theta
    C: The Z position / Angle Phi / Length Z

    The values in A, B and C depend on the type of coordinate system used.



    Create a vector:

    Code:
     Dim myvector1 As New Vector.Cartesian
     myvector1.StartPoint = mycartcoordorigin
     myvector1.EndPoint = mycartcoord
    Cartesian vectors have a startpoint and endpoint, which are both coordinates. They also have X1, Y1, Z1 properties which are the X, Y and Z values of Startpoint, and X2, Y2, Z2 properties which are the X, Y and Z values of Endpoint.
    In addition, vectors have a length property, which is calculated whenever the start or end points change. If the Length property is set directly, then the startpoint remains in place and the endpoint is recalculated.
    Vectors can be located in a coordinate system.



    Angle operations:

    Code:
     myangle = Operations.Angles.Degrees(myangle) 'Radians to degrees
     myangle = Operations.Angles.Radians(myangle) 'Degrees to radians
    Transforms between degrees and radians.



    Coordinate operations:

    Local transforms:

    Code:
    mycartcoord = Operations.Transform.LocalSphToCart(mysphcoord)
    Transforms a spherical polar coordinate to the equivalent in local cartesians, i.e. the cartesian system with the same origin, rotation and scale with respect to the global cartesian system.
    Equivalent functions transform a coordinate between local spherical, cylindrical and cartesian coordinate systems.


    Local cartesian rotation:

    Code:
    mycartcoord = Operations.Transform.RotateCartesian(mycartcoord, T, P, S)
    Calculates the position of a coordinate if the local cartesian system was rotated by angles theta (T), phi (P) and psi (S).


    Local cartesian scale:

    Code:
    mycartcoord = Operations.Transform.ScaleCartesian(mycartcoord, S)
    Calculates the position of a coordinate if the local cartesian system was scaled by a factor S.


    Transform coordinate from one system to another:

    Code:
     mysphcoord = Operations.Transform.Coordinate(mycartcoord, mysphsys)
    Transforms the coordinate mycartcoord in a cartesian coordinate system to the new coordinate mysphcoord in the spherical polar system mysphsys. Overloaded to accept coordinates and systems in Cartesian, Spherical polar and Cylindrical Polar styles.



    Vector operations:

    Code:
    C = Operations.VectorOps.Add(A, B)
    Returns the resultant vector C from the vector sum (A + B)

    Code:
    C = Operations.VectorOps.Subtract(A, B)
    Returns the resultant vector C from the vector subtraction (A - B)

    Code:
     D = Operations.VectorOps.DotProduct(A, B)
    Returns the scalar D which is the dot product of two vectors (A.B)

    Code:
    C = Operations.VectorOps.CrossProduct(A, B)
    Returns the vector C which is the cross product of two vectors (AxB)

    Code:
    C = Operations.VectorOps.TranslateStart(A, mycartcoord)
    Translates the vector A such that the new start point is at the coordinate mycartcoord. A similar function TranslateEnd translates the endpoint to the coordinate mycartcoord.



    Solids:

    Code:
    Dim mysolid As New Solids.Sphere(A, B, C, D)
    Generates a new set of coordinates on a spherical volume. The volume is centred on the origin of coordinate system A with radius B. There are C points on each of D layers. It is also possible to generate ellipsoids, cuboids and cylinders.
    Last edited by zaza; Nov 7th, 2009 at 05:34 AM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  3. #3

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates and Vectors

    Examples:

    1) Draw a circle on a form

    Add a button called Button1 to your form and then add the following code.

    VB Code:
    1. Friend cartsystem As New CoordinateSystem.Cartesian(200, 200) 'Create coordinate system at (200,200)
    2. Friend cart As New Coordinate.Cartesian(cartsystem, 10, 0, 0) 'Create coordinate within that system at (10,0,0)
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5. Dim graphics As System.Drawing.Graphics = Me.CreateGraphics()
    6.  
    7. Dim rectangle As New System.Drawing.Rectangle(cartsystem.XOffset + cart.X, cartsystem.YOffset - cart.Y, 20, 20)
    8.  '20 is the width of the rectangle = diameter of circle
    9.  
    10. graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle)
    11.  
    12. End Sub

    This will draw a black circle of diameter 20 at the location (210,200). We are looking down from the z axis at the xy plane.


    2) Draw a series of circles using a Timer

    Add a Timer called Timer1 to your form and then paste the following code.

    VB Code:
    1. Friend circsystem As New CoordinateSystem.SphericalPolar(200, 200)
    2. Friend circ As New Coordinate.SphericalPolar(circsystem, 10, 0, 0)
    3.  
    4. Friend Sub drawcircle()
    5.  
    6.         Dim graphics As System.Drawing.Graphics = Me.CreateGraphics()
    7.  
    8.         Dim coord As Coordinate.Cartesian
    9.  
    10.         coord = Operations.Transform.LocalSphToCart(circ)
    11.  
    12.         Dim rectangle As New System.Drawing.Rectangle(circsystem.XOffset + coord.X, circsystem.YOffset - coord.Y, 5, 5)
    13.  
    14.         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.FromArgb(285 - (3 * Int(circ.R)), (3 * Int(circ.R)) - 30, 0))
    15.  
    16.         graphics.DrawEllipse(mypen, rectangle)
    17.  
    18.         mypen.Dispose()
    19.  
    20. End Sub
    21.  
    22. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    23.         Call drawcircle()
    24.         circ.Theta = circ.Theta + Operations.Angles.Radians(5)
    25.         If circ.Theta >= Operations.Angles.Radians(359) Then circ.R = circ.R + 10
    26.         If circ.R > 285 / 3 Then Timer1.Enabled = False
    27. End Sub
    28.  
    29. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    30.         Me.Width = 400
    31.         Me.Height = 400
    32.         Timer1.Interval = 100
    33.         Timer1.Enabled = True
    34. End Sub

    As before, we are looking down from the z axis at the xy plane. This code sets up a spherical polar coordinate system called circsystem located at (200,200). Within that, a spherical polar coordinate called circ is defined with radius 10 and no angle (i.e. along the x axis). On each timer tick, the circle is drawn at the current circ coordinate, and then the theta parameter of the coordinate is incremented by 5 degrees (using the Angles.Radians operation to convert into radians). When the angle is greater than 359 degrees, the radius is incremented by 10. The timer is stopped when the radius gets too big.
    Within the drawcircle sub, a cartesian coordinate is defined and used to transform the spherical polar coordinate into the local cartesian coordinates, i.e. into coordinates that can be used for drawing on a form. The circle is then drawn. The colour of the pen is set to change with the radius, so that it blends from red to green.

    You could also try the following code:

    VB Code:
    1. Friend circsystem As New CoordinateSystem.SphericalPolar(200, 200, 0, 0, Operations.Angles.Radians(45))
    2. Friend circ As New Coordinate.SphericalPolar(circsystem, 10, 0, 0)
    3. Friend rotcircsystem As New CoordinateSystem.SphericalPolar(200, 200)
    4.  
    5. Friend Sub drawcircle()
    6.  
    7.         Dim graphics As System.Drawing.Graphics = Me.CreateGraphics()
    8.  
    9.         Dim coord As Coordinate.Cartesian
    10.         Dim newcirccoord As Coordinate.SphericalPolar
    11.  
    12.         newcirccoord = Operations.Transform.Coordinate(circ, rotcircsystem)
    13.         coord = Operations.Transform.LocalSphToCart(newcirccoord)
    14.  
    15.         Dim rectangle As New System.Drawing.Rectangle(circsystem.XOffset + coord.X, circsystem.YOffset - coord.Y, 5, 5)
    16.  
    17.         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.FromArgb(285 - (3 * Int(circ.R)), (3 * Int(circ.R)) - 30, 0))
    18.  
    19.         graphics.DrawEllipse(mypen, rectangle)
    20.  
    21.         mypen.Dispose()
    22.  
    23. End Sub
    24.  
    25. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    26.         Call drawcircle()
    27.         circ.Theta = circ.Theta + Operations.Angles.Radians(5)
    28.         If circ.Theta >= Operations.Angles.Radians(359) Then circ.R = circ.R + 10
    29.         If circ.R > 285 / 3 Then Timer1.Enabled = False
    30. End Sub
    31.  
    32. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    33.         Me.Width = 400
    34.         Me.Height = 400
    35.         Timer1.Enabled = True
    36. End Sub

    Here, the coordinate system circsystem is rotated by 45 degrees about the y axis (i.e. phi = 45 degrees, see picture in previous post), so the right-hand side of the system is tilted towards you and the left-hand side away from you. We are now no longer looking from the z axis of circsystem, so we need to define another coordinate system which corresponds to our viewpoint, called rotcircsystem. We are looking from the z axis of rotcircsystem, and the coordinate newcirccoord within it is used to transform from the tilted coordinate system circsystem. The effect is to take the previous example and tilt it by 45 degrees, pulling the RHS towards you and the LHS away from you.
    Last edited by zaza; Mar 25th, 2006 at 01:27 PM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  4. #4

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates and Vectors

    Update: Added in a Psi rotation about the x axis. You can now rotate about all three cartesian axes.

    3) Plot a rotating cube

    Add a picturebox called Picturebox1 and a timer called Timer1 to your form and then add the following code:

    VB Code:
    1. Friend cartsystem As New CoordinateSystem.Cartesian(200, 200, 0, Operations.Angles.Radians(30), Operations.Angles.Radians(0), Operations.Angles.Radians(0))
    2. Friend cartx As New Coordinate.Cartesian(cartsystem, 100, 0, 0)
    3. Friend carty As New Coordinate.Cartesian(cartsystem, 0, 100, 0)
    4. Friend cartz As New Coordinate.Cartesian(cartsystem, 0, 0, 100)
    5. Friend cartxy As New Coordinate.Cartesian(cartsystem, 100, 100, 0)
    6. Friend cartxz As New Coordinate.Cartesian(cartsystem, 100, 0, 100)
    7. Friend cartyz As New Coordinate.Cartesian(cartsystem, 0, 100, 100)
    8. Friend cartxyz As New Coordinate.Cartesian(cartsystem, 100, 100, 100)
    9.  
    10. Friend myview As New CoordinateSystem.Cartesian(200, 200)
    11.  
    12. Friend Sub drawcube()
    13.  
    14.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    15.  
    16.         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
    17.         Dim coordx As Coordinate.Cartesian
    18.         Dim coordy As Coordinate.Cartesian
    19.         Dim coordz As Coordinate.Cartesian
    20.         Dim coordxy As Coordinate.Cartesian
    21.         Dim coordxz As Coordinate.Cartesian
    22.         Dim coordyz As Coordinate.Cartesian
    23.         Dim coordxyz As Coordinate.Cartesian
    24.  
    25.         coordx = Operations.Transform.Coordinate(cartx, myview)
    26.         coordy = Operations.Transform.Coordinate(carty, myview)
    27.         coordz = Operations.Transform.Coordinate(cartz, myview)
    28.         coordxy = Operations.Transform.Coordinate(cartxy, myview)
    29.         coordxz = Operations.Transform.Coordinate(cartxz, myview)
    30.         coordyz = Operations.Transform.Coordinate(cartyz, myview)
    31.         coordxyz = Operations.Transform.Coordinate(cartxyz, myview)
    32.  
    33.         Dim wpen As New System.Drawing.Pen(System.Drawing.Color.LemonChiffon)
    34.         Dim xpen As New System.Drawing.Pen(System.Drawing.Color.Green)
    35.         Dim ypen As New System.Drawing.Pen(System.Drawing.Color.DodgerBlue)
    36.         Dim zpen As New System.Drawing.Pen(System.Drawing.Color.Red)
    37.        
    38.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    39.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    40.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    41.  
    42.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    43.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    44.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    45.  
    46.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    47.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    48.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    49.  
    50.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    51.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    52.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    53.  
    54.         xpen.Dispose()
    55.         ypen.Dispose()
    56.         zpen.Dispose()
    57.  
    58.     End Sub
    59.  
    60.     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    61.         Call drawcube()
    62.  
    63.         cartsystem.ThetaRotation = cartsystem.ThetaRotation + Operations.Angles.Radians(1)
    64.         cartsystem.PhiRotation = cartsystem.PhiRotation + Operations.Angles.Radians(1)
    65.         cartsystem.PsiRotation = cartsystem.PsiRotation + Operations.Angles.Radians(1)
    66.  
    67.     End Sub
    68.  
    69.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    70.         Me.Width = 600
    71.         Me.Height = 600
    72.         PictureBox1.Left = 0
    73.         PictureBox1.Top = 0
    74.         PictureBox1.Width = 600
    75.         PictureBox1.Height = 600
    76.         Timer1.Interval = 100
    77.         Timer1.Enabled = True
    78.     End Sub

    The angles specified in Cartsystem denote the starting orientation, whilst the 7 coordinates denote the 7 vertices of the cube. The eighth, at (0,0,0) is implied and left as the offest of the coordinate system. Different coloured pens are used to colour the edges and the timer is used to rotate the angle of the coordinate system in the theta, phi and psi directions by 1 degree (converted to radians).

    To make it drift as well, put the following into the timer in addition:

    VB Code:
    1. cartsystem.XOffset = cartsystem.XOffset + 1
    2.  cartsystem.YOffset = cartsystem.YOffset + 1
    3.  myview.XOffset = myview.XOffset + 1
    4.  myview.YOffset = myview.YOffset + 1



    4) Simple graphing

    You will need a picturebox called Picturebox1 and a timer called Timer1.

    Paste the following code:

    VB Code:
    1. Friend cartsystem As New CoordinateSystem.Cartesian(200, 400, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 50)
    2. Friend myview As New CoordinateSystem.Cartesian(200, 200)
    3. Friend Sub drawgraph()
    4.  
    5.         Dim i As Integer
    6.         Dim plotcoord As New Coordinate.Cartesian(cartsystem, 0, 0, 0)
    7.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    8.         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Green)
    9.         Dim axispen As New System.Drawing.Pen(System.Drawing.Color.Black)
    10.         Dim myview As New CoordinateSystem.Cartesian(cartsystem.XOffset, cartsystem.YOffset)
    11.         Dim mycoord As New Coordinate.Cartesian(myview, 0, 0, 0)
    12.  
    13.         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
    14.  
    15.         Dim xaxis As New Coordinate.Cartesian(cartsystem, 2, 0, 0)
    16.         Dim yaxis As New Coordinate.Cartesian(cartsystem, 0, 2, 0)
    17.         Dim zaxis As New Coordinate.Cartesian(cartsystem, 0, 0, 2)
    18.  
    19.         xaxis = Operations.Transform.Coordinate(xaxis, myview)
    20.         yaxis = Operations.Transform.Coordinate(yaxis, myview)
    21.         zaxis = Operations.Transform.Coordinate(zaxis, myview)
    22.  
    23.         graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + xaxis.X), CInt(cartsystem.YOffset - xaxis.Y))
    24.         graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + yaxis.X), CInt(cartsystem.YOffset - yaxis.Y))
    25.         graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + zaxis.X), CInt(cartsystem.YOffset - zaxis.Y))
    26.  
    27.  
    28.         For i = -20 To 20
    29.             plotcoord.X = i / 10
    30.             plotcoord.Y = y(i / 10)
    31.             mycoord = Operations.Transform.Coordinate(plotcoord, myview)
    32.             Dim rectangle As New System.Drawing.Rectangle(cartsystem.XOffset + Int(mycoord.X) - 3, cartsystem.YOffset - Int(mycoord.Y) - 3, 6, 6)
    33.             graphics.DrawEllipse(mypen, rectangle)
    34.         Next
    35.  
    36.         mypen.Dispose()
    37.         axispen.Dispose()
    38.  
    39. End Sub
    40.  
    41. Private Function y(ByVal x As Double)
    42.         Dim yval As Double
    43.         yval = (x ^ 2)
    44.         Return yval
    45. End Function  
    46.  
    47. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    48.  
    49.         Call drawgraph()
    50.         'cartsystem.ThetaRotation = cartsystem.ThetaRotation - Operations.Angles.Radians(2)
    51.         cartsystem.PhiRotation = cartsystem.PhiRotation - Operations.Angles.Radians(2)
    52.         'cartsystem.PsiRotation = cartsystem.PsiRotation + Operations.Angles.Radians(2)
    53.  
    54.     End Sub
    55.  
    56.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    57.         Me.Width = 680
    58.         Me.Height = 600
    59.         PictureBox1.Left = 80
    60.         PictureBox1.Top = 0
    61.         PictureBox1.Width = 600
    62.         PictureBox1.Height = 600
    63.         Timer1.Interval = 100
    64.         Timer1.Enabled = True
    65. End Sub

    Here, a function called y(x) is used to calculate x^2 for each value of x within the For loop in the Drawgraph routine. The rectangle is drawn at the system offset position + the point offset position and then adjusted to centre the circle before the ellipse is actually drawn.
    As usual, the cartesian system cartsystem is used as the graph coordinate system whilst myview transforms it to the screen coordinate system. Cartsystem is tilted so as to be easier to view and rotated using the timer. Axes are drawn in black and points in green.
    Last edited by zaza; Apr 21st, 2006 at 06:14 PM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  5. #5

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates and Vectors

    Update: Added rotations about the local cartesian axes.

    Usage:

    VB Code:
    1. newcoord = Operations.Transform.RotateLocalCartesian.Theta(oldcoord, angle)

    where newcoord and oldcoord are cartesian coordinates, angle is double in radians and Theta indicates to rotate about the local Z axis (by angle radians). There are also Phi and Psi operations to rotate about the y and z axes respectively.

    5) Rotate a cube about its local axes

    Add a picturebox called Picturebox1 and a timer called Timer1 to your form and then add the following code:

    VB Code:
    1. Friend cartsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 1)
    2. Friend cartorig As New Coordinate.Cartesian(cartsystem, -50, -50, -50)
    3. Friend cartx As New Coordinate.Cartesian(cartsystem, 50, -50, -50)
    4. Friend carty As New Coordinate.Cartesian(cartsystem, -50, 50, -50)
    5. Friend cartz As New Coordinate.Cartesian(cartsystem, -50, -50, 50)
    6. Friend cartxy As New Coordinate.Cartesian(cartsystem, 50, 50, -50)
    7. Friend cartxz As New Coordinate.Cartesian(cartsystem, 50, -50, 50)
    8. Friend cartyz As New Coordinate.Cartesian(cartsystem, -50, 50, 50)
    9. Friend cartxyz As New Coordinate.Cartesian(cartsystem, 50, 50, 50)
    10. Friend myview As New CoordinateSystem.Cartesian(250, 250)
    11.  
    12. Friend Sub drawcube()
    13.  
    14.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    15.  
    16.         Dim coordorig As Coordinate.Cartesian
    17.         Dim coordx As Coordinate.Cartesian
    18.         Dim coordy As Coordinate.Cartesian
    19.         Dim coordz As Coordinate.Cartesian
    20.         Dim coordxy As Coordinate.Cartesian
    21.         Dim coordxz As Coordinate.Cartesian
    22.         Dim coordyz As Coordinate.Cartesian
    23.         Dim coordxyz As Coordinate.Cartesian
    24.  
    25.         coordorig = cartorig
    26.  
    27.         coordorig = Operations.Transform.Coordinate(cartorig, myview)
    28.         coordx = Operations.Transform.Coordinate(cartx, myview)
    29.         coordy = Operations.Transform.Coordinate(carty, myview)
    30.         coordz = Operations.Transform.Coordinate(cartz, myview)
    31.         coordxy = Operations.Transform.Coordinate(cartxy, myview)
    32.         coordxz = Operations.Transform.Coordinate(cartxz, myview)
    33.         coordyz = Operations.Transform.Coordinate(cartyz, myview)
    34.         coordxyz = Operations.Transform.Coordinate(cartxyz, myview)
    35.  
    36.  
    37.         Dim wpen As New System.Drawing.Pen(System.Drawing.Color.LemonChiffon)
    38.         Dim xpen As New System.Drawing.Pen(System.Drawing.Color.Green)
    39.         Dim ypen As New System.Drawing.Pen(System.Drawing.Color.DodgerBlue)
    40.         Dim zpen As New System.Drawing.Pen(System.Drawing.Color.Red)
    41.  
    42.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    43.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    44.         graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    45.  
    46.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    47.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    48.         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    49.  
    50.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))
    51.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    52.         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    53.  
    54.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))
    55.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))
    56.         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))
    57.  
    58.         xpen.Dispose()
    59.         ypen.Dispose()
    60.         zpen.Dispose()
    61.  
    62. End Sub
    63.  
    64. Friend Sub drawaxes()
    65.  
    66.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    67.         Dim axispen As New System.Drawing.Pen(System.Drawing.Color.Black)
    68.  
    69.         Dim xaxis As New Coordinate.Cartesian(cartsystem, 200, 0, 0)
    70.         Dim yaxis As New Coordinate.Cartesian(cartsystem, 0, 200, 0)
    71.         Dim zaxis As New Coordinate.Cartesian(cartsystem, 0, 0, 200)
    72.  
    73.         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
    74.  
    75.         xaxis = Operations.Transform.Coordinate(xaxis, myview)
    76.         yaxis = Operations.Transform.Coordinate(yaxis, myview)
    77.         zaxis = Operations.Transform.Coordinate(zaxis, myview)
    78.  
    79.         Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + xaxis.X), CInt(cartsystem.YOffset - xaxis.Y))
    80.         Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + yaxis.X), CInt(cartsystem.YOffset - yaxis.Y))
    81.         Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + zaxis.X), CInt(cartsystem.YOffset - zaxis.Y))
    82.  
    83.         axispen.Dispose()
    84.  
    85. End Sub
    86.  
    87.  
    88. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    89.      
    90.         cartorig = Operations.Transform.RotateLocalCartesian.Psi(cartorig, Operations.Angles.Radians(2))
    91.         cartx = Operations.Transform.RotateLocalCartesian.Psi(cartx, Operations.Angles.Radians(2))
    92.         carty = Operations.Transform.RotateLocalCartesian.Psi(carty, Operations.Angles.Radians(2))
    93.         cartz = Operations.Transform.RotateLocalCartesian.Psi(cartz, Operations.Angles.Radians(2))
    94.         cartxy = Operations.Transform.RotateLocalCartesian.Psi(cartxy, Operations.Angles.Radians(2))
    95.         cartxz = Operations.Transform.RotateLocalCartesian.Psi(cartxz, Operations.Angles.Radians(2))
    96.         cartyz = Operations.Transform.RotateLocalCartesian.Psi(cartyz, Operations.Angles.Radians(2))
    97.         cartxyz = Operations.Transform.RotateLocalCartesian.Psi(cartxyz, Operations.Angles.Radians(2))
    98.  
    99.  Call drawaxes()
    100.  Call drawcube()
    101.  
    102.  
    103. End Sub
    104.  
    105.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    106.         Me.Width = 680
    107.         Me.Height = 600
    108.         PictureBox1.Left = 80
    109.         PictureBox1.Top = 0
    110.         PictureBox1.Width = 600
    111.         PictureBox1.Height = 600
    112.         Timer1.Interval = 20
    113.         Timer1.Enabled = True
    114.  
    115. End Sub

    Here, the code in the timer is used to rotate each of the corner points of the cube in the Psi direction (about local x axis) by 2 degrees before plotting it. Note that the positions of the axes do not change and the cube is rotating about them.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  6. #6

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates and Vectors

    Update: Added the functionality to generate a set of coordinates on the surface of different volumes; spheres, ellipsoids, cuboids and cylinders.

    6) Generate a wireframe model of different solids

    Add a picturebox called Picturebox1 and a timer called Timer1 to your form and then add the following code:

    VB Code:
    1. Friend circsystem As New CoordinateSystem.SphericalPolar(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)
    2. Friend cylsystem As New CoordinateSystem.CylindricalPolar(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)
    3. Friend cubsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)
    4. Friend cartsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 1)
    5.  
    6.  
    7. Friend Sub drawcircsolid()
    8.  
    9.         'For drawing circular solids
    10.  
    11.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    12.         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Black)
    13.  
    14.         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
    15.  
    16.         'Generate a solid here
    17.  
    18.         Dim mysolid As New Solids.Sphere(circsystem, 100, 16, 10)
    19.         'Dim mysolid As New Solids.Ellipsoid(cylsystem, 100, 50, 16, 10)
    20.         'Dim mysolid As New Solids.Cylinder(cylsystem, 100, 100, 16, 14, 3)
    21.  
    22.         Dim i As Integer
    23.         Dim j As Integer
    24.         Dim counter As Integer = 1
    25.  
    26.         Dim cartcoord As New Coordinate.Cartesian(cartsystem, 0, 0, 0)
    27.  
    28.         'Convert the coordinates on the solid into cartesian array
    29.         Dim newpointsarray(mysolid.Layers, mysolid.Points) As Coordinate.Cartesian
    30.  
    31.         'Put the single coordinate at each end
    32.         newpointsarray(0, 0) = Operations.Transform.Coordinate(mysolid.PointsArray(0), cartsystem)
    33.         newpointsarray(0, 1) = Operations.Transform.Coordinate(mysolid.PointsArray(mysolid.TotalPoints), cartsystem)
    34.  
    35.         'Iterate through the points and layers joining lines to preceding coordinates where applicable
    36.         For i = 1 To mysolid.Layers
    37.             For j = 1 To mysolid.Points
    38.                 newpointsarray(i, j) = Operations.Transform.Coordinate(mysolid.PointsArray(counter), cartsystem)
    39.                 If j > 1 Then
    40.                     graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(i, j).X), CInt(cartsystem.YOffset - newpointsarray(i, j).Y), CInt(cartsystem.XOffset + newpointsarray(i, j - 1).X), CInt(cartsystem.YOffset - newpointsarray(i, j - 1).Y))
    41.                 End If
    42.                 If j = mysolid.Points Then
    43.                     graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(i, j).X), CInt(cartsystem.YOffset - newpointsarray(i, j).Y), CInt(cartsystem.XOffset + newpointsarray(i, 1).X), CInt(cartsystem.YOffset - newpointsarray(i, 1).Y))
    44.                 End If
    45.  
    46.                 If i = 1 Then
    47.                     graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(i, j).X), CInt(cartsystem.YOffset - newpointsarray(i, j).Y), CInt(cartsystem.XOffset + newpointsarray(0, 0).X), CInt(cartsystem.YOffset - newpointsarray(0, 0).Y))
    48.                 Else
    49.                     graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(i, j).X), CInt(cartsystem.YOffset - newpointsarray(i, j).Y), CInt(cartsystem.XOffset + newpointsarray(i - 1, j).X), CInt(cartsystem.YOffset - newpointsarray(i - 1, j).Y))
    50.                 End If
    51.                 If i = mysolid.Layers Then
    52.                     graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(i, j).X), CInt(cartsystem.YOffset - newpointsarray(i, j).Y), CInt(cartsystem.XOffset + newpointsarray(0, 1).X), CInt(cartsystem.YOffset - newpointsarray(0, 1).Y))
    53.                 End If
    54.  
    55.                 counter = counter + 1
    56.             Next
    57.         Next
    58.  
    59.         mysolid = Nothing
    60.  
    61.         mypen.Dispose()
    62. End Sub
    63.  
    64. Friend Sub drawcuboid()
    65.  
    66.         'For drawing cuboids
    67.  
    68.         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics
    69.         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Black)
    70.  
    71.         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
    72.  
    73.         'Generate a solid here
    74.  
    75.         Dim mysolid As New Solids.Cuboid(cubsystem, 200, 150, 100, 7, 7, 7)
    76.  
    77.         Dim i As Integer
    78.         Dim j As Integer
    79.         Dim k As Integer
    80.  
    81.  
    82.         'Convert the cuboid coordinates to a new cartesian array
    83.         Dim newpointsarray(mysolid.XPoints, mysolid.YPoints, mysolid.ZPoints) As Coordinate.Cartesian
    84.  
    85.  
    86.         'Iterate through all the points, transforming to the new array where appropriate
    87.         For i = 0 To mysolid.ZPoints - 1
    88.             For j = 0 To mysolid.YPoints - 1
    89.                 For k = 0 To mysolid.XPoints - 1
    90.  
    91.                     If Not (mysolid.PointsArray(k, j, i) Is Nothing) Then
    92.                         newpointsarray(k, j, i) = Operations.Transform.Coordinate(mysolid.PointsArray(k, j, i), cartsystem)
    93.                     End If
    94.  
    95.  
    96.                     If k > 0 And (j = 0 Or j = mysolid.YPoints - 1 Or i = 0 Or i = mysolid.ZPoints - 1) Then
    97.                         graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(k, j, i).X), CInt(cartsystem.YOffset - newpointsarray(k, j, i).Y), CInt(cartsystem.XOffset + newpointsarray(k - 1, j, i).X), CInt(cartsystem.YOffset - newpointsarray(k - 1, j, i).Y))
    98.                     End If
    99.  
    100.                     If j > 0 And (k = 0 Or k = mysolid.XPoints - 1 Or i = 0 Or i = mysolid.ZPoints - 1) Then
    101.                         graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(k, j, i).X), CInt(cartsystem.YOffset - newpointsarray(k, j, i).Y), CInt(cartsystem.XOffset + newpointsarray(k, j - 1, i).X), CInt(cartsystem.YOffset - newpointsarray(k, j - 1, i).Y))
    102.                     End If
    103.  
    104.                     If i > 0 And (k = 0 Or k = mysolid.XPoints - 1 Or j = 0 Or j = mysolid.YPoints - 1) Then
    105.                         graphics.DrawLine(mypen, CInt(cartsystem.XOffset + newpointsarray(k, j, i).X), CInt(cartsystem.YOffset - newpointsarray(k, j, i).Y), CInt(cartsystem.XOffset + newpointsarray(k, j, i - 1).X), CInt(cartsystem.YOffset - newpointsarray(k, j, i - 1).Y))
    106.                     End If
    107.  
    108.                 Next
    109.             Next
    110.         Next
    111.  
    112.         mysolid = Nothing
    113.  
    114.         mypen.Dispose()
    115. End Sub
    116.  
    117.  
    118. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    119.        
    120.         Call drawcircsolid()
    121.         'drawcuboid()
    122.  
    123.         cartsystem.ThetaRotation = cartsystem.ThetaRotation - Operations.Angles.Radians(2)
    124.  
    125. End Sub
    126.  
    127. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    128.         Me.Width = 680
    129.         Me.Height = 600
    130.         PictureBox1.Left = 80
    131.         PictureBox1.Top = 0
    132.         PictureBox1.Width = 600
    133.         PictureBox1.Height = 600
    134.         Timer1.Interval = 100
    135.         Timer1.Enabled = True
    136.  
    137. End Sub


    The solids are generated within their own coordinate systems (circsystem, cylsystem or cubsystem) and rotated about the z axis with the timer.
    drawcircsolid generates the wireframe mesh for the sphere, ellipsoid and cylinder - change the commented lines to choose a different solid. drawcuboid generates the cuboid. Choose which by changing the comment in the Timer code.
    The parameters of the solids can be changed so that, for example, the cuboid routine generates a cube or the ellipsoid routine generates a sphere.
    Note that when generating cylinders, the number of layers includes any circles on the ends of the cylinder - the number of layers in between are then calculated from what is left.
    Last edited by zaza; Apr 21st, 2006 at 05:57 PM.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  7. #7
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Re: Coordinates, Vectors and 3D volumes

    Hi Zaza, sorry for dredging up such an old post. Please could you tell me if the classes you have outlined here can be used for transforming cartesian points from the world coordinate system to a user coordinate system defined by an origin, positive x point and positive y point? I can see transforms between different coordinate systems, but nothing as above.

    At the moment I am using methods built into a CAD API I am using, but I would like to do the transforms without having to refer to 3rd party objects.

    Regards,
    Nick

  8. #8

    Thread Starter
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,484

    Re: Coordinates, Vectors and 3D volumes

    These are just general coordinate transforms, so they will work between any two coordinate systems howsoever defined.

    If you were to use them to define both your "world" system and your "user" system according to whatever the relational conditions are, then the methods and functions here will allow you to transform between them at will.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

  9. #9
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Re: Coordinates, Vectors and 3D volumes

    Great; thanks very much for that. I will give them a whirl.

  10. #10
    New Member
    Join Date
    Apr 2012
    Posts
    3

    Re: Coordinates, Vectors and 3D volumes

    Hi Zaza, I have converted the classes to C# and all seems to be working. The only problem I have had so far is that most of the CAD packages I have worked with define coordinate systems by specifying 3 points; an origin point, a positive point on the X axis, and a positive point on the Y axis.

    Your method for specifying a coordinate system is with x, y, and z offsets and with rotations around the 3 axes. I can get the offsets OK, these should just be the coordinates of the origin point, but I am struggling with calculating the angles from the other 2 points.

    I have tried this method, which works the way I thought it should for rotations around the z axis, but I think I'm doing something silly that causes it to misbehave for rotations in the other axes:

    public Cartesian(bgPoint AOrigin, bgPoint AXAxis, bgPoint AYAxis)
    {
    offsetx = AOrigin.x;
    offsety = AOrigin.y;
    offsetz = AOrigin.z;
    double a = Math.Atan2(AXAxis.y - AOrigin.y, AXAxis.x - AOrigin.x) * 180 / Math.PI;
    double b = Math.Atan2(AXAxis.z - AOrigin.z, AXAxis.x - AOrigin.x) * 180 / Math.PI;
    double c = Math.Atan2(AYAxis.z - AOrigin.z, AYAxis.y - AOrigin.y) * 180 / Math.PI;
    if (AOrigin.z == AXAxis.z && AXAxis.z == AYAxis.z)
    {
    rottheta = Operations.Angles.Radians(-a);
    rotphi = Operations.Angles.Radians(b);
    rotpsi = Operations.Angles.Radians(c);
    }
    else
    {
    rottheta = Operations.Angles.Radians(b);
    rotphi = Operations.Angles.Radians(-a);
    rotpsi = Operations.Angles.Radians(c);
    }
    }

    Are you able to see where I'm going wrong? Any help would be most appreciated.

    Regards,
    Nick

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.