how to get the (s,s1) in the arrow?using direct2d?

mc 4,436 Reputation points
2024-09-03T10:57:00.9+00:00

User's image

I draw a line and I know the two points one of them is (x,y) and then I want to draw arraw. and I can not get the axis of the (s,s1) because the arraw may have different directions.

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,693 questions
{count} votes

Accepted answer
  1. Castorix31 84,546 Reputation points
    2024-09-03T14:09:10.1633333+00:00

    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();
    	}
    }
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.