Results 1 to 7 of 7

Thread: 2D Mapping, 3D Calulations of Points

  1. #1

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    2D Mapping, 3D Calulations of Points

    I'm pretty good at doing this on My own, But of course, If you can do this, It'l cut down on development time.

    I want to do some 2d drawing of a n X n X n matrix of cubes.
    Obviously, thats a 2D representation of a series of points mapped
    in 3d space. I'd like to be able to rotate an individual cube around
    its center, which is X,Y,Z degrees away from the 3 different axis, and
    whos center of mass is a certain radial distance from the center
    of the entire 3D origin.

    {Also, I'd like to be able to change 'Perspective' on the whole mass of 27 cubes, but i'd settle for much less, if you willing to share.}

    So,

    I'd also like to do some other 3 to2 d transformaytons.

    Anybody have some Equations?
    Just wondering.

    -Lou

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    I have an application which does gravitational computations and generates a 3D display. I assume a viewer one meter (100cm) from a viewing window. His position is known by Longitude, Latitude, & Distance from the origin. My Latitude range is -90 to +90 degrees, & Longitude is 0-360 degrees. Angles are converted to radians for the VB Trig Functions. All my units are centimeters and I use ScaleMode in cm for the Picture Box used for display.

    Not sure what the equations are for transforming from 3 direction cosines (or 3 angles) to longitude and latitude. Perhaps you know how to do this. It should not be difficult to do. I will think about it if you ask.

    To provide 3D perspective, I transform XYZ coordinates using a 3 by 3 rotation matrix and scale XY distances by the distances along the new Z-Axis. I had to experiment a bit with the rotation matrix. I created it from the product of a matrix which does rotation about the Z-Axis (Longitude) and a matrix which does rotation about the new Y-Axis. The result is that the viewer is on the new Z-Axis. My original matrix changed a left handed XYZ system to a right handed XYZ system. I had to fiddle a bit with signs to make it look correct.

    The following code is incomplete, but has the critical Subroutines and Variable declarations.

    The MatrixMaker Subroutine creates the rotation matrix. The Transformer Subroutine determines XY coordinates with correct perspective. XY coordinates are scaled using distances along Z-Axis: Ratio is Viewer to Viewing window divided by Viewer to XYZ position. You probably do not need the Else-Clause code in the Transformer Subroutine.

    I have not shown my SineCosine Subroutine nor the Subroutine which actually plots points. The Point Plotter is a bit complicated because it maintains an array, allowing for replotting if the viewer position changes, without redoing the gravitational calculations.

    I found it handy to include a color variable with my PointModel.

    Let me know if the following helps you. If you have questions, I will try to answer them.
    Code:
    'Dpi is 6.28318 53071 79586 47692 52868
    'Pie is 3.14159 26535 89793 23846 26434
    '          1.57079 63267 94896 61923 13217
    '            .78539 81633 97448 30961 56608
    '            .39269 90816 98724 15480 78304
    Public Const Dpi As Double = 6.28318530717959  '360 degrees
    Public Const Pie As Double = 3.14159265358979  '180 degrees
    Public Const Hpi As Double = 1.5707963267949   ' 90 degrees
    Public Const Qpi As Double = 0.785398163397448 ' 45 degrees
    
    Type MatrixModel
        R11 As Double
        R12 As Double
        R13 As Double
        R21 As Double
        R22 As Double
        R23 As Double
        R31 As Double
        R32 As Double
        R33 As Double
    End Type
    
    Type PointModel
        X As Double
        Y As Double
        Z As Double
        Color As Long
     End Type
    
    Public Sub MatrixMaker(R As Double, Z As Integer, _
                               X As Integer, M As MatrixModel)
    Dim Longitude As Double
    Dim Latitude As Double
    Dim SineX As Double
    Dim CosineX As Double
    Dim SineZ As Double
    Dim CosineZ As Double
    
    Longitude = X * Pie / 180#
    Latitude = Z * Pie / 180#
    Call SineCosine(Longitude, SineX, CosineX)
    Call SineCosine(Latitude, SineZ, CosineZ)
    
    'M.R11 = -sineX   This seemed right, but did not work'
    'M.R12 = cosineX'
    'M.R13 = 0'
    'M.R21 = -cosineX * sineZ'
    'M.R22 = -sineX * sineZ'
    'M.R23 = -cosineZ'
    'M.R31 = -cosineX * cosineZ'
    'M.R32 = -sineX * cosineZ'
    'M.R33 = sineZ'
    
    M.R11 = -SineX
    M.R12 = CosineX
    M.R13 = 0
    M.R21 = CosineX * SineZ
    M.R22 = SineX * SineZ
    M.R23 = -CosineZ
    M.R31 = CosineX * CosineZ
    M.R32 = SineX * CosineZ
    M.R33 = SineZ
    
    ViewerX = R * CosineX * CosineZ
    ViewerY = R * SineX * CosineZ
    ViewerZ = R * SineZ
     
    End Sub
    
    Public Sub Transformer(ViewX As Single, ViewY As Single, _
                                    Source As PointModel)
    Dim Target As PointModel
    Dim P As PointModel
    Dim DeltaZ As Double
    
    If ViewSlot = 0 Then 'Observer looking toward orign
    
            Target.X = Source.X * Rmatrix.R11 + Source.Y * Rmatrix.R12 + Source.Z * Rmatrix.R13
            Target.Y = Source.X * Rmatrix.R21 + Source.Y * Rmatrix.R22 + Source.Z * Rmatrix.R23
            Target.Z = Source.X * Rmatrix.R31 + Source.Y * Rmatrix.R32 + Source.Z * Rmatrix.R33
        
        Else 'Observer looking toward object indicated by ViewSlot
        
            P.X = Source.X - Body(ViewSlot).Point.X
            P.Y = Source.Y - Body(ViewSlot).Point.Y
            P.Z = Source.Z - Body(ViewSlot).Point.Z
            Target.X = P.X * Rmatrix.R11 + P.Y * Rmatrix.R12 + P.Z * Rmatrix.R13
            Target.Y = P.X * Rmatrix.R21 + P.Y * Rmatrix.R22 + P.Z * Rmatrix.R23
            Target.Z = P.X * Rmatrix.R31 + P.Y * Rmatrix.R32 + P.Z * Rmatrix.R33
      End If
    
    DeltaZ = ViewerR - Target.Z 'Scale XY coordinates for viewer 100 cm from Window
    ViewX = Target.X * 100# / DeltaZ
    ViewY = Target.Y * 100# / DeltaZ
    
    End Sub
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  3. #3
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151
    NotLKH: Did my post help you? Was that the sort of code you were looking for?
    Live long & prosper.

    The Dinosaur from prehistoric era prior to computers.

    Eschew obfuscation!
    If a billion people believe a foolish idea, it is still a foolish idea!
    VB.net 2010 Express
    64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.

  4. #4
    Lively Member
    Join Date
    Jun 2001
    Location
    Banana Republic
    Posts
    115
    Try the attachment.
    Marriage - is not a word, but a sentence.

  5. #5
    Lively Member
    Join Date
    Jun 2001
    Location
    Banana Republic
    Posts
    115
    OMG....I didn't see.. the above example uses cartesian coordinates.
    Marriage - is not a word, but a sentence.

  6. #6

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Sorry, I have'nt responded for a while.

    Guv- Thanks for the code. It looked potentially useful, but I thought trying to convert physical measurements to longi & latitude measurements might be a little daunting.

    After searching thru Many pages of "Rotational Matrices" inet articles, Great if I just stepped out of My Matrix college class yesterday, and all explaining the theory, with no applied examples, I stumbled over this, which is rotating a point around a 3d line's unit vector:

    VB Code:
    1. import java.util.*;
    2. /**
    3. * A rotation about a line.
    4. *
    5. * The l ine is specified by a point (x,y,z) and a unit vector (a,b,c).
    6. * Rotation (r) is in radians.
    7. */
    8. public class Rotation extends Transformation
    9. {
    10. private double m[][];
    11. public Rotation(double x, double y, double z,
    12. double a, double b, double c,
    13. double r)
    14. {
    15. double sin_r = Math.sin(r);
    16. double cos_r = Math.cos(r);
    17. double i = 1.0 - cos_r;
    18. double a_a_i = a * a * i;
    19. double b_b_i = b * b * i;
    20. double c_c_i = c * c * i;
    21. double a_b_i = a * b * i;
    22. double a_c_i = a * c * i;
    23. double b_c_i = b * c * i;
    24. double a_sin_r = a * sin_r;
    25. double b_sin_r = b * sin_r;
    26. double c_sin_r = c * sin_r;
    27. m = new double [3][3];
    28. m[0][0] = a_a_i + cos_r;
    29. m[1][0] = a_b_i - c_sin_r;
    30. m[2][0] = a_c_i + b_sin_r;
    31. m[0][1] = a_b_i + c_sin_r;
    32. m[1][1] = b_b_i + cos_r;
    33. m[2][1] = b_c_i - a_sin_r;
    34. m[0][2] = a_c_i - b_sin_r;
    35. m[1][2] = b_c_i + a_sin_r;
    36. m[2][2] = c_c_i + cos_r;
    37. }
    38. public void transform(Coordinates coords)
    39. {
    40. if (coords == null) return;
    41. double x = coords.x * m[0][0] + coords.y * m[0][1] + coords.z * m[0][2];
    42. double y = coords.x * m[1][0] + coords.y * m[1][1] + coords.z * m[1][2];
    43. double z = coords.x * m[2][0] + coords.y * m[2][1] + coords.z * m[2][2];
    44. coords.x = x;
    45. coords.y = y;
    46. coords.z = z;
    47. }
    48. }

    I'm not into Java yet, but this was perfectly understandable, and I retrofitted it to my app.

    ThinkTank: Thats a good example for 3d X,Y,Z Translocation. You should try getting some rotation in there!

    Thanks all!
    And if you're curious about what I'm developing, just ask. I've got a thing or two left to do, Like optimizing the code if I'm rotating 10x10x10 cubes. But, it's still worth a look or two.

    And, once I'm done VB'ing it, I'm going to try doing it in Direct3d, since I've never used it before, even though I've had it for a few months.

    -Lou

  7. #7

    Thread Starter
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397
    Well, this is pretty much of a dead thread, but if you want to see
    what I developed with the 3d code, you can find the project
    here or see a sample screenshot here.
    -Lou

Posting Permissions

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



Click Here to Expand Forum to Full Width