How do I change the color pixel in C language? [closed]

0

I know that nowadays there are easier ways to draw on the screen, but I want for a minute not to abstract myself so much and understand what would be the most basic way of drawing on the screen considering that I am working on C.

I thank you for your patience. Greetings.

    
asked by MZIPI 26.02.2018 в 22:25
source

1 answer

2

I vary a lot depending on the operating system, because of your label it is clear that you ask questions for windows.

First of all you change a pixel in a context, be it the console or a widget etc, that's how openGL works, for example, which is a rendering library. A very similar question was asked and the answer that was given serves as an answer to your question link

Use SetPinxel in the header windows.h I encourage you to take a look.

EDITED 1

I add a small explanation of the sample code shown in the link and add a small modification because the code draws the cosine function and you only need one pixel.

#include<windows.h>   

int main() 
{
    //Obtiene el identificador de la consola
    HWND myconsole = GetConsoleWindow();
    //Obtiene el identificador del dispositivo del contexto
    //En este caso es la consola
    HDC mydc = GetDC(myconsole);

    //Elige el color
    COLORREF COLOR= RGB(255,255,255); 

    //Son las coordenadas del pixel a modificar
    int crdX = 5, crdY = 10;


    //Dibuja pixel
    SetPixel(mydc,crdX, crdY, COLOR);

    ReleaseDC(myconsole, mydc);
    return 0;
}

As I mentioned, the draws is in a context, the context can be any that is compatible, in this case it is the console, and it is passed to SetPixel, it has as prototype the following form:

It is quoted as it is in the Microsoft documentation, it was only translated by me.

  

This function sets the pixel to the coordinates specified for the specified color.

     

COLORREF SetPixel (    HDC hdc,    int X,    int Y,    COLORREF crColor   );

     

Parameters

     

hdc      [in] Device context handler.

     

X      [in] Specify the X coordinate, in logical units, of the point to be established

     

Y      [in] Specify the Y coordinate, in logical units, of the point to be established

     

crColor      [in] Specifies the color that will be used to paint the point.

     

Return Values

     
  • The RGB value that the function sets to the pixel indicates success.

  •   
  • This value may differ with the color specified by crColor; this   It happens when you can not find a match for the color   specified.

  •   
  • -1 indicates failure.

  •   

    To get extended error information, call GetLastError .

         

    Remarks

         

    The function fails if the pixel coordinates are outside the current crop region.

         

    Not all devices with SetPixel support. For more information, see GetDeviceCaps

        
    answered by 27.02.2018 / 06:09
    source