我刚刚开始学习C ++。我想要做的就是在指定的坐标上绘制一条线,它作为方法的输入。我使用MoveToEx
设置每个循环的起始点(使用不同的参数在循环中调用此函数)并给出我想要绘制lane的坐标。
如何让它在循环中工作?
我的代码类似于:
void Clock::drawSecondLine(float x,float y) {
HWND console_handle = GetConsoleWindow();
HDC device_context = GetDC(console_handle);
HPEN pen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0));
SelectObject(device_context, pen);
MoveToEx(device_context, 0, 0, NULL);
Ellipse(device_context, 400, 0, 0, 400);
MoveToEx(device_context, 200, 200, NULL);
LineTo(device_context, (int)x, (int)y);
ReleaseDC(console_handle, device_context);
cin.ignore();
}
循环:
void Zegar::startClock() {
while (true) {
drawSecondLine(laneShowingSecond.getX(), laneShowingSecond.getY());
laneShowingSecond.movePointByRadius(RADIUS_PER_SECOND);
Sleep(1000);
increaseSecond();
}
}
答案 0 :(得分:1)
这是一些示例代码(我在 VStudio 2k10 中运行)。
备注强>:
iostream
- 这意味着它不会编译为 C ),它仍然是旧的 C 。RECT_*
定义(400,0,0,400))我提取中心坐标和 X 和 Y 轴,使用一些简单的数学计算(由于矩形是正方形,2个半径相等,所以我们点击椭圆实际上是圆的特殊情况强>)。nextPoint
函数替代了代码中的laneShowingSecond.getX(), laneShowingSecond.getY()
。init
函数中。请注意,如果在初始化期间出现问题,则会以错误(< 0 )代码退出,因为它无法绘制。cleanup
函数中(这里我没有费心去检查返回代码,因为它还在退出)。draw
函数包含绘图循环。在每次迭代中:
INCERMENT_DEG
(默认情况下为30°)。ITERATION_SLEEP_TIME
毫秒(我将其设置为200以避免每行绘制一秒钟)。的main.cpp :
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <Windows.h>
#define RECT_LEFT 400 // Modify any of these 4 RECT_* values to get different ellipse shapes.
#define RECT_TOP 0
#define RECT_RIGHT 0
#define RECT_BOT 400
#define ITERATION_SLEEP_TIME 200 // Sleep time while in loop.
#define INCERMENT_DEG 30 // 30 degrees per step; a full circle has 360 (2 * PI RAD).
#define M_PI_180 M_PI / 180
using std::cout;
using std::endl;
typedef enum {DRAW_RADII, DRAW_POLY} DrawMethod;
const int radiusX = abs(RECT_RIGHT - RECT_LEFT) / 2;
const int radiusY = abs(RECT_BOT - RECT_TOP) / 2;
const int centerX = (RECT_RIGHT + RECT_LEFT) / 2;
const int centerY = (RECT_BOT + RECT_TOP) / 2;
HWND hwnd = NULL;
HDC hdc = NULL;
HPEN hpen = NULL;
DrawMethod meth = DRAW_RADII; // Modify this to DRAW_POLY to draw a polygon instead of the "bike wheel".
int deg = 0;
double x = 0, y = 0;
void nextPoint(int degree, double *x, double *y) {
*x = centerX + radiusX * cos(M_PI_180 * degree );
*y = centerY - radiusY * sin(M_PI_180 * degree);
}
int init() {
if ((hwnd = GetConsoleWindow()) == NULL) {
cout << "GetConsoleWindow error: " << GetLastError() << endl;
return -1;
}
if ((hdc = GetDC(hwnd)) == NULL) {
cout << "GetDC error: " << GetLastError() << endl;
return -2;
}
if ((hpen = CreatePen(PS_SOLID, 3, RGB(255, 0, 0))) == NULL) {
cout << "CreatePen error: " << GetLastError() << endl;
return -3;
}
SelectObject(hdc, hpen);
Ellipse(hdc, RECT_LEFT, RECT_TOP, RECT_RIGHT, RECT_BOT);
nextPoint(deg, &x, &y);
if (meth == DRAW_RADII) {
MoveToEx(hdc, centerX, centerY, NULL);
LineTo(hdc, (int)x, (int)y);
} else if (meth == DRAW_POLY) {
MoveToEx(hdc, (int)x, (int)y, NULL);
}
return 0;
}
void draw() {
while (deg < 360) {
deg += INCERMENT_DEG;
nextPoint(deg, &x, &y);
if (meth == DRAW_RADII) {
MoveToEx(hdc, centerX, centerY, NULL);
LineTo(hdc, (int)x, (int)y);
} else if (meth == DRAW_POLY) {
LineTo(hdc, (int)x, (int)y);
} else
break;
Sleep(ITERATION_SLEEP_TIME);
}
}
void cleanup() {
if (hpen) {
DeleteObject(hpen);
}
if (hwnd && hdc) {
ReleaseDC(hwnd, hdc);
}
}
int main() {
if (!init())
draw();
cleanup();
return 0;
}