Like Tree2Likes

Make this image in c:

This is a discussion on Make this image in c: within the C Programming forums, part of the General Programming Boards category; Make this image in c: You will write the following functions: 1. make_header(int w, int h) ; the same as ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    8

    Make this image in c:

    Make this image in c:
    Name:  image.DYI6LW.png
Views: 501
Size:  8.2 KB

    You will write the following functions:
    1. make_header(int w, int h) ; the same as the header we used in class
    2. print_Row (int yValue, int width, int height); the purpose of this function is to make a
    row of pixels from column start to column finish; yValue will provide the row number.
    3. make_pixel (unsigned char r, unsigned char g, unsigned char b)

    i understand that i need to use y=mx+b and tell it to display a color if it is above or below the two lines. I just don't understand how to do that.

    here is my code so far:
    Code:
    #include <stdio.h>
    
    
    //This function makes the header file for the image
    void make_header(int width, int height)
    {
      fprintf(stdout, "P6\n"); 
      fprintf(stdout, "%d %d %d\n", width, height, 255); 
    }
    
    
    //The function prints the pixels on the screen
    void make_pixel (unsigned char r, unsigned char g, unsigned char b)
    {
      fprintf(stdout, "%c%c%c", r, g, b);
    }
    
    
    /*the purpose of this function is to make a
    row of pixels from column start to column finish; yValue will provide the row number.*/
    void print_Row (int yValue, int width, int height)
    {
      int x=0, m=(1/1), b=0;
      yValue= ((width/2) + (height/2))-1;
      int i;
        for (i = 0; i < yValue; i++)
        {
          make_pixel(255,0,0);
          b++;
          x++;
          yValue= m*x+b;
        }
        for (i = 0; i > yValue; i++)
        {
          make_pixel(0,0,255);
          b++;
          x++;
          yValue= m*x+b;
        }
    }
    
    
    void make_image (int width, int height)
    {
      int i;
      int yValue;
      yValue= ((width/2) + (height/2))-1;
      make_header(width, height);
            for (i = 0; i < height; i++)
        {
             print_Row(yValue, width, height);
        }
    }
    
    
    int main (void)
    {
      int width;
      int height;
      fprintf(stdin,"Enter the width and the height: ");
      fscanf(stdin, "%d %d", &width, &height);
      make_image(width, height);
    
    
    
    
    
    
      return (0);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    It would be better if you directed the output to a file, rather than stdout.

    Next, start with simpler image, say a solid red rectangle.

    Do you have a program which will view the files you generate?
    Netpbm format - Wikipedia, the free encyclopedia

    Consider better names for variables, like
    Code:
    for ( rows = 0 ; rows < height/2 ; rows++ ) {
      for ( cyan = 0 ; cyan < numCyan ; cyan++ ) { }
      for ( magenta = 0 ; magenta < numMagenta ; magenta++ ) { }
      for ( red = 0 ; red < numRed ; red++ ) { }
      // can you figure out how numCyan, numMagenta and numRed
      // change from one row to the next?
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can't print a prompt to user, by fprintf(stdin, ...)

    There's no output except 255 apparently (cursor moves, but nothing is shown). It compiles, but it's hard to see what's doing when there's no output. Even if it's obviously wrong, get SOMETHING to show on the screen.

    This might give you some idea's perhaps:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define CYAN    3
    #define RED     4
    #define YELLOW 14
    #define MAGENTA 5
    #define CH    219
    
    int main(void) {
       int cpx,cpy,red,cyan,magenta,midx,midy,r;
       int ht=26, wt=26;
       
       midx=wt/2;
       midy=ht/2;
       cpx=cpy=r=cyan=magenta=0;
       red=wt;
       printf("midy is %d\n\n",midy);
       while(cpy<midy) {
          _textcolor(YELLOW);
          printf("%2d. ",r++);
          
          while(++cpx<=cyan) {
             _textcolor(CYAN); 
             printf("%c",CH);
          }
          while(cpx>cyan && cpx<red){
             _textcolor(MAGENTA);
             printf("%c",CH);
             cpx++;
          }
          while(cpx>magenta && cpx < wt) {
             _textcolor(RED);
             printf("%c",CH);
             cpx++;
          }
          --red;
          ++cyan;
          ++cpy;
          _putch('\n');
          cpx=0;    
       }
    
       _textcolor(15);
       printf("\n");
       return 0;
    }
    Last edited by Adak; 10-15-2012 at 12:50 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > This might give you some idea's perhaps:
    And a health warning about how obsolete it is, and what subset of compilers it has a chance of working on.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    > This might give you some idea's perhaps:
    And a health warning about how obsolete it is, and what subset of compilers it has a chance of working on.
    1) It's there to show an algorithm, not a finished image.

    2) Every OS has some way of printing colored text to the console. In our last discussion on this topic, we heard from several compiler users that saying that they DID have conio.h. included with their compiler. It's not as obsolete as I thought it was.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If it's there for backward compatibility, then it's still obsolete.
    It enables old code to be compiled and maintained (in a transition phase).

    You shouldn't be lazy, and use it in new code.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Salem View Post
    If it's there for backward compatibility, then it's still obsolete.
    It enables old code to be compiled and maintained (in a transition phase).

    You shouldn't be lazy, and use it in new code.
    C has no code to control text color.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    On the contrary, it does. If you can use conio.h you can just as easily use something else. windows.h, ncurses.h, etc

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by whiteflags View Post
    On the contrary, it does. If you can use conio.h you can just as easily use something else. windows.h, ncurses.h, etc
    Windows.h, ncurses.h, etc., are not part of the C standard. Conio.h works on all versions of Windows, also works on DOS, if you happen to run across such relic hardware, and is easier to use (more concise).

    Which would you rather keyboard?

    conio.h:
    gotoxy()

    or

    windows.h:
    setConsoleCursorPosition()

    I like standards, but I love being practical.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Not bad for a console window though.
    Attached Images Attached Images  

  12. #12
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    It would be just as easy to do something like this:
    Code:
    $ ./program | display -
    to display the PPM image output of the program. The best part is that it doesn't depend on a non-standard codepage or old, obsolete (not to mention non-standard) functions like _textcolor, so it can run on any system with the relatively ubiquitous ImageMagick package installed.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by christop View Post
    It would be just as easy to do something like this:
    Code:
    $ ./program | display -
    to display the PPM image output of the program. The best part is that it doesn't depend on a non-standard codepage or old, obsolete (not to mention non-standard) functions like _textcolor, so it can run on any system with the relatively ubiquitous ImageMagick package installed.
    That's OK for Linux if you have that package installed - but not for Windows. I've never heard of "ImageMagick", for Linux or Windows. My Linux PC's pretty much fold 24/7 for medical research anyway.

    The conio.h functions are simple to use, and most compilers support conio.h anyway.

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    This is what I am trying to do in c with my program. The colors don't matter, I just need to get the math right.

  15. #15
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    Not bad for a console window though.
    This is what I am trying to do in c with my program. The colors don't matter, I just need to get the math right.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make an image reappear
    By hannah_n in forum C# Programming
    Replies: 2
    Last Post: 03-05-2011, 06:53 PM
  2. Replies: 4
    Last Post: 03-13-2010, 04:10 AM
  3. I can't make bmp image files show up in an SDL window
    By Lucas89 in forum Game Programming
    Replies: 5
    Last Post: 05-25-2009, 01:04 PM
  4. Problems with Image Magick [Unable to Quantisize Image]
    By Maragato in forum C Programming
    Replies: 1
    Last Post: 09-18-2006, 10:41 PM
  5. Beginner, trying to learn how to make an image.
    By shishkabob in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2004, 08:46 PM

Tags for this Thread


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