- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我刚刚开始学习 C++。我想要做的就是画一条线到指定的坐标,它作为方法的输入。我使用 MoveToEx
在每个循环中设置起点(在循环中使用不同的参数调用此函数)并给出我想要绘制车道的坐标。
关于如何使其循环工作的任何想法?
我的代码类似于:
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();
}
}
最佳答案
这是一些示例代码(我在 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;
}
关于c++ - WinApi LineTo 不刷新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42517628/
我试图根据点'数组中的坐标绘制线条,但当我调用此方法时,它在线条中显示以下错误。当我写例如console.log(points[1][1]) 它向我显示了该元素。有人能指出我在这里想念的东西吗? "U
你好我想用java画线,刷新率为60fps: import javafx.application.Application; import javafx.stage.Stage; import java
我有一个来自Inkscape,Illustrator或任何其他应用程序的SVG文件。我想将形状转换为lineto,moveto,curveto格式。 我想要的是这样的: ./Appname svgfi
我目前正在 Android Studio 上学习 Canvas,我不太确定使用正确的标题,但我一直在研究如何在使用 Seekbar 拖动红点时控制三角形位置。以下是详细信息 下面是我成功绘制三角形的代
我需要随机化形状的坐标。我知道我需要以某种方式介绍 Math.floor(Math.Random * num);在那里排队,但我所做的一切似乎都不起作用。 这是我要随机化的填充了坐标的 block 。
我刚刚开始学习 C++。我想要做的就是画一条线到指定的坐标,它作为方法的输入。我使用 MoveToEx 在每个循环中设置起点(在循环中使用不同的参数调用此函数)并给出我想要绘制车道的坐标。 关于如何使
有人可以帮助我了解 path.lineTo(x,y) 的实际工作原理吗?我正在尝试在手指画类型的应用程序中绘制一条简单的直线。这是我试过的一个例子: mPath.reset()
我正在尝试使用两个for循环绘制网格,一个用于绘制10条垂直线,另一个用于绘制10条水平线。像这样: for(var i=1;i 这确实很奇怪。我测试了你在这里写的所有内容,结果是一样的。现在我拼命打
我是 JavaScript 新手! 如何将变量分配给当前的 xy 坐标,以便我可以使用相对位置来绘制线条?尝试用键盘进行 eclipse 刻草图。上、下、左、右箭头键...使用 JS、CSS 和 HT
假设我有以下 JavaScript 代码: var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext(
function TrackGraphic(model, canvas) { //TrackModel this._model = model; this.draw = fun
这种情况不好解释,我用一张图来说明: 创建的第一个形状内的像素变亮。用黑色清除屏幕,绘制红色和绿色框,然后绘制路径。到目前为止,我发现的唯一修复方法是将框的线宽设置为 2 像素,原因已概述 here
有没有办法为 TCanvas.LineTo 方法设置 lineends 的样式?它似乎默认为圆角末端,当 Pen.Width 设置为较大值(例如 9)时,对于不同颜色的一行中的多条线来说,这看起来非常
我尝试使用 HTML5 Canvas 制作动画,在其中绘制 3 个片段的位置,每个片段用一个圆圈连接起来。 每个圆的位置由 (x1,y1)、(x2,y2) 和 (x3,y3) 确定。 这是我的代码片段
我正在一个单独的项目中,试图从炮塔到敌人画一条线。但是我希望这条线保持在炮塔边界之内,而不是一直延伸到敌人。我在下面的代码段中找到了一个较小的示例。 请彻底回答,因为我绝对不是专家。非常感谢!谢谢。
我一直在查看 Emanuele Feronato 的字符串避免器代码(下面的代码和链接)并尝试对其进行调整,以便当字符串与自身相交时 - 形成一个闭环 - 它只填充该区域 see image .我的数
我目前正在尝试在我的 Canvas speechbubble 元素下绘制一个箭头。 我的问题: 箭头的右侧似乎比左侧长得多。 我尝试了什么: 我尝试通过以下方式创建箭头: ctx.moveTo(x,
我创建了一个扩展 Shape 的类,以便为我的按钮背景绘制自定义形状(带切角的矩形)。我遇到的问题是画成对角线的线看起来比直线粗得多: 如何使边框始终保持一致的宽度?我尝试将抗锯齿设置为 true/f
主题说明了一切:您可以将负数传递给 HMTL5 Canvas 中的绘图基元吗? 例如,如果我转换为 (100,100),我可以使用坐标 (-25,-25) 绘制一个矩形或直线吗?使用 lineTo 进
我有一个简单的 Canvas 绘图应用程序。 有时 lineTo() 命令会生成一条坐标较少的线,并且绘图有很多边: 我正在使用最新的 firefox,是因为连接不好还是我的电脑很忙?有解决办法吗?这
我是一名优秀的程序员,十分优秀!