Thread: Plotting a horizontal line

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Question Plotting a horizontal line

    I'm trying to create a horizontal line using a predefined colour between any two points on an image, which is 256 wide by 128 high, which is printed to a .ppm file that is then viewed using XnView. This is created by using a for loop which also contains code to create a vertical line, which works fine. I have been advised to use only one for loop on one of the variables, and then insert the other into the for loop. However, it is only printing out vertical lines and will print only one pixel for the horizontal line. Could anyone offer any tips on how to progress? The code is included below.

    Code:
    #include <stdio.h>
    #include <math.h>
    #define I_WIDE 256 // Define row with a value of 256 
    #define I_HIGH 128 // Define column with a value of 128
    #define RANGE 255 // Defines the range that the colour can take
    
    void main (void)
    {
        unsigned char image [I_WIDE] [I_HIGH] [3]; // Array of Predefined Values as shown above
        // Declare variables before the code, Intialise variables to 0
        int x = 0, y = 0;
        int xpos = 0, ypos = 5, end_xpos = 5, end_ypos = 5;
        int r1 = 55, g1 = 210, b1 = 38;
        FILE*ppmfile = NULL;
    	
        for (y = ypos;y <= end_ypos; y++)
        {
              x = xpos; xpos < end_xpos; xpos++;
              image[x][y][0] = (unsigned char)r1; 
              image[x][y][1] = (unsigned char)g1; 
              image[x][y][2] = (unsigned char)b1;
        }
                              
        ppmfile = fopen("test.ppm","w"); // Opens the file to be written
        fprintf(ppmfile, "P3\n"); // Magic Number at the top of the document
        fprintf(ppmfile, "#test.ppm\n"); // Name of the document (Commented out)
        fprintf(ppmfile, "%d %d\n", I_WIDE, I_HIGH); // Inserts the size of the image into the document
        fprintf(ppmfile, "%d\n", RANGE); // number of colour variations in the document 
        
        
        for (y = 0; y < I_HIGH; y++)
        {
                for (x = 0; x < I_WIDE; x++)
                {
                        fprintf(ppmfile, "%4u %4u %4u \n", image[x][y][0], image[x][y][1], image[x][y][2]); // prints all of the different values
                }
        }
    
    fclose(ppmfile); 
    }
    Thanks in advance.
    Last edited by hessajee; 01-27-2009 at 10:13 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > x = xpos; xpos < end_xpos; xpos++;
    Is this meant to be a for loop?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    It was a for loop, but as I was told that one for loop would do the job, I removed it, hence producing the result mentioned earlier. Thank you for your question.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Why are you trying to make a horizontal line, using a y variable in a loop? You need an x variable in a loop, and no y.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    The reason being is that the same for loop will be able to print both vertical and horizontal lines. It should be able to go along the y, plotting the x values as appropriate, and vice versa depending on what is in the for loop. Hope that clarifies the matter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  3. Need help with drawing a line
    By dv007 in forum C Programming
    Replies: 3
    Last Post: 06-25-2002, 01:10 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM