Hello members,

I am a new member here, i would like to draw a parabol graphs: y=x^2 in C++.
But i don't know how to write it, anyone can help me?Thank you so much.

Dani AI

Generated

As asked for the environment and noted Visual C++, the simplest workable route is a Win32 windowed project that draws the curve in WM_PAINT. The basic idea: sample x across a chosen range, compute y = xx, map mathematical (x,y) to client pixels (screenX = centerX + xscale, screenY = centerY - y*scale), and draw a polyline or pixels. The snippet below is intended to be dropped into a Win32 WndProc WM_PAINT handler; include <vector> in the source.

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hWnd, &ps);

    RECT rc; GetClientRect(hWnd, &rc);
    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;
    int cx = width / 2;                    // origin in center
    int cy = height / 2;

    double scale = 20.0;                   // pixels per unit (adjust)
    double xmin = -10.0, xmax = 10.0;
    int steps = width;                     // one sample per pixel

    std::vector<POINT> pts;
    pts.reserve(steps + 1);
    for (int i = 0; i <= steps; ++i) {
        double t = xmin + (xmax - xmin) * i / double(steps);
        double y = t * t;                   // y = x^2
        int sx = cx + int(t * scale + 0.5);
        int sy = cy - int(y * scale + 0.5); // invert Y for screen coords
        POINT p; p.x = sx; p.y = sy;
        pts.push_back(p);
    }

    Polyline(hdc, pts.data(), (int)pts.size());
    EndPaint(hWnd, &ps);
}
break;

Troubleshooting and tips: pick scale and xmin/xmax to keep y within the client area (x^2 grows fast). If the curve is missing or clipped, reduce scale or narrow the x range. For smoother, anti-aliased output use GDI+ (Graphics::DrawCurve with SmoothingModeAntiAlias) or draw to a larger bitmap and downsample. For learning or quick tests, an ASCII-console plot can illustrate mapping logic before using GDI. This snippet assumes a standard Win32 project compiled with Visual C++ and demonstrates the coordinate mapping and drawing workflow needed to render y = x^2.

Recommended Answers

All 2 Replies

Standard C++ doesn't know about screens or graphics or pixels.
You need to say more about your setup (OS, Compiler etc)

Start by practising simple stuff, like say just drawing a line.

Thank you so much dear Salem, but i have just learnt Visual C++.Could you help me solve my problem.Thank you so much...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.