Построить графика функции cos(x), используя всю плоскость окна консоли — C++(Си)

#include <windows.h>
#include <stdio.h>
#include <math.h>
 
#define RED RGB(255,0,0)
#define GRN RGB(0,255,0)
#define BLU RGB(0,0,255)
 
 
#define BLK RGB(0,0,0)
#define WHT RGB(255,255,255)
 
HPEN  getPen(int iPEN_STYLE, int iPEN_SIZE, int iCOLORREF);
BOOL  SetPoint(HDC hDC, HPEN hPen, COORD PNT);
BOOL  PlotLine(HDC hDC, HPEN hPen, COORD BGN, COORD END);
//Функция график которой строим
double f(double x){return cos(x);}
double * mf(double a, double b, double n)
{
        double x = a;
        double h = (b - a)/n;
        double vec[2];
        vec[0] = f(a);
        vec[1] = f(a);
        while(x < b)
        {
                if(vec[1] < f(x))
                        vec[1] = f(x);
                if(f(x) < vec[0])
                        vec[0] = f(x);
                x += h;
        }
        return &vec[0];
}
 
int main()
{
        long n;
        double a = 0, b = 0,x, dx, *vec;
        double CX, CY;
 
        HWND    hWnd = GetForegroundWindow();
        HPEN    RPEN = getPen(PS_SOLID, 2, RED);
        HPEN    GPEN = getPen(PS_SOLID, 2, GRN);
        HPEN    BPEN = getPen(PS_SOLID, 2, BLU);
    HPEN    WPEN = getPen(PS_SOLID, 3, WHT);
        RECT    pRECT= {0};
    COORD   BGN  = {0};
        COORD   END  = {0};
    GetClientRect(hWnd,&pRECT);
 
        HDC hDC = GetWindowDC(hWnd);
 
        printf("\tPloting f(x) = ln(x)\r\n");
        printf("Enter diapasone [a;b]\r\n");
        printf("a = ");scanf("%lf",&a);
        printf("b = ");scanf("%lf",&b);
        printf("Enter number of points\r\n");
        printf("n = ");scanf("%u",&n);
        vec = mf(a, b, n);//Нужно знать макс и мин для масштабирования
    if(hDC)
    {
                CX = (b - a)/(pRECT.right - pRECT.left);
                CY = (vec[1] - vec[0])/(pRECT.bottom - pRECT.top);
 
                x = a;dx = (b - a)/n;
                BGN.X = x   /CX;
                BGN.Y = f(x)/CY + (pRECT.bottom - pRECT.top)/2;
                SetBkMode(hDC,TRANSPARENT);
        SetPoint (hDC, WPEN, BGN);
                while(x < b)
                {
                        END.X = x   /CX;
                        END.Y = f(x)/CY + (pRECT.bottom - pRECT.top)/2;
                        PlotLine(hDC, RPEN, BGN, END);
                        SetPoint (hDC,WPEN, END);
                        BGN = END;
                        x += dx;
                }
        }
        system("pause");
        return 0;
}
 
HPEN getPen(int iPEN_STYLE, int iPEN_SIZE, int iCOLORREF)
{
        return CreatePen(iPEN_STYLE, iPEN_SIZE, iCOLORREF);
}
 
BOOL SetPoint(HDC hDC, HPEN hPen, COORD PNT)
{
        EXTLOGPEN pINFO;
        GetObject(hPen,sizeof(EXTLOGPEN),&pINFO);
        SelectObject(hDC,hPen);
        return Ellipse
        (
                hDC,
                PNT.X - pINFO.elpWidth,
                PNT.Y + pINFO.elpWidth, 
                PNT.X + pINFO.elpWidth,
                PNT.Y - pINFO.elpWidth
        );
}
 
BOOL PlotLine(HDC hDC, HPEN hPen, COORD BGN, COORD END)
{
        SelectObject(hDC,hPen);
        MoveToEx(hDC,BGN.X,BGN.Y,NULL);
        return LineTo(  hDC,END.X,END.Y);
}

Leave a Comment