Reversing output in this function

This is a discussion on Reversing output in this function within the C Programming forums, part of the General Programming Boards category; I have posted a couple of threads relating to this program, Astar pathifinding, it is now complete and i just ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    Reversing output in this function

    I have posted a couple of threads relating to this program, Astar pathifinding, it is now complete and i just need to sort out this final function.

    It works fine at the moment, this function plots the shortest path found, backwards from the target square to the start square, and at present draws it too with circles, what i need is to somehow save this path back and then draw it from the start square to the target square.

    in the earlier functions on each loop the current best path square is stored in ParentSQ[] the value stored is the number of the grid on the board found by 'down * maxacross + across'
    then the adjacent 8squares are filled with this value in ParentGrid[cgY][cgX]so they 'point' to the centre square.
    so reversing back from target i take the last value in ParentSQ[] (PlotXY) and turn it into cgY and cgX co-ordinates with
    cgY = PlotXY / MXDWN , cgX = PlotXY - MXACR * cgY;
    then i see what is stored in ParentGrid[cgY][cgX] then i take this value (PlotXY) and turn it into cgY, cgX etc etc till i get to start square.


    Code:
    ShowPath(int PathSearch,int ParentGrid[MXDWN][MXACR],int ParentSQ[MXDWN * MXACR],int cgY,int cgX)
    {
    
    int count = 0;
    int PlotXY = 0;
    char nextgo;
    
    	      PlotXY = ParentSQ[PathSearch];
    	      cgY = PlotXY / MXDWN;
    	      cgX = PlotXY - MXACR * cgY;
    	      setfillstyle(1,15);
    
    		 for(count = PathSearch; count > -1; count--)
    		 {
    
                      if(ParentGrid[cgY][cgX] == 0) count = 0;
    		  circle((cgX * MXACR)+ 10,(cgY * MXDWN)+ 10,8);
                      PlotXY = ParentGrid[cgY][cgX];
    		  cgY = PlotXY / MXDWN;
    		  cgX = PlotXY - MXACR * cgY;
    		  
    		  nextgo = getch();
    		 }
    
    return(0);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    If you are just looking to reverse your list of coords being used to draw the circles, just put a push(x,y) instruction right after the circle() call. Then after the for loop, have another loop to pop() back the coords. Something like:
    Code:
    #define MAX_STACK 100
     
    typedef struct Coord {
        int x, y;
    } Coord;
    
    Coord stack[MAX_STACK];
     
    int push( int x, int y )
    {
        // save in stack, return 0 if array exceeded, otherwise 1
    }
     
    int pop( int *x, int *y )
    {
        // store top of stack into *x, *y
        // return 0 when empty, 1 otherwise
    }
    Last edited by oogabooga; 02-06-2008 at 02:15 PM. Reason: error in struct definition

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    hey thanks, i have no idea what the push and pop means but will look them up, how is it that x & y can both be stored seperately in the same array element tho? If i had known that was possible i could have saved myself some hassle!

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Look up the "stack" data structure.
    Push adds elements to the top of the stack.
    Pop removes them in reverse order (last in, first out).

    You can use a struct to gather x and y together, or you could use two separate arrays if you wish.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    cheers buddy, that will open a few doors in general methinks!

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of an array holding just an integer in each element, it's now holding a struct which contains one x and one y number.

    Array's of structs are very useful, indeed. Access to each part of the struct can be done using the dot operator.

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    hokay, is this similar to an array element containing another array? which was something i had forgotten about incidentally, i will have to look up this dot operator malarkey... is it a bit like in objects .... cat.meow etc? i recall this as an tutorial example... cat object with meow attribute or something.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,630
    is this similar to an array element containing another array?
    In a way, yes, though there is a layer of abstraction not present with a two dimensional array. Instead of thinking "I have an array of two element arrays" you now think "I have an array of Coords".

    i will have to look up this dot operator malarkey... is it a bit like in objects .... cat.meow etc?
    Yes, structs are a (the?) way by which one can do object based programming in C.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    man, this sounds just the ticket, i think this will let me rewrite a fair chunk of code into a much clearer and efficient method... meant to get into structures and start on more C++ object orientated stuff before now, but have always had other options, this project at the moment is throwing problems at me that are making me learn new material, which is fine by me... one step closer to launching my world changing operating system that sweeps all before it... hee!!!! ;->

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21