With the help of ChatGPT, this works for me :
// Helper function to subtract two D2D1_POINT_2F points
D2D1_POINT_2F SubtractPoints(D2D1_POINT_2F p1, D2D1_POINT_2F p2)
{
return D2D1::Point2F(p1.x - p2.x, p1.y - p2.y);
}
// Helper function to add two D2D1_POINT_2F points
D2D1_POINT_2F AddPoints(D2D1_POINT_2F p1, D2D1_POINT_2F p2)
{
return D2D1::Point2F(p1.x + p2.x, p1.y + p2.y);
}
void DrawLineWithArrow(ID2D1DeviceContext* pDeviceContext, ID2D1Brush* pBrush, D2D1_POINT_2F startPoint,
D2D1_POINT_2F endPoint, float arrowSize)
{
// Draw the line
pDeviceContext->DrawLine(startPoint, endPoint, pBrush);
// Calculate the direction vector and normalize it
D2D1_POINT_2F direction = D2D1::Point2F(endPoint.x - startPoint.x, endPoint.y - startPoint.y);
float length = sqrtf(direction.x * direction.x + direction.y * direction.y);
direction.x /= length;
direction.y /= length;
// Calculate the points of the arrowhead
D2D1_POINT_2F arrowBase1 = D2D1::Point2F(
direction.x * arrowSize + direction.y * arrowSize,
direction.y * arrowSize - direction.x * arrowSize
);
D2D1_POINT_2F arrowBase2 = D2D1::Point2F(
direction.x * arrowSize - direction.y * arrowSize,
direction.y * arrowSize + direction.x * arrowSize
);
D2D1_POINT_2F left = SubtractPoints(endPoint, arrowBase1);
D2D1_POINT_2F right = SubtractPoints(endPoint, arrowBase2);
// Retrieve the factory from the device context
ID2D1Factory* pFactory = nullptr;
pDeviceContext->GetFactory(&pFactory);
// Create a path geometry for the arrowhead
ID2D1PathGeometry* pArrowGeometry = nullptr;
HRESULT hr = pFactory->CreatePathGeometry(&pArrowGeometry);
if (SUCCEEDED(hr))
{
ID2D1GeometrySink* pSink = nullptr;
hr = pArrowGeometry->Open(&pSink);
if (SUCCEEDED(hr))
{
pSink->BeginFigure(endPoint, D2D1_FIGURE_BEGIN_FILLED);
pSink->AddLine(left);
pSink->AddLine(right);
pSink->EndFigure(D2D1_FIGURE_END_CLOSED);
pSink->Close();
pSink->Release();
}
}
// Draw the arrowhead
if (pArrowGeometry)
{
pDeviceContext->FillGeometry(pArrowGeometry, pBrush);
pArrowGeometry->Release();
}
if (pFactory)
{
pFactory->Release();
}
}