Thread: Arrays

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Arrays

    Hi guys, I'm at uni and I have some coursework involving a virtual line in C/C++.

    The user should be able to input start and end coords (3D) of this line eg X1,Y1,Z1 and X2,Y2,Z2, and execute various transformations on the line as a whole around arbitrary points eg. rotation, translation etc.

    Things is, I am stupid, and I don't even know how to set up a single 2D array to cope with this and then allow a user to modify the points in the array. I have a functioning-ish menu (see code below) but I haven't a clue about the important bits.

    Could anyone please help me on how to get the array to show its six coordinates and then allow the user to modify them at least?

    Thanks, Simon Phillips <spuk>

    NB- can be written in C or C++

    Code:
    #include <stdio.h>
    #include <stdlib.h>   //required for user to exit program
    #include <math.h>     //needed to apply maths such as sin, cos etc.
    int main()
    {
       double p1[3]={0,0,0};
       double p2[3]={0,0,0};  
       
       int option;  
       
       for (;;)
       {
          printf("\nMAIN MENU\n");
          printf("--------------------\n");
          printf("1| Set Start Point\n");                 //1. Sets start point of line
          printf("2| Set End Point\n");                   //2. Sets end point of line
          printf("3| Apply Translations\n");              //3. Translates the line by user-selected amount
          printf("4| Apply Rotation\n");                  //4. Rotates the line by user-selected amount
          printf("5| Apply Scaling\n");                   //5. Scales the line by user-selected amount
          printf("6| Evaluate Line\n");                   //6. User selects a point along line between 0 and 1
          printf("7| Exit Program\n");                    //7. Finish using program
          
          printf("\nPlease enter your choice:\t");           
          
          scanf("%d", &option); printf("\n"); 
          
          printf("%lf\n", p1);
          
          if (option==1);  
          else if (option==2);
          else if (option==3);
          else if (option==4);
          else if (option==5);
          else if (option==6);
          else if (option==7) 
          {
             printf ("Program terminated.\n\n");
             exit(1);
          }
          else printf ("You have entered an incorrect value, please enter a number from the menu only.\n");// Tells user if wrong number entered
          
       }
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well at this point in your class I'm sure you were taught how to output the values of the array, so that should be no problem.

    For the rest of this, you have to provide a little more information so we can help.

    1. Are you having trouble with the mathematics aspect or the programming aspect of scaling, rotating, and translating the line?
    2. Are these functions on the line based around the midpoint or one of the endpoints of the line?
    3. Do you know how to use any of the following; functions, structures, switch statements?
    4. Is this graphical at all? Are you expected to output what the line looks like on a grid?
    5. By 3D array, are you refering to an array such as:
    Code:
    int coord[3];
    // or
    int coord[x][y][z];  // This is what a 3D array really is.
    Answer as much of that as you can, then I'll see how I can help you.
    Sent from my iPadŽ

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Just find a basic tutorial on translating, rotating, etc. for vectors.

    I cant find a link for a good one off the top of my head, but http://home10.inet.tele.dk/moelhave/...00000000000000 is okay.

    I know a really simple, and to the point, but its in a .html file I downloaded.

    Traditionally, we deal with one plane, the XY plane. Now there are the XZ and YZ planes too.

    For the purposes of this text, the Z axis goes into/comes out of the screen and the XY plane is the screen. The Y axis is vertical. Just think of the XZ plane as looking down from the top and the XY as looking at the screen. The YZ is a side view. Its not difficult after a bit of programming, because increasing the Y values of things makes them go up on the screen. Changing the Z moves them side to side, etc.

    There are 3 basic operations that can be performed on a point:

    - Scaling/skewing:

    Scaling is the multiplication of the x, y & z locations by a value (usually not 0)

    Equation:
    x=x*n
    y=y*n
    z=z*n

    Skewing is the multiplication of the x, y & z locations by 3 different values (one for each axis)

    Equation:
    x=x*nx
    y=y*ny
    z=z*nz

    - Translating (or transforming):

    Translating is moving the points.
    Each set of points is located in its own coordinate system, called model space. Within model space, points can be manipulated. Changing from model space to world space is known as translation.

    Translation equation:
    x=x+new_x
    y=y+new_y where the point to move to is (new_x,new_y,new_z)
    z=z+new_z

    - Rotating:

    Rotation is the moving of points a specified amount of arc (an arc is a part of the edge of a circle) around a circle on the plane of rotation (x, y or z) with a center at 0, 0, 0.
    Rotation is accomplished by appling a form of the circle equation to an axis.

    The circle equation is:

    x=x*cos(q) - y*sin(q)
    y=x*sin(q) + y*cos(q) where q is an angle in radians (covered below)

    Note that if we add a z=z equation, we have rotation about the z axis.
    Extra: I don't have the proof for this, but you can see why it forms a circle with a graphing calculator


    Rotation for y:

    x=x*cos(q)+z*sin(q)
    y=y
    z=-x*sin(q)+z*cos(q)

    Rotation for x:

    x=x
    y=y*cos(q)-z*sin(q)
    z=y*sin(q)+z*cos(q)
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 02:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM