Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How should I set a pixels color?

How should I set a pixels color?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rbc
    wrote on last edited by
    #1

    Hi, I am trying to write a simple Mandelbrot set Visual C++ program. I assume that I am not handeling the setting the color of a pixel correctly. Here's some of my code ... ... void CMand_View::OnDraw(CDC* pDC) { ... for( int i=0; i<320; i++ ) { for( int j=0; j<200; j++ ) { int c = CMand_View::Mandel_calcColor( i, j ); pDC->SetPixel(i,j, PALETTEINDEX(c)); } } } ... double CManel_View::Mandel_Abs ( double real, double img ) { double rc = real * real + img * img; return sqrt( rc ); } int CMand_View::Mandel_calcColor( int X, int Y ) { int N; // WHILE-loop control variable double XPos, YPos; // The real and imaginary parts of C double RealPart; // The real part of Z double ImagPart; // The imaginary part of Z int AbsZ; // BOOLEAN; double Temp; double width = 320.0; double height = 200.0; N = 0; AbsZ = TRUE; RealPart = 0; ImagPart = 0; XPos = X / width * (XMax - XMin) + XMin; YPos = Y / height * (YMax - YMin) + YMin; while ( (N < Iterations) && (AbsZ == TRUE) ) { N++; Temp = (RealPart*RealPart) - (ImagPart*ImagPart) + XPos; ImagPart = 2.0 * ImagPart * RealPart + YPos; RealPart = Temp; AbsZ = (( CMand_View::Mandel_Abs (ImagPart, RealPart) <= Bailout) ? TRUE : FALSE); } //end while if (AbsZ == 0 ) { return (N + sColor) % Colors; } else { return 0; } } //end calcColor I think that this ... pDC->SetPixel(i,j, PALETTEINDEX(c)); ... is not right. :confused:

    C C R 3 Replies Last reply
    0
    • R rbc

      Hi, I am trying to write a simple Mandelbrot set Visual C++ program. I assume that I am not handeling the setting the color of a pixel correctly. Here's some of my code ... ... void CMand_View::OnDraw(CDC* pDC) { ... for( int i=0; i<320; i++ ) { for( int j=0; j<200; j++ ) { int c = CMand_View::Mandel_calcColor( i, j ); pDC->SetPixel(i,j, PALETTEINDEX(c)); } } } ... double CManel_View::Mandel_Abs ( double real, double img ) { double rc = real * real + img * img; return sqrt( rc ); } int CMand_View::Mandel_calcColor( int X, int Y ) { int N; // WHILE-loop control variable double XPos, YPos; // The real and imaginary parts of C double RealPart; // The real part of Z double ImagPart; // The imaginary part of Z int AbsZ; // BOOLEAN; double Temp; double width = 320.0; double height = 200.0; N = 0; AbsZ = TRUE; RealPart = 0; ImagPart = 0; XPos = X / width * (XMax - XMin) + XMin; YPos = Y / height * (YMax - YMin) + YMin; while ( (N < Iterations) && (AbsZ == TRUE) ) { N++; Temp = (RealPart*RealPart) - (ImagPart*ImagPart) + XPos; ImagPart = 2.0 * ImagPart * RealPart + YPos; RealPart = Temp; AbsZ = (( CMand_View::Mandel_Abs (ImagPart, RealPart) <= Bailout) ? TRUE : FALSE); } //end while if (AbsZ == 0 ) { return (N + sColor) % Colors; } else { return 0; } } //end calcColor I think that this ... pDC->SetPixel(i,j, PALETTEINDEX(c)); ... is not right. :confused:

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Are you using 256 colours ? Try it in a higher res. The last paremeter as an RGB value ( as returned by the RGB macro ) will work. It will be slow though. A DIBSection would give you a pixel array to set directly instead. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
      C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
      Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

      R 1 Reply Last reply
      0
      • R rbc

        Hi, I am trying to write a simple Mandelbrot set Visual C++ program. I assume that I am not handeling the setting the color of a pixel correctly. Here's some of my code ... ... void CMand_View::OnDraw(CDC* pDC) { ... for( int i=0; i<320; i++ ) { for( int j=0; j<200; j++ ) { int c = CMand_View::Mandel_calcColor( i, j ); pDC->SetPixel(i,j, PALETTEINDEX(c)); } } } ... double CManel_View::Mandel_Abs ( double real, double img ) { double rc = real * real + img * img; return sqrt( rc ); } int CMand_View::Mandel_calcColor( int X, int Y ) { int N; // WHILE-loop control variable double XPos, YPos; // The real and imaginary parts of C double RealPart; // The real part of Z double ImagPart; // The imaginary part of Z int AbsZ; // BOOLEAN; double Temp; double width = 320.0; double height = 200.0; N = 0; AbsZ = TRUE; RealPart = 0; ImagPart = 0; XPos = X / width * (XMax - XMin) + XMin; YPos = Y / height * (YMax - YMin) + YMin; while ( (N < Iterations) && (AbsZ == TRUE) ) { N++; Temp = (RealPart*RealPart) - (ImagPart*ImagPart) + XPos; ImagPart = 2.0 * ImagPart * RealPart + YPos; RealPart = Temp; AbsZ = (( CMand_View::Mandel_Abs (ImagPart, RealPart) <= Bailout) ? TRUE : FALSE); } //end while if (AbsZ == 0 ) { return (N + sColor) % Colors; } else { return 0; } } //end calcColor I think that this ... pDC->SetPixel(i,j, PALETTEINDEX(c)); ... is not right. :confused:

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        are you using a palette ? i don't see one. -c


        Please stand by

        ThumbNailer

        R 1 Reply Last reply
        0
        • C Chris Losinger

          are you using a palette ? i don't see one. -c


          Please stand by

          ThumbNailer

          R Offline
          R Offline
          rbc
          wrote on last edited by
          #4

          That's a good question! How do I use a palette? I thought just using the marco ... PALETTEINDEX(c)); ... would handle all that for me :laugh: Something is going on as my program shows the mandelbrot set all in green! :wtf:

          C 1 Reply Last reply
          0
          • R rbc

            That's a good question! How do I use a palette? I thought just using the marco ... PALETTEINDEX(c)); ... would handle all that for me :laugh: Something is going on as my program shows the mandelbrot set all in green! :wtf:

            C Offline
            C Offline
            Chris Losinger
            wrote on last edited by
            #5

            the simplest way is to just come up with an array of COLORREF values: COLORREF pal[MAXITERATIONS]; pal[0] = RGB(0,0,0); pal[1] = RGB(64,64,0); pal[2] = RGB(128,128,0); ...etc then, in your loop, just use "pal[c]" instead of that PALETTEINDEX macro. an 8-bit display is pretty rare these days. you might as well just aim for 24-bit color. -c


            Please stand by

            ThumbNailer

            R 1 Reply Last reply
            0
            • C Christian Graus

              Are you using 256 colours ? Try it in a higher res. The last paremeter as an RGB value ( as returned by the RGB macro ) will work. It will be slow though. A DIBSection would give you a pixel array to set directly instead. Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
              C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
              Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

              R Offline
              R Offline
              rbc
              wrote on last edited by
              #6

              Hi Christian, Yes I want to use 256 colors. How could I use the RGB macro when int CMand_View::Mandel_calcColor( int X, int Y ) returns an int and not 3 ints? Should I some how derieve the 3 ints (r, g, b) from c? Where c = CMand_View::Mandel_calcColor( X, Y ); :)

              C 1 Reply Last reply
              0
              • C Chris Losinger

                the simplest way is to just come up with an array of COLORREF values: COLORREF pal[MAXITERATIONS]; pal[0] = RGB(0,0,0); pal[1] = RGB(64,64,0); pal[2] = RGB(128,128,0); ...etc then, in your loop, just use "pal[c]" instead of that PALETTEINDEX macro. an 8-bit display is pretty rare these days. you might as well just aim for 24-bit color. -c


                Please stand by

                ThumbNailer

                R Offline
                R Offline
                rbc
                wrote on last edited by
                #7

                How should I set pal[3] through pal[255]? Am I using a 8-bit display? How do I use a 24-bit color?

                C 1 Reply Last reply
                0
                • R rbc

                  How should I set pal[3] through pal[255]? Am I using a 8-bit display? How do I use a 24-bit color?

                  C Offline
                  C Offline
                  Chris Losinger
                  wrote on last edited by
                  #8

                  rbc wrote: How should I set pal[3] through pal[255]? pick a set of colors that looks good. in my opinion: 1. fractals work best when the palette fades between two or three colors and 2. generating the palette is as important as picking the right region to render: http://www.smalleranimals.com/thumbfrax/thumbs/_index.htm[^] rbc wrote: Am I using a 8-bit display? i don't know. rbc wrote: How do I use a 24-bit color? don't do anything. just render the pixels by using their COLORREF values. -c


                  Please stand by

                  ThumbNailer

                  R 1 Reply Last reply
                  0
                  • C Chris Losinger

                    rbc wrote: How should I set pal[3] through pal[255]? pick a set of colors that looks good. in my opinion: 1. fractals work best when the palette fades between two or three colors and 2. generating the palette is as important as picking the right region to render: http://www.smalleranimals.com/thumbfrax/thumbs/_index.htm[^] rbc wrote: Am I using a 8-bit display? i don't know. rbc wrote: How do I use a 24-bit color? don't do anything. just render the pixels by using their COLORREF values. -c


                    Please stand by

                    ThumbNailer

                    R Offline
                    R Offline
                    rbc
                    wrote on last edited by
                    #9

                    Cool web page. You are obviously no stranger to generating fractals! Yet I am. How would you write a mandelbrot program in Visual C++? :)

                    C 1 Reply Last reply
                    0
                    • R rbc

                      Cool web page. You are obviously no stranger to generating fractals! Yet I am. How would you write a mandelbrot program in Visual C++? :)

                      C Offline
                      C Offline
                      Chris Losinger
                      wrote on last edited by
                      #10

                      rbc wrote: How would you write a mandelbrot program in Visual C++? i would start by doing exactly what you're doing just to get a feel for how things work. then i would add controls to move around (zoom in/out, pan, etc.). then i would add palette creation UI (no idea what i'd do for that). then i'd add a bunch of other fractal types. a tiny mandelbrot program[^]

                      #include "stdio.h"
                      main()
                      {
                      int b=0,c=0,q=60,_=q;for(float i=-20,o,O=0,l=0,j,p;j=O*O,p=l*l,
                      (!_--|(j+p>4)?fputc(b?q+(_/3):10,(i+=!b,p=j=O=l=0,c++,stdout)),
                      _=q:l=2*O*l+i/20,O=j-p+o),b=c%q,c<2400;o=-2+b*.05);
                      }

                      -c


                      Please stand by

                      ThumbNailer

                      1 Reply Last reply
                      0
                      • R rbc

                        Hi Christian, Yes I want to use 256 colors. How could I use the RGB macro when int CMand_View::Mandel_calcColor( int X, int Y ) returns an int and not 3 ints? Should I some how derieve the 3 ints (r, g, b) from c? Where c = CMand_View::Mandel_calcColor( X, Y ); :)

                        C Offline
                        C Offline
                        Christian Graus
                        wrote on last edited by
                        #11

                        It looks like you're header for 24 bit colour and Chris has sorted it all out for you. Good luck !!! Christian No offense, but I don't really want to encourage the creation of another VB developer. - Larry Antram 22 Oct 2002
                        C# will attract all comers, where VB is for IT Journalists and managers - Michael P Butler 05-12-2002
                        Again, you can screw up a C/C++ program just as easily as a VB program. OK, maybe not as easily, but it's certainly doable. - Jamie Nordmeyer - 15-Nov-2002

                        1 Reply Last reply
                        0
                        • R rbc

                          Hi, I am trying to write a simple Mandelbrot set Visual C++ program. I assume that I am not handeling the setting the color of a pixel correctly. Here's some of my code ... ... void CMand_View::OnDraw(CDC* pDC) { ... for( int i=0; i<320; i++ ) { for( int j=0; j<200; j++ ) { int c = CMand_View::Mandel_calcColor( i, j ); pDC->SetPixel(i,j, PALETTEINDEX(c)); } } } ... double CManel_View::Mandel_Abs ( double real, double img ) { double rc = real * real + img * img; return sqrt( rc ); } int CMand_View::Mandel_calcColor( int X, int Y ) { int N; // WHILE-loop control variable double XPos, YPos; // The real and imaginary parts of C double RealPart; // The real part of Z double ImagPart; // The imaginary part of Z int AbsZ; // BOOLEAN; double Temp; double width = 320.0; double height = 200.0; N = 0; AbsZ = TRUE; RealPart = 0; ImagPart = 0; XPos = X / width * (XMax - XMin) + XMin; YPos = Y / height * (YMax - YMin) + YMin; while ( (N < Iterations) && (AbsZ == TRUE) ) { N++; Temp = (RealPart*RealPart) - (ImagPart*ImagPart) + XPos; ImagPart = 2.0 * ImagPart * RealPart + YPos; RealPart = Temp; AbsZ = (( CMand_View::Mandel_Abs (ImagPart, RealPart) <= Bailout) ? TRUE : FALSE); } //end while if (AbsZ == 0 ) { return (N + sColor) % Colors; } else { return 0; } } //end calcColor I think that this ... pDC->SetPixel(i,j, PALETTEINDEX(c)); ... is not right. :confused:

                          R Offline
                          R Offline
                          rbc
                          wrote on last edited by
                          #12

                          :wtf:

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups