Thread: Make this image in c:

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Midy is the name of the variable that holds height of the figure, divided by 2. It's the mid point in height (y) value.

    How are you displaying the graphic you're creating?

    You should draw out on paper, the diagram (large), and note for each color, what the slope should be, for that section:

    Example:
    Magenta pixels are all:
    y value > midy
    > slope of lineLR
    > slope of lineRL

    and repeat that for every different colored section. Now you can easily refer to it, when you're coding up your program.
    Last edited by Adak; 10-16-2012 at 03:43 PM.

  2. #32
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Where can one find and install additional header files, such as this infamous conio.h?

    Thanks
    Last edited by gratiafide; 10-16-2012 at 05:31 PM.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gratiafide View Post
    Where can one find and install additional header files, such as this infamous conio.h?

    Thanks
    Several compilers support it, (and have it in their include files directory), but other compilers do not support it.

    If they don't support it, I don't believe you can make them support it. You can't just d/l the conio.h file, and put it into your include file directory, and expect it to work.

    At least that has been my experience. The conio.h file has to be made up to fit that particular compiler and OS.

    Sounds like what you are looking for is a library, and not an include header file.

  4. #34
    Registered User
    Join Date
    Oct 2012
    Posts
    99
    Quote Originally Posted by Adak View Post
    Several compilers support it, (and have it in their include files directory), but other compilers do not support it.

    If they don't support it, I don't believe you can make them support it. You can't just d/l the conio.h file, and put it into your include file directory, and expect it to work.

    At least that has been my experience. The conio.h file has to be made up to fit that particular compiler and OS.

    Sounds like what you are looking for is a library, and not an include header file.
    So is that why people pay $BIG BUCKS for Microsoft Developer Studio instead of just using Pelles or CodeBlocks?

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by gratiafide View Post
    So is that why people pay $BIG BUCKS for Microsoft Developer Studio instead of just using Pelles or CodeBlocks?
    I don't know. I use Pelles C, because I did NOT like MS's product. Would be OK I guess, if I was working in C++, but I program in C.

  6. #36
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    It seems Windows 7 users cannot download the ANSICON from the link I provided above.

    Since the ZIP archive has the following DISTRIBUTION notice,
    The original zipfile can be freely distributed, by any means. However, I would like to be informed if it is placed on a CD-ROM (other than an archive compilation; permission is granted, I'd just like to know). Modified versions may be distributed, provided it is indicated as such in the version text and a source diff is included.
    I believe it might be useful to redistribute it right here.

    The attached Attachment 12092 was downloaded on September 16, 2012, at about 14:30 GMT.
    Code:
    SHA256SUM: 62e9f6fa2d808dc400c8a1b77a96e7d20b5640e8ca3c4b8ea8dc0566485e17bc
    SHA1SUM:   1ecf68076d548b35ca2e5893564de819bd534407
    MD5SUM:    0ad9972c4bc27f1575ac843b1b289585
    Length:    121489 bytes
    Last edited by Nominal Animal; 10-17-2012 at 12:31 AM. Reason: Added length in bytes for the ZIP file

  7. #37
    Registered User
    Join Date
    Sep 2012
    Location
    Clemson, SC USA
    Posts
    19
    I'm gonna go out on a limb and say you're in Srimani's class at Clemson and it's due Thursday. What section are you in?

    You beat me at creating a thread.

  8. #38
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Quote Originally Posted by Adak View Post
    Midy is the name of the variable that holds height of the figure, divided by 2. It's the mid point in height (y) value.

    How are you displaying the graphic you're creating?

    You should draw out on paper, the diagram (large), and note for each color, what the slope should be, for that section:

    Example:
    Magenta pixels are all:
    y value > midy
    > slope of lineLR
    > slope of lineRL

    and repeat that for every different colored section. Now you can easily refer to it, when you're coding up your program.
    So would it be
    Magenta pixels are all:
    y value > midy
    y value > slope of lineLR
    y value > slope of lineRL

    Cyan pixels are all:
    y value > midy
    y value < slope of lineLR
    y value < slope of lineRL


    Red pixels are all:
    y value > midy
    y value < slope of lineLR
    y value > slope of lineRL

    Yellow pixels are all:
    y value < midy
    y value > slope of lineLR
    y value > slope of lineRL

    Then put this in a while loop?

    Also I am using ./a.out > out.ppm to view the image.

    Thanks for all your help.

  9. #39
    Registered User
    Join Date
    Oct 2012
    Posts
    8
    Here is my code can you tell me what s wrong with it please?
    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)
    {
      yValue=1;
      int midy= height/2;
      int rise_LR=1;
      int run_LR=1;
      int line_LR=(rise_LR/run_LR);
      int rise_RL=-1;
      int run_RL=1;
      int line_RL=(rise_RL/run_RL);
      int rise_x=height;
      int run_x=1;
      int x=(rise_x/run_x);
      while (yValue>midy)
      {
        if (x>line_LR && x>line_RL)
        {
          //Prints the color red in frist triangle
          make_pixel(255,0,0);
        }
        else if (x<line_LR && x<line_RL)
        {
          //Prints the color green in second trigangle
          make_pixel(0,255,0);
        }
        yValue++;
        x++;
      }
      for (yValue=height/2; yValue<midy; yValue++)
      {
        if (x<line_LR && x<line_RL)
        {
          //Prints the color blue in second triangle
          make_pixel(0,0,255);
        }
        else 
        {
          make_pixel(255,0,255);
        }
      }
    }
    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);
    }
    Last edited by meLearningC; 10-17-2012 at 10:03 AM. Reason: Changed the code, and made it cleaner.

  10. #40
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I broke drawing the figure into two steps:

    1) draw the top half, in a while loop, and 2) draw the bottom half, also in a while loop.

    Then, inside those while loops, were the while loops for each of the color zones.

    Code:
       //cp = current position. cpx=current position x value, etc.
       int cpx,cpy,red,cyan,magenta,yellow,midx,midy,r;
    
       int ht=26, wt=27;   //ht=height and wt=width of drawing
       
       midx=wt/2;  //midx=middle x
       midy=ht/2;   //midy=middle y midpoint is midx,midy
    
       cpx=cpy=r=cyan=magenta=yellow=0;
       red=wt-1;
       printf("height: %d   width: %d  midx: %d  midy: %d\n\n",ht, wt, midx, midy);
    
       //upper half of figure
       while(cpy<midy) {
          cpx=0;    
          while(cpx<=cyan && cpy<midy) {
             set color to CYAN
             printf("%c",CH); 
             ++cpx;
          }
          while(cpx>=cyan && cpx<red){
             set color to MAGENTA);
             printf("%c",CH);
             cpx++;
          }
          while(cpx < wt) {
             set color to RED
             printf("%c",CH);
             cpx++;
          }
          --red;     //red gets larger when decremented
          ++cyan;   //cyan gets larger "      incremented
          ++cpy;  
          printf("\n");
          
       }
       //lower half of figure
       yellow=0;
       ++cyan;   
       while(cpy<ht) {
          cpx=0;
          --cyan;
          ++red;
          ++yellow;
          while(cpx<cyan) {
             set color to BLUE; 
             printf("%c",CH);
             ++cpx;
          }
          while(cpx>=cyan && cpx<red){
             set color to YELLOW);
             printf("%c",CH);
             cpx++;
          }
          while(cpx < wt) {
             set color to RED;
             printf("%c",CH);
             cpx++;
          }
          ++cpy;
          _putch('\n');
              
       }
       printf("\n");
       return 0;
    }
    Your test in each while (test) portion will use the slope and etc., that we discussed previously. Which were more fuzzy than precise, on my part.

    I wasn't sure you were coming back, so I changed my program around a bit:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    #define BLUE    9
    #define CYAN    3
    #define RED     4
    #define YELLOW 14
    #define MAGENTA 5
    #define WHITE  15
    #define CH    254 //254-best, 219 for a full char
    
    int main(void) {
       int cpx,cpy,red,cyan,magenta,yellow,midx,midy,r;
       //cp = current position. cpx=current position x value, etc.
    
       int ht=26, wt=27;   //ht=height and wt=width of drawing
       
       midx=wt/2;  //midx=middle x
       midy=ht/2;   //midy=middle y midpoint is midx,midy
    
       cpx=cpy=r=cyan=magenta=yellow=0;
       red=wt-1;
       printf("height: %d   width: %d  midx: %d  midy: %d\n\n",ht, wt, midx, midy);
       //upper half of figure
       while(cpy<midy) {
          cpx=0;    
          _textcolor(WHITE);
          printf("%2d. ",r++);
          
          while(cpx<=cyan && cpy<midy) {
             _textcolor(RED); 
             printf("%c",CH);
             ++cpx;
          }
          while(cpx>=cyan && cpx<red){
             _textcolor(MAGENTA);
             printf("%c",CH);
             cpx++;
          }
          while(cpx < wt) {
             _textcolor(BLUE);
             printf("%c",CH);
             cpx++;
          }
          --red;
          ++cyan;
          ++cpy;
          _putch('\n');
          
       }
       //lower half of figure
       yellow=0;
       ++cyan;   
       while(cpy<ht) {
          cpx=0;
          --cyan;
          ++red;
          ++yellow;
          _textcolor(YELLOW);
          printf("%2d. ",r++);
          
          while(cpx<cyan) {
             _textcolor(BLUE); 
             printf("%c",CH);
             ++cpx;
          }
          //if(cpx==midx && cpy==midy) { _textcolor(WHITE); printf("%c",CH);cpx++;} //a middle dot
    
          while(cpx>=cyan && cpx<red){
             _textcolor(YELLOW);
             printf("%c",CH);
             cpx++;
          }
          //_getch();
          while(cpx < wt) {
             _textcolor(RED);
             printf("%c",CH);
             cpx++;
          }
          ++cpy;
          _putch('\n');
              
       }
       _textcolor(YELLOW);
       printf("\n");
       return 0;
    }
    Which gave me this little beauty:
    Attached Images Attached Images Make this image in c:-triangles1-png 

  11. #41
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm looking at your code.

    OK. On your first fprintf(), (line 71), you have output going to stdin. That should be stdout instead- and this was mentioned twice before in this thread.
    Last edited by Adak; 10-17-2012 at 10:31 AM.

  12. #42
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Made a couple changes, commented below, file is now written out, but IrvanView says the header is damaged.


    Code:
    void print_Row (int yValue, int width, int height)
    {
      yValue=1;
      int midy= height/2;
      int rise_LR=1;
      int run_LR=1;
      int line_LR=(rise_LR/run_LR);
      int rise_RL=-1;
      int run_RL=1;
      int line_RL=(rise_RL/run_RL);
      int rise_x=height;
      int run_x=width;     //not 1
      int x=(rise_x/run_x);
      while (yValue<midy)   //should be < instead of > ?
    In this function, why isn't yValue the value of i?
    Code:
    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);
        }
    }
    This is all that I have in out.ppm:
    Code:
    Enter the width and the height:
    So I need to change the output on these prompts. What do you have in your out.ppm file?
    Last edited by Adak; 10-17-2012 at 11:20 AM.

  13. #43
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, now I'm getting the header OK, and a very short green line.

  14. #44
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This draws the image below - so the top half of the image you had.

    I worked with the slope of the line idea, but rejected it ultimately. The big problem with this program, is the image becomes all distorted, if you have the height not equal to the width. The lines between the colors are only drawn at 45° angles.



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    FILE *fp;
    
    //This function makes the header file for the image
    void make_header(int width, int height)
    {
      fprintf(fp,"P6\n"); 
      fprintf(fp, "%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(fp, "%c%c%c", r, g, b);
    }
    /* sends data for the image, to make_pixel */
    void print_Row (double width, double height)
    {
       double midy= height/2.0;
       double x=0.0;
       double y=0.0;
       double cyan=0.0;
       double red=width-1;
       
       while (y<midy)   
       { 
          x=0;
          do {
             //make_pixel(0,0,0); //debug
             make_pixel(0,214,255); //Prints cyan in first triangle 
             ++x;
          }while(x < cyan); // && x>line_RL)  //cyan color
        
          do {
             //make_pixel(0,0,0); //debug
             make_pixel(255,0,255); //Prints magenta in first triangle
             ++x;
          }while(x < red); // && x<line_RL)
        
          do {  
             //make_pixel(0,0,0); //debug
             make_pixel(255,0,0); //Prints red in third triangle
             ++x;
          }while(x < width);
       
          cyan++;
          red--;
          y++;
          
       }
    
    }
    void make_image (int width, int height)
    {
       make_header(width, height);
       print_Row(width, height);
    }
    int main (void)
    {
       int width=600;
       int height=600;
       fp=fopen("out.ppm","wb");
       
       if(!fp) {
          printf("Error: file failed to open\n");
          return 0;
       }
       //printf("Enter the width and the height: ");
       //fscanf(stdin, "%d %d", &width, &height);
       make_image(width, height);
       fclose(fp);
       system("out.ppm");
       return 0;
    }
    Attached Images Attached Images Make this image in c:-triangles2-png 
    Last edited by Adak; 10-17-2012 at 05:17 PM.

  15. #45
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of incrementing the colors by 1 each time. Have a variables like this:
    double inc=width/height;

    and increment and decrement all the colors by that amount, instead of by 1. Increments and decrements to x and y are still the same(by one). Then any old rectangular shape will look perfect.

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, 07:53 PM
  2. Replies: 4
    Last Post: 03-13-2010, 05: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