21xrx.com
2025-03-18 07:21:43 Tuesday
文章检索 我的文章 写文章
C++如何绘制坐标轴?
2023-06-25 20:04:30 深夜i     17     0
C++ 绘制 坐标轴 图形库 函数绘制

C++是一种面向对象编程语言,广泛应用于计算机科学领域。当我们需要绘制一张图表时,往往需要用到坐标轴。那么,C++中如何绘制坐标轴呢?

首先,我们需要利用C++的图形库绘制一个平面。例如,我们可以用WinAPI来绘制一个窗口,并在窗口中绘制一个矩形作为平面。具体的代码可以如下:

#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASS wc = {};
  wc.lpfnWndProc = WindowProc;
  wc.hInstance = hInstance;
  wc.lpszClassName = L"MyClass";
  RegisterClass(&wc);
  // create the window
  HWND hwnd = CreateWindowEx(0L, L"MyClass", L"My Window", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
  ShowWindow(hwnd, nCmdShow);
  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
  case WM_PAINT:
    {
      PAINTSTRUCT ps;
      HDC hdc = BeginPaint(hwnd, &ps);
      RECT rc;
      GetClientRect(hwnd, &rc);
      FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW + 1));
      EndPaint(hwnd, &ps);
    }
    return 0;
  }
  return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

在这段代码中,我们创建了一个窗口,并在该窗口中使用WinAPI的函数来绘制一个矩形。

接下来,我们需要绘制坐标轴。我们可以手动计算平面上的点的位置,并在这些点上绘制直线来形成坐标轴。如下是绘制x轴和y轴的示例代码。

//画直线函数
void DrawLine(HDC hdc, int x1, int y1, int x2, int y2)
{
  MoveToEx(hdc, x1, y1, NULL);
  LineTo(hdc, x2, y2);
}
//绘制坐标轴
int xLeft = 50, yTop = 50, xRight = 350, yBottom = 350;//坐标轴区域
int x_ = 50, y_ = 300, x_width = 300, y_width = 250;//坐标轴长度
void DrawAxis(HDC hdc)
{
  //设置画笔颜色及线宽
  HPEN pen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
  SelectObject(hdc, pen);
  //画x轴
  DrawLine(hdc, x_, y_, x_ + x_width, y_);
  //画y轴
  DrawLine(hdc, x_, y_, x_, y_ - y_width);
  //释放画笔资源
  DeleteObject(pen);
}

这段代码中,我们定义了绘制坐标轴的区域和长度,并在这个区域内绘制了x轴和y轴。

最后,我们把绘制图表的代码整合到一起,就可以得到一张带有坐标轴的图表了。完整代码如下:

#include <windows.h>
//画直线函数
void DrawLine(HDC hdc, int x1, int y1, int x2, int y2)
{
  MoveToEx(hdc, x1, y1, NULL);
  LineTo(hdc, x2, y2);
}
//绘制坐标轴
int xLeft = 50, yTop = 50, xRight = 350, yBottom = 350;//坐标轴区域
int x_ = 50, y_ = 300, x_width = 300, y_width = 250;//坐标轴长度
void DrawAxis(HDC hdc)
{
  //设置画笔颜色及线宽
  HPEN pen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
  SelectObject(hdc, pen);
  //画x轴
  DrawLine(hdc, x_, y_, x_ + x_width, y_);
  //画y轴
  DrawLine(hdc, x_, y_, x_, y_ - y_width);
  //释放画笔资源
  DeleteObject(pen);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg)
  {
  case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
  case WM_PAINT:
    {
      PAINTSTRUCT ps;
      HDC hdc = BeginPaint(hwnd, &ps);
      RECT rc;
      GetClientRect(hwnd, &rc);
      FillRect(hdc, &rc, (HBRUSH)(COLOR_WINDOW + 1));
      DrawAxis(hdc);
      EndPaint(hwnd, &ps);
    }
    return 0;
  }
  return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASS wc = {};
  wc.lpfnWndProc = WindowProc;
  wc.hInstance = hInstance;
  wc.lpszClassName = L"MyClass";
  RegisterClass(&wc);
  // create the window
  HWND hwnd = CreateWindowEx(0L, L"MyClass", L"My Window", WS_OVERLAPPEDWINDOW, 100, 100, 400, 400, NULL, NULL, hInstance, NULL);
  ShowWindow(hwnd, nCmdShow);
  MSG msg;
  while (GetMessage(&msg, NULL, 0, 0))
  {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }
  return msg.wParam;
}

通过这样的绘制,我们就可以在C++中实现绘制坐标轴了。当然,这只是一个简单的示例,如需更加复杂的图表,还需要用到更多的图形绘制功能。

  
  
下一篇: C++上传CPP文件

评论区