Graphics Problem!

This is a discussion on Graphics Problem! within the C Programming forums, part of the General Programming Boards category; Hey - wonder if anyone can have a look at this and see what's up with it. It's meant to ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    London
    Posts
    7

    Graphics Problem!

    Hey - wonder if anyone can have a look at this and see what's up with it. It's meant to take an array of xcoordinates and ycoordinates from an earlier piece of code and then plot the straight fit line of those points. For some reason though I'm not reading the arrays into the function properly or something, because I'm getting gobbledeegook.

    Regards.

    Code:
    void plot(float xcoords[], float ycoords[], int numpoints)
          {
               float xmax, ymax, xmin, ymin;
               float xpixels [1000], ypixels [1000];
               int xres, yres;
         
               //Prints input data as a check.
               printf("x\t    y");
               printf("\n---------------------------\n");
               int i;
               for (i=0;i<numpoints;i++) {
                                         printf("%f\t    %f\n", xcoords[i], ycoords[i]);
                                         }//End Loop.
                                     
               //INITIALISE GRAPHICS MODE AND SETUP SCREEN:
         
               GrSetMode(GR_default_graphics);
               xres = GrScreenX();
               yres = GrScreenY();
               
               //SCALING:
         
               xmax = maximum(xcoords, numpoints); //Call "maximum" function.
               ymax = maximum(ycoords, numpoints); 
               
               xmin = minimum(ycoords, numpoints); //Call "minimum" function.
               ymin = minimum(ycoords, numpoints);
         
               int xpixmin = xres - 20; 
               int ypixmin = yres - 20; 
               int xpixmax = xres;
               int ypixmax = yres;
               
               for(i=0;i<numpoints;i++) {
                                        xpixels[i] = xpixmin + ((xcoords[i]-xmin)*(xpixmax-xpixmin));
                                        ypixels[i] = ypixmin + ((ycoords[i]-ymin)*(ypixmax-ypixmin));
                                        } // End loop.
                                        
               printf("xres is %d and yres is %d \n", xres, yres);
               printf("\n\nx pixel\t    y pixel");
               printf("\n-----------------------------\n");
         
               for (i=0;i<numpoints;i++) {
                                         printf("%f\t    %f\n", xpixels[i], ypixels[i]);
                                         }
                                   
               // DRAW AXES:
         
               GrLine(20, yres - 20, xres, yres - 20, 9); // x-axis.
               GrLine(20, yres - 20, 20, 20, 9); // y-axis. 
         
               // DRAW CURVE OR LINE:
                       
               float xp = xpixels[0], yp = ypixels[0];
               for(i=1;i<numpoints;i++) {
                                        GrLine((int)xp, (int)yp, (int)xpixels[i], (int)ypixels[i], 4);
                                        xp = xpixels[i];
                                        yp = ypixels[i];
                                        } // End loop.
               // EXIT GRAPHICS SCREEN
         
               printf("Enter any key to continue...\n");
               scanf("%d", &xp);
               GrSetMode(GR_default_text);
               }
         
               plot(xp, yp, n); // Calls function.

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    try printing out the values of (int)xp ,(int)yp,cuz u may not find what u r expecting.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
               printf("Enter any key to continue...\n");
               scanf("%d", &xp);
    There are better ways to wait for the user to "press <enter> to continue": see the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginning Game Programming Type Books
    By bumfluff in forum Game Programming
    Replies: 36
    Last Post: 09-13-2006, 04:15 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Graphics Programming :: Approach and Books
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 05-11-2004, 08:33 PM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Graphics Problem
    By drdroid in forum C++ Programming
    Replies: 5
    Last Post: 02-27-2002, 01:37 PM

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