带箭头的直线
来源:互联网 发布:三国志13自创武将数据 编辑:程序博客网 时间:2024/04/30 08:29
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 );
- 带箭头的直线
- 绘制带箭头的直线
- 绘制带箭头的直线
- 绘制带箭头的直线
- java 画带箭头的直线
- As3 带箭头直线的不同画法
- java 画带箭头的直线
- Silverlight带箭头直线控件
- 画直线 水平夹角的 带箭头的
- MFC中如何画带实心箭头的直线
- MFC中如何画带实心箭头的直线
- MFC中如何画带实心箭头的直线
- html5画直线带箭头 画圆
- google map 地图轨迹点以画带箭头的直线连接,直观轨迹记录。
- 带箭头的listbox
- 带箭头的DIV
- 在直线上画箭头的实现
- 关于google地图api3的离线和在线开发(画带箭头的直线,指定范围,搜索,计算距离)
- 蓝色妖姬花的故事和花语
- 魅族M6硬件升级(闪存+电池)
- 23中设计模式的有趣见解
- Windows网络编程经验小结
- (转)linux内核的移植与遭遇问题的解决
- 带箭头的直线
- js实现一点击组件滚动条到最下面
- 带进度条的ASP无组件断点续传下载
- 城市应急联动指挥系统——(系统集成)稳定高效的大型项目设计案例
- 防止用户重复登录
- FCKeditor使用说明
- Function实现ALV Table六:页眉页脚
- 寻找
- lucene按时间排序 我在项目中的应用(三)