SHARE
Facebook X Pinterest WhatsApp

Drawing an ArrowLine

Website : www.mayankmalik.cjb.net Environment: VC 5-6 , Windows 95/98/2000/NT/Me Recently I had to develop a Graphics Software and wanted to add support for drawing an Arrow Line / Measuring Line. This was essential for the software and I had no option but to go over my trigonometry fundas. After churning for a couple of hours […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Sep 28, 2001
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Website : www.mayankmalik.cjb.net

Environment: VC 5-6 , Windows 95/98/2000/NT/Me

Recently I had to develop a Graphics Software and wanted to add support for drawing an Arrow Line / Measuring Line. This was essential for the software and I had no option but to go over my trigonometry fundas. After churning for a couple of hours , I came up with this technique for drawing arrowheads at the end of a line.

The Lines are drawn when you drag the mouse . The drawing mode used is R2_NOT .As long as the user is dragging the mouse , the Line is refreshing itself (by redrawing) . You need to include "math.h" in your View.cpp file .

This checks when the User Starts dragging the mouse :

void CPolygonsView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_Drag = true;   // for mouse drag check
  PointOrigin = point;  // value when mouse drag starts
  CView::OnLButtonDown(nFlags, point);
}

void CPolygonsView::OnLButtonUp(UINT nFlags, CPoint point)
{

  m_Drag = false;  // for mouse drag check
  MotionFix=0;
  CView::OnLButtonUp(nFlags, point);

}

All the drawing is invoked by the MouseMove function . First the previously drawn Line is erased (by redrawing over it – using R2_NOT) and then the new Line is drawn using the new coordinates.

The loop computes all the other coordinates using these elements and draws lines connecting one vertex to the other.

void CPolygonsView::OnMouseMove(UINT nFlags, CPoint point)
{
  if (m_Drag && PointOrigin!=point) // for mouse drag check
  {
    CClientDC ClientDC(this);  // graphics
    ClientDC.SetROP2(R2_NOT);
    if (MotionFix) DrawArrow(&ClientDC,PointOrigin,PointOld);
    MotionFix=1;
 // MotionFix is used to prevent redrawing in case it is the
 // First Element 
    DrawArrow(&ClientDC,PointOrigin,point);
  }
  PointOld = point;
  CView::OnMouseMove(nFlags, point);
}

ok , that was the easy part , now the actual computation is done in the DrawArrow Function

void DrawArrow(CDC *pdc,CPoint m_One, CPoint m_Two)
{
  double slopy , cosy , siny;
  double Par = 10.0;  //length of Arrow (>)
  slopy = atan2( ( m_One.y - m_Two.y ),
    ( m_One.x - m_Two.x ) );
  cosy = cos( slopy );
  siny = sin( slopy ); //need math.h for these functions

  //draw a line between the 2 endpoint
  pdc->MoveTo( m_One );
  pdc->LineTo( m_Two );
  
  //here is the tough part - actually drawing the arrows
  //a total of 6 lines drawn to make the arrow shape
  pdc->MoveTo( m_One);
  pdc->LineTo( m_One.x + int( - Par * cosy - ( Par / 2.0 * siny ) ),
    m_One.y + int( - Par * siny + ( Par / 2.0 * cosy ) ) );
  pdc->LineTo( m_One.x + int( - Par * cosy + ( Par / 2.0 * siny ) ),
    m_One.y - int( Par / 2.0 * cosy + Par * siny ) );
  pdc->LineTo( m_One );
  /*/-------------similarly the the other end-------------/*/
  pdc->MoveTo( m_Two );
  pdc->LineTo( m_Two.x + int( Par * cosy - ( Par / 2.0 * siny ) ),
    m_Two.y + int( Par * siny + ( Par / 2.0 * cosy ) ) );
  pdc->LineTo( m_Two.x + int( Par * cosy + Par / 2.0 * siny ),
    m_Two.y - int( Par / 2.0 * cosy - Par * siny ) );
  pdc->LineTo( m_Two );

}

Downloads

Download demo project – 88 Kb
Download source – 22 Kb

Recommended for you...

Drawing 3D OpenGL Graphics on Google Maps
Mandelbrot Using C++ AMP
CodeGuru Staff
Jan 27, 2012
Simple C++ MP3 Player Class
CodeGuru Staff
Aug 22, 2011
Library for Raw Video Processing
CodeGuru Staff
Jun 14, 2011
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.