Coordinates, Vectors and 3D volumes-VBForums

# Thread: Coordinates, Vectors and 3D volumes

1. ## 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.

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:
`Dim Global As New CoordinateSystem.Cartesian()         Dim mycartsystem As New CoordinateSystem.Cartesian(10, 10, 0, 0, 0, 0, 1)         Dim mycartcoord As New Coordinate.Cartesian(mycartsystem, 1, 0, 0)         Dim mycartcoord2 As New Coordinate.Cartesian(mycartsystem, 0, 1, 0)                Dim mycartcoordorigin As New Coordinate.Cartesian(mycartsystem, 0, 0, 0)         Dim myvector1 As New Vector.Cartesian        myvector1.StartPoint = mycartcoordorigin        myvector1.EndPoint = mycartcoord         Dim myvector2 As New Vector.Cartesian        myvector2.StartPoint = mycartcoordorigin        myvector2.EndPoint = mycartcoord2         Dim subvector As New Vector.Cartesian         subvector = Operations.VectorOps.Subtract(myvector1, myvector2)         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

2. ## 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
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

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.

3. ## Re: Coordinates and Vectors

Examples:

1) Draw a circle on a form

VB Code:
`Friend cartsystem As New CoordinateSystem.Cartesian(200, 200) 'Create coordinate system at (200,200)Friend cart As New Coordinate.Cartesian(cartsystem, 10, 0, 0) 'Create coordinate within that system at (10,0,0) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.ClickDim graphics As System.Drawing.Graphics = Me.CreateGraphics() Dim rectangle As New System.Drawing.Rectangle(cartsystem.XOffset + cart.X, cartsystem.YOffset - cart.Y, 20, 20) '20 is the width of the rectangle = diameter of circle graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle) 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:
`Friend circsystem As New CoordinateSystem.SphericalPolar(200, 200)Friend circ As New Coordinate.SphericalPolar(circsystem, 10, 0, 0) Friend Sub drawcircle()         Dim graphics As System.Drawing.Graphics = Me.CreateGraphics()         Dim coord As Coordinate.Cartesian         coord = Operations.Transform.LocalSphToCart(circ)         Dim rectangle As New System.Drawing.Rectangle(circsystem.XOffset + coord.X, circsystem.YOffset - coord.Y, 5, 5)         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.FromArgb(285 - (3 * Int(circ.R)), (3 * Int(circ.R)) - 30, 0))         graphics.DrawEllipse(mypen, rectangle)         mypen.Dispose() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick        Call drawcircle()        circ.Theta = circ.Theta + Operations.Angles.Radians(5)        If circ.Theta >= Operations.Angles.Radians(359) Then circ.R = circ.R + 10        If circ.R > 285 / 3 Then Timer1.Enabled = FalseEnd Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 400        Me.Height = 400        Timer1.Interval = 100        Timer1.Enabled = TrueEnd 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:
`Friend circsystem As New CoordinateSystem.SphericalPolar(200, 200, 0, 0, Operations.Angles.Radians(45))Friend circ As New Coordinate.SphericalPolar(circsystem, 10, 0, 0)Friend rotcircsystem As New CoordinateSystem.SphericalPolar(200, 200) Friend Sub drawcircle()         Dim graphics As System.Drawing.Graphics = Me.CreateGraphics()         Dim coord As Coordinate.Cartesian        Dim newcirccoord As Coordinate.SphericalPolar         newcirccoord = Operations.Transform.Coordinate(circ, rotcircsystem)        coord = Operations.Transform.LocalSphToCart(newcirccoord)         Dim rectangle As New System.Drawing.Rectangle(circsystem.XOffset + coord.X, circsystem.YOffset - coord.Y, 5, 5)         Dim mypen As New System.Drawing.Pen(System.Drawing.Color.FromArgb(285 - (3 * Int(circ.R)), (3 * Int(circ.R)) - 30, 0))         graphics.DrawEllipse(mypen, rectangle)         mypen.Dispose() End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick        Call drawcircle()        circ.Theta = circ.Theta + Operations.Angles.Radians(5)        If circ.Theta >= Operations.Angles.Radians(359) Then circ.R = circ.R + 10        If circ.R > 285 / 3 Then Timer1.Enabled = FalseEnd Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 400        Me.Height = 400        Timer1.Enabled = TrueEnd 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.

