MFC中怎么画带箭头的直线
2个回答
展开全部
// MFC中画带箭头的直线
void CDrawArrowDemoView::DrawArrow(CPoint p1,CPoint p2,double theta,int length)
{
theta=3.1415926*theta/180;//转换为弧度
double Px,Py,P1x,P1y,P2x,P2y;
//以P2为原点得到向量P2P1(P)
Px=p1.x-p2.x;
Py=p1.y-p2.y;
//向量P旋转theta角得到向量P1
P1x=Px*cos(theta)-Py*sin(theta);
P1y=Px*sin(theta)+Py*cos(theta);
//向量P旋转-theta角得到向量P2
P2x=Px*cos(-theta)-Py*sin(-theta);
P2y=Px*sin(-theta)+Py*cos(-theta);
//伸缩向量至制定长度
double x1,x2;
x1=sqrt(P1x*P1x+P1y*P1y);
P1x=P1x*length/x1;
P1y=P1y*length/x1;
x2=sqrt(P2x*P2x+P2y*P2y);
P2x=P2x*length/x2;
P2y=P2y*length/x2;
//平移变量到直线的末端
P1x=P1x+p2.x;
P1y=P1y+p2.y;
P2x=P2x+p2.x;
P2y=P2y+p2.y;
CClientDC dc(this);//获取客户窗口DC
CPen pen,pen1,*oldpen;
pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
pen1.CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
oldpen=dc.SelectObject(&pen);
dc.MoveTo(p1.x,p1.y);
dc.LineTo(p2.x,p2.y);
dc.SelectObject(&pen1);
dc.MoveTo(p2.x,p2.y);
dc.LineTo(P1x,P1y);
dc.MoveTo(p2.x,p2.y);
dc.LineTo(P2x,P2y);
dc.SelectObject(oldpen);
}
推荐于2016-04-04 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:25897
获赞数:1464988
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。
向TA提问 私信TA
关注
展开全部
构造一个函数,是在startPoint,endPoint间画一条带箭头的线段:
void CTry1View::DrawLine(POINT startPoint, POINT endPoint)
{
CClientDC dc(this);
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
double PI = 3.1415926;
double t=PI/4; //箭头与直线夹角
double l=0.2; //箭头边长度占直线长度的百分比
POINT arrowPoint;
arrowPoint.x = (int)(l*((startPoint.x-endPoint.x)*cos(t)-(startPoint.y-endPoint.y)*sin(t))+endPoint.x);
arrowPoint.y = (int)(l*((startPoint.x-endPoint.x)*sin(t)+(startPoint.y-endPoint.y)*cos(t))+endPoint.y);
dc.MoveTo(endPoint);
dc.LineTo(arrowPoint);
arrowPoint.x = (int)(l*((startPoint.x-endPoint.x)*cos(-t)-(startPoint.y-endPoint.y)*sin(-t))+endPoint.x);
arrowPoint.y = (int)(l*((startPoint.x-endPoint.x)*sin(-t)+(startPoint.y-endPoint.y)*cos(-t))+endPoint.y);
dc.MoveTo(endPoint);
dc.LineTo(arrowPoint);
}
void CTry1View::DrawLine(POINT startPoint, POINT endPoint)
{
CClientDC dc(this);
dc.MoveTo(startPoint);
dc.LineTo(endPoint);
double PI = 3.1415926;
double t=PI/4; //箭头与直线夹角
double l=0.2; //箭头边长度占直线长度的百分比
POINT arrowPoint;
arrowPoint.x = (int)(l*((startPoint.x-endPoint.x)*cos(t)-(startPoint.y-endPoint.y)*sin(t))+endPoint.x);
arrowPoint.y = (int)(l*((startPoint.x-endPoint.x)*sin(t)+(startPoint.y-endPoint.y)*cos(t))+endPoint.y);
dc.MoveTo(endPoint);
dc.LineTo(arrowPoint);
arrowPoint.x = (int)(l*((startPoint.x-endPoint.x)*cos(-t)-(startPoint.y-endPoint.y)*sin(-t))+endPoint.x);
arrowPoint.y = (int)(l*((startPoint.x-endPoint.x)*sin(-t)+(startPoint.y-endPoint.y)*cos(-t))+endPoint.y);
dc.MoveTo(endPoint);
dc.LineTo(arrowPoint);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询