#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Desperate for help! Creation of image in C


    I'm having to write a program for homework using the C language that will create a ppm file. The ppm file must have a diagonal line going from the top left corner to the bottom right corner. On the top of the line, the image must be blue, and under the line, the image must be green. The user of the program will specify the width and height of the ppm image.

    I've written a program that will create a ppm image before, except they consisted of horizontal/vertical stripes of color. I am completely stuck on this with the line being diagonal. :chomp:

    Can someone help point me in the right direction? I've got the program to make the top half (top rectangle of the image) one color, and the bottom half (bottom rectangle of the image) another color. I just don't know how to make this line separating the two colors diagonal.

    Thank you so much in advance!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    Code:
    for ( green = 0 ; green < midpoint ; green++ ) { }
    for ( blue = midpoint+1 ; blue < width ; blue++ ) { }
    midpoint++;  // for a square
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Thank you for your quick reply!

    I understand how to divide the image up into rectangular sections. What I'm having trouble with is dividing it into two halves diagonally. The final image needs to be rectangular (dimensions based on user input which I already have the code for) with a diagonal line going from the top left corner to the bottom right corner.

    Thank you!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,902
    Rep Power
    481
    You could make a less than table


    Code:
       i. 8    NB. input lines are indented as a 3 space prompt
    0 1 2 3 4 5 6 7
       < table i. 8
    ┌─┬───────────────┐
    │<│0 1 2 3 4 5 6 7│
    ├─┼───────────────┤
    │0│0 1 1 1 1 1 1 1│
    │1│0 0 1 1 1 1 1 1│
    │2│0 0 0 1 1 1 1 1│
    │3│0 0 0 0 1 1 1 1│
    │4│0 0 0 0 0 1 1 1│
    │5│0 0 0 0 0 0 1 1│
    │6│0 0 0 0 0 0 0 1│
    │7│0 0 0 0 0 0 0 0│
    └─┴───────────────┘
       |. 'barg'
    grab
       |. </~ i. 8
    0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 1
    0 0 0 0 0 0 1 1
    0 0 0 0 0 1 1 1
    0 0 0 0 1 1 1 1
    0 0 0 1 1 1 1 1
    0 0 1 1 1 1 1 1
    0 1 1 1 1 1 1 1
    
    
    
       ((+:@:=) + |.@:(</~)) i. 8
    2 0 0 0 0 0 0 0
    0 2 0 0 0 0 0 1
    0 0 2 0 0 0 1 1
    0 0 0 2 0 1 1 1
    0 0 0 0 3 1 1 1
    0 0 0 1 1 3 1 1
    0 0 1 1 1 1 3 1
    0 1 1 1 1 1 1 3
    Last edited by b49P23TIvg; October 17th, 2012 at 09:02 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,393
    Rep Power
    1871
    The final image needs to be rectangular (dimensions based on user input which I already have the code for) with a diagonal line going from the top left corner to the bottom right corner.
    So if the rectangle was 20 wide, and 10 high, can you work out how much to increment midpoint by on each iteration of the loop?

    This is maths, not programming.
    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

IMN logo majestic logo threadwatch logo seochat tools logo