有人可以帮我提一些关于如何在末尾画一条带箭头的线条的想法(最好带代码)吗?
箭头必须与线的方向正确对齐。我想用C ++做这件事。
答案 0 :(得分:2)
需要一点几何来确定形成箭头的三角形的角落。
假设线从P0到P1,我们希望箭头的尖端在P1。要找到箭头的“后角”,我们希望沿着线从尖端向后移动,然后左右转动以使箭头有一些宽度。
如果线与x轴或y轴对齐,这将很简单。为了处理任何角度的线,我们可以构造一个坐标系,其轴与原始线平行并垂直。我们将这些轴称为u和v,u指向线的方向,v指向它的垂直方向。
现在我们可以从P0开始,然后通过踩踏u和v确定的方向移动到角落,按照我们想要的任何箭头长度和宽度进行缩放。
在代码中:
constexpr int Round(float x) { return static_cast<int>(x + 0.5f); }
// Draws a line from p0 to p1 with an arrowhead at p1. Arrowhead is outlined
// with the current pen and filled with the current brush.
void DrawArrow(HDC hdc, POINT p0, POINT p1, int head_length, int head_width) {
::MoveToEx(hdc, p0.x, p0.y, nullptr);
::LineTo(hdc, p1.x, p1.y);
const float dx = static_cast<float>(p1.x - p0.x);
const float dy = static_cast<float>(p1.y - p0.y);
const auto length = std::sqrt(dx*dx + dy*dy);
if (head_length < 1 || length < head_length) return;
// ux,uy is a unit vector parallel to the line.
const auto ux = dx / length;
const auto uy = dy / length;
// vx,vy is a unit vector perpendicular to ux,uy
const auto vx = -uy;
const auto vy = ux;
const auto half_width = 0.5f * head_width;
const POINT arrow[3] =
{ p1,
POINT{ Round(p1.x - head_length*ux + half_width*vx),
Round(p1.y - head_length*uy + half_width*vy) },
POINT{ Round(p1.x - head_length*ux - half_width*vx),
Round(p1.y - head_length*uy - half_width*vy) }
};
::Polygon(hdc, arrow, 3);
}
和一个演示(使用WTL):
LRESULT OnPaint(UINT, WPARAM, LPARAM, BOOL &) {
PAINTSTRUCT ps;
BeginPaint(&ps);
RECT rc;
GetClientRect(&rc);
const auto origin = POINT{rc.left + (rc.right - rc.left)/2,
rc.top + (rc.bottom - rc.top)/2 };
const auto pi = 3.1415926f;
const auto tau = 2.0f*pi;
const auto cxInch = ::GetDeviceCaps(ps.hdc, LOGPIXELSX);
const auto radius = 2.0f * cxInch;
const auto size = Round(0.333f * cxInch);
for (float theta = 0.0f; theta < tau; theta += tau/12.0f) {
const auto p1 =
POINT{Round(origin.x + radius * std::cos(theta)),
Round(origin.y + radius * std::sin(theta))};
DrawArrow(ps.hdc, origin, p1, size, size/3);
}
EndPaint(&ps);
return 0;
}