4. ## 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:
`Friend cartsystem As New CoordinateSystem.Cartesian(200, 200, 0, Operations.Angles.Radians(30), Operations.Angles.Radians(0), Operations.Angles.Radians(0))Friend cartx As New Coordinate.Cartesian(cartsystem, 100, 0, 0)Friend carty As New Coordinate.Cartesian(cartsystem, 0, 100, 0)Friend cartz As New Coordinate.Cartesian(cartsystem, 0, 0, 100)Friend cartxy As New Coordinate.Cartesian(cartsystem, 100, 100, 0)Friend cartxz As New Coordinate.Cartesian(cartsystem, 100, 0, 100)Friend cartyz As New Coordinate.Cartesian(cartsystem, 0, 100, 100)Friend cartxyz As New Coordinate.Cartesian(cartsystem, 100, 100, 100) Friend myview As New CoordinateSystem.Cartesian(200, 200) Friend Sub drawcube()         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)        Dim coordx As Coordinate.Cartesian        Dim coordy As Coordinate.Cartesian        Dim coordz As Coordinate.Cartesian        Dim coordxy As Coordinate.Cartesian        Dim coordxz As Coordinate.Cartesian        Dim coordyz As Coordinate.Cartesian        Dim coordxyz As Coordinate.Cartesian         coordx = Operations.Transform.Coordinate(cartx, myview)        coordy = Operations.Transform.Coordinate(carty, myview)        coordz = Operations.Transform.Coordinate(cartz, myview)        coordxy = Operations.Transform.Coordinate(cartxy, myview)        coordxz = Operations.Transform.Coordinate(cartxz, myview)        coordyz = Operations.Transform.Coordinate(cartyz, myview)        coordxyz = Operations.Transform.Coordinate(cartxyz, myview)         Dim wpen As New System.Drawing.Pen(System.Drawing.Color.LemonChiffon)        Dim xpen As New System.Drawing.Pen(System.Drawing.Color.Green)        Dim ypen As New System.Drawing.Pen(System.Drawing.Color.DodgerBlue)        Dim zpen As New System.Drawing.Pen(System.Drawing.Color.Red)               graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(wpen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))        graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))        graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         xpen.Dispose()        ypen.Dispose()        zpen.Dispose()     End Sub     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick        Call drawcube()         cartsystem.ThetaRotation = cartsystem.ThetaRotation + Operations.Angles.Radians(1)        cartsystem.PhiRotation = cartsystem.PhiRotation + Operations.Angles.Radians(1)        cartsystem.PsiRotation = cartsystem.PsiRotation + Operations.Angles.Radians(1)     End Sub     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 600        Me.Height = 600        PictureBox1.Left = 0        PictureBox1.Top = 0        PictureBox1.Width = 600        PictureBox1.Height = 600        Timer1.Interval = 100        Timer1.Enabled = True    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:
`cartsystem.XOffset = cartsystem.XOffset + 1 cartsystem.YOffset = cartsystem.YOffset + 1 myview.XOffset = myview.XOffset + 1 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:
`Friend cartsystem As New CoordinateSystem.Cartesian(200, 400, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 50)Friend myview As New CoordinateSystem.Cartesian(200, 200)Friend Sub drawgraph()         Dim i As Integer        Dim plotcoord As New Coordinate.Cartesian(cartsystem, 0, 0, 0)        Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics        Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Green)        Dim axispen As New System.Drawing.Pen(System.Drawing.Color.Black)        Dim myview As New CoordinateSystem.Cartesian(cartsystem.XOffset, cartsystem.YOffset)        Dim mycoord As New Coordinate.Cartesian(myview, 0, 0, 0)         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)         Dim xaxis As New Coordinate.Cartesian(cartsystem, 2, 0, 0)        Dim yaxis As New Coordinate.Cartesian(cartsystem, 0, 2, 0)        Dim zaxis As New Coordinate.Cartesian(cartsystem, 0, 0, 2)         xaxis = Operations.Transform.Coordinate(xaxis, myview)        yaxis = Operations.Transform.Coordinate(yaxis, myview)        zaxis = Operations.Transform.Coordinate(zaxis, myview)         graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + xaxis.X), CInt(cartsystem.YOffset - xaxis.Y))        graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + yaxis.X), CInt(cartsystem.YOffset - yaxis.Y))        graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + zaxis.X), CInt(cartsystem.YOffset - zaxis.Y))          For i = -20 To 20            plotcoord.X = i / 10            plotcoord.Y = y(i / 10)            mycoord = Operations.Transform.Coordinate(plotcoord, myview)            Dim rectangle As New System.Drawing.Rectangle(cartsystem.XOffset + Int(mycoord.X) - 3, cartsystem.YOffset - Int(mycoord.Y) - 3, 6, 6)            graphics.DrawEllipse(mypen, rectangle)        Next         mypen.Dispose()        axispen.Dispose() End Sub Private Function y(ByVal x As Double)        Dim yval As Double        yval = (x ^ 2)        Return yvalEnd Function   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick          Call drawgraph()        'cartsystem.ThetaRotation = cartsystem.ThetaRotation - Operations.Angles.Radians(2)        cartsystem.PhiRotation = cartsystem.PhiRotation - Operations.Angles.Radians(2)        'cartsystem.PsiRotation = cartsystem.PsiRotation + Operations.Angles.Radians(2)     End Sub     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 680        Me.Height = 600        PictureBox1.Left = 80        PictureBox1.Top = 0        PictureBox1.Width = 600        PictureBox1.Height = 600        Timer1.Interval = 100        Timer1.Enabled = TrueEnd 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.

5. ## Re: Coordinates and Vectors

Usage:

VB Code:
`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:
`Friend cartsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 1)Friend cartorig As New Coordinate.Cartesian(cartsystem, -50, -50, -50)Friend cartx As New Coordinate.Cartesian(cartsystem, 50, -50, -50)Friend carty As New Coordinate.Cartesian(cartsystem, -50, 50, -50)Friend cartz As New Coordinate.Cartesian(cartsystem, -50, -50, 50)Friend cartxy As New Coordinate.Cartesian(cartsystem, 50, 50, -50)Friend cartxz As New Coordinate.Cartesian(cartsystem, 50, -50, 50)Friend cartyz As New Coordinate.Cartesian(cartsystem, -50, 50, 50)Friend cartxyz As New Coordinate.Cartesian(cartsystem, 50, 50, 50)Friend myview As New CoordinateSystem.Cartesian(250, 250) Friend Sub drawcube()         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics         Dim coordorig As Coordinate.Cartesian        Dim coordx As Coordinate.Cartesian        Dim coordy As Coordinate.Cartesian        Dim coordz As Coordinate.Cartesian        Dim coordxy As Coordinate.Cartesian        Dim coordxz As Coordinate.Cartesian        Dim coordyz As Coordinate.Cartesian        Dim coordxyz As Coordinate.Cartesian         coordorig = cartorig         coordorig = Operations.Transform.Coordinate(cartorig, myview)        coordx = Operations.Transform.Coordinate(cartx, myview)        coordy = Operations.Transform.Coordinate(carty, myview)        coordz = Operations.Transform.Coordinate(cartz, myview)        coordxy = Operations.Transform.Coordinate(cartxy, myview)        coordxz = Operations.Transform.Coordinate(cartxz, myview)        coordyz = Operations.Transform.Coordinate(cartyz, myview)        coordxyz = Operations.Transform.Coordinate(cartxyz, myview)          Dim wpen As New System.Drawing.Pen(System.Drawing.Color.LemonChiffon)        Dim xpen As New System.Drawing.Pen(System.Drawing.Color.Green)        Dim ypen As New System.Drawing.Pen(System.Drawing.Color.DodgerBlue)        Dim zpen As New System.Drawing.Pen(System.Drawing.Color.Red)         graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(wpen, CInt(cartsystem.XOffset + coordorig.X), CInt(cartsystem.YOffset - coordorig.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))         graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(xpen, CInt(cartsystem.XOffset + coordxy.X), CInt(cartsystem.YOffset - coordxy.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordy.X), CInt(cartsystem.YOffset - coordy.Y))        graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))        graphics.DrawLine(ypen, CInt(cartsystem.XOffset + coordyz.X), CInt(cartsystem.YOffset - coordyz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordx.X), CInt(cartsystem.YOffset - coordx.Y))        graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordz.X), CInt(cartsystem.YOffset - coordz.Y))        graphics.DrawLine(zpen, CInt(cartsystem.XOffset + coordxz.X), CInt(cartsystem.YOffset - coordxz.Y), CInt(cartsystem.XOffset + coordxyz.X), CInt(cartsystem.YOffset - coordxyz.Y))         xpen.Dispose()        ypen.Dispose()        zpen.Dispose() End Sub Friend Sub drawaxes()         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics        Dim axispen As New System.Drawing.Pen(System.Drawing.Color.Black)         Dim xaxis As New Coordinate.Cartesian(cartsystem, 200, 0, 0)        Dim yaxis As New Coordinate.Cartesian(cartsystem, 0, 200, 0)        Dim zaxis As New Coordinate.Cartesian(cartsystem, 0, 0, 200)         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)         xaxis = Operations.Transform.Coordinate(xaxis, myview)        yaxis = Operations.Transform.Coordinate(yaxis, myview)        zaxis = Operations.Transform.Coordinate(zaxis, myview)         Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + xaxis.X), CInt(cartsystem.YOffset - xaxis.Y))        Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + yaxis.X), CInt(cartsystem.YOffset - yaxis.Y))        Graphics.DrawLine(axispen, CInt(cartsystem.XOffset), CInt(cartsystem.YOffset), CInt(cartsystem.XOffset + zaxis.X), CInt(cartsystem.YOffset - zaxis.Y))         axispen.Dispose() End Sub  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick              cartorig = Operations.Transform.RotateLocalCartesian.Psi(cartorig, Operations.Angles.Radians(2))        cartx = Operations.Transform.RotateLocalCartesian.Psi(cartx, Operations.Angles.Radians(2))        carty = Operations.Transform.RotateLocalCartesian.Psi(carty, Operations.Angles.Radians(2))        cartz = Operations.Transform.RotateLocalCartesian.Psi(cartz, Operations.Angles.Radians(2))        cartxy = Operations.Transform.RotateLocalCartesian.Psi(cartxy, Operations.Angles.Radians(2))        cartxz = Operations.Transform.RotateLocalCartesian.Psi(cartxz, Operations.Angles.Radians(2))        cartyz = Operations.Transform.RotateLocalCartesian.Psi(cartyz, Operations.Angles.Radians(2))        cartxyz = Operations.Transform.RotateLocalCartesian.Psi(cartxyz, Operations.Angles.Radians(2))  Call drawaxes() Call drawcube()  End Sub     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 680        Me.Height = 600        PictureBox1.Left = 80        PictureBox1.Top = 0        PictureBox1.Width = 600        PictureBox1.Height = 600        Timer1.Interval = 20        Timer1.Enabled = True 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.

6. ## 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:
`Friend circsystem As New CoordinateSystem.SphericalPolar(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)Friend cylsystem As New CoordinateSystem.CylindricalPolar(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)Friend cubsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(0), Operations.Angles.Radians(0), 1)Friend cartsystem As New CoordinateSystem.Cartesian(250, 250, 0, Operations.Angles.Radians(0), Operations.Angles.Radians(45), Operations.Angles.Radians(45), 1)  Friend Sub drawcircsolid()         'For drawing circular solids         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics        Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Black)         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)         'Generate a solid here         Dim mysolid As New Solids.Sphere(circsystem, 100, 16, 10)        'Dim mysolid As New Solids.Ellipsoid(cylsystem, 100, 50, 16, 10)        'Dim mysolid As New Solids.Cylinder(cylsystem, 100, 100, 16, 14, 3)         Dim i As Integer        Dim j As Integer        Dim counter As Integer = 1         Dim cartcoord As New Coordinate.Cartesian(cartsystem, 0, 0, 0)         'Convert the coordinates on the solid into cartesian array        Dim newpointsarray(mysolid.Layers, mysolid.Points) As Coordinate.Cartesian         'Put the single coordinate at each end        newpointsarray(0, 0) = Operations.Transform.Coordinate(mysolid.PointsArray(0), cartsystem)        newpointsarray(0, 1) = Operations.Transform.Coordinate(mysolid.PointsArray(mysolid.TotalPoints), cartsystem)         'Iterate through the points and layers joining lines to preceding coordinates where applicable        For i = 1 To mysolid.Layers            For j = 1 To mysolid.Points                newpointsarray(i, j) = Operations.Transform.Coordinate(mysolid.PointsArray(counter), cartsystem)                If j > 1 Then                    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))                End If                If j = mysolid.Points Then                    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))                End If                 If i = 1 Then                    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))                Else                    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))                End If                If i = mysolid.Layers Then                    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))                End If                 counter = counter + 1            Next        Next         mysolid = Nothing         mypen.Dispose()End Sub Friend Sub drawcuboid()         'For drawing cuboids         Dim graphics As System.Drawing.Graphics = PictureBox1.CreateGraphics        Dim mypen As New System.Drawing.Pen(System.Drawing.Color.Black)         PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)         'Generate a solid here         Dim mysolid As New Solids.Cuboid(cubsystem, 200, 150, 100, 7, 7, 7)         Dim i As Integer        Dim j As Integer        Dim k As Integer          'Convert the cuboid coordinates to a new cartesian array        Dim newpointsarray(mysolid.XPoints, mysolid.YPoints, mysolid.ZPoints) As Coordinate.Cartesian          'Iterate through all the points, transforming to the new array where appropriate        For i = 0 To mysolid.ZPoints - 1            For j = 0 To mysolid.YPoints - 1                For k = 0 To mysolid.XPoints - 1                     If Not (mysolid.PointsArray(k, j, i) Is Nothing) Then                        newpointsarray(k, j, i) = Operations.Transform.Coordinate(mysolid.PointsArray(k, j, i), cartsystem)                    End If                      If k > 0 And (j = 0 Or j = mysolid.YPoints - 1 Or i = 0 Or i = mysolid.ZPoints - 1) Then                        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))                    End If                     If j > 0 And (k = 0 Or k = mysolid.XPoints - 1 Or i = 0 Or i = mysolid.ZPoints - 1) Then                        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))                    End If                     If i > 0 And (k = 0 Or k = mysolid.XPoints - 1 Or j = 0 Or j = mysolid.YPoints - 1) Then                        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))                    End If                 Next            Next        Next         mysolid = Nothing         mypen.Dispose()End Sub  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick               Call drawcircsolid()        'drawcuboid()         cartsystem.ThetaRotation = cartsystem.ThetaRotation - Operations.Angles.Radians(2) End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Width = 680        Me.Height = 600        PictureBox1.Left = 80        PictureBox1.Top = 0        PictureBox1.Width = 600        PictureBox1.Height = 600        Timer1.Interval = 100        Timer1.Enabled = True 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.

7. ## 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. ## 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.

9. ## Re: Coordinates, Vectors and 3D volumes

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

10. ## 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)
{
}
else
{
}
}

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