⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ugkpainter.cpp

📁 linux下一款GIS程序源码
💻 CPP
字号:
/* ugkpainter.cpp */#include "ugkpainter.h"#include <qpointarray.h> #include <math.h>#include <assert.h>#include "UGKGlobalFunc.h"#define PI 3.1415926UGKPainter::UGKPainter(const QPaintDevice *pd, bool unclipped):	QPainter(pd){}/************************************************    简单判断一下,一条直线是否与矩形相交    如果相交,则用x1,y1,x2,y2来保存在矩形    中的那段线段的坐标************************************************/bool UGKPainter::lineInPaintRect(int x1,int y1,int x2,int y2,QRect pRect){    LineCoord ln;    ln.start.x = x1;    ln.start.y = y1;    ln.end.x = x2;    ln.end.y = y2;    PTCoord a,b;    if( LineRect( ln, pRect) )	return TRUE;      else	return FALSE;}void UGKPainter::getLineBox(int x1,int y1, int x2, int y2,LineBox &lnBox){    double ang;    int aX,aY,bX,bY,cX,cY,dX,dY;    if( (x2>x1)&&(y2>y1) )//一象限    {	ang=atan( 1.0*(y2-y1)/(x2-x1));	aX = int(x1 +2*sin(ang));	aY = int(y1 -2*cos(ang));	bX = int(x1 -2*sin(ang));	bY = int(y1 +2*cos(ang));	cX = int(x2 +2*sin(ang));	cY = int(y2 -2*cos(ang));	dX = int(x2 -2*sin(ang));	dY = int(y2 +2*cos(ang));	       }    else if( (x1>x2)&&(y2>y1) )//二象限    {	ang=atan( 1.0*(y2-y1)/(x1-x2));	aX = int(x1+2*sin(ang));	aY = int(y1+2*cos(ang));	bX = int(x1-2*sin(ang));	bY = int(y1-2*cos(ang));	cX = int(x2+2*sin(ang));	cY = int(y2+2*cos(ang));	dX = int(x2-2*sin(ang));	dY = int(y2-2*cos(ang));    }    else if( (x1>x2)&&(y1>y2) )//三象限    {	ang=atan(1.0*(y1-y2)/(x1-x2));	aX = int(x1-2*sin(ang));	aY = int(y1+2*cos(ang));	bX = int(x1+2*sin(ang));	bY = int(y1-2*cos(ang));	cX = int(x2-2*sin(ang));	cY = int(y2+2*cos(ang));	dX = int(x2+2*sin(ang));	dY = int(y2-2*cos(ang));	    }    else if( (x2>x1)&&(y1>y2) )//四象限    {	ang=atan(1.0*(y1-y2)/(x2-x1));	aX = int(x1-2*sin(ang));	aY = int(y1-2*cos(ang));	bX = int(x1+2*sin(ang));	bY = int(y1+2*cos(ang));	cX = int(x2-2*sin(ang));	cY = int(y2-2*cos(ang));	dX = int(x2+2*sin(ang));	dY = int(y2+2*cos(ang));	    }    else if( (x2==x1)&&(y2>y1) )    {	aX = x1 +2;	aY = y1;	bX = x1-2;	bY = y1;	cX = x1 +2;	cY = y2;	dX = x1-2;	dY = y2;	    }    else if( (x2==x1)&&(y2<y1) )    {	aX = x1 -2;	aY = y1;	bX = x1+2;	bY = y1;	cX = x1 -2;	cY = y2;	dX = x1+2;	dY = y2;    }    else if( (x2>x1)&&(y2==y1) )    {	aX = x1;	aY = y1 -2;	bX = x1;	bY = y1 +2;	cX = x2;	cY = y1 -2;	dX = x2;	dY = y1 +2;    }    else if( (x2<x1)&&(y2==y1) )    {	aX = x1;	aY = y1 +2;	bX = x1;	bY = y1 -2;	cX = x2;	cY = y1 +2;	dX = x2;	dY = y1 -2;    }    else	assert(FALSE);//Error;       lnBox.leftStart.setX(aX);   lnBox.leftStart.setY(aY);   lnBox.leftEnd.setX(cX);   lnBox.leftEnd.setY(cY);   lnBox.rightStart.setX(bX);   lnBox.rightStart.setY(bY);   lnBox.rightEnd.setX(dX);   lnBox.rightEnd.setY(dY);   }/********************************************************               用指定线型绘制直线********************************************************/void UGKPainter::drawGISLine(const QPointArray & a,int linenum,QRect pRect,QColor cl){    switch(linenum)    {    case 1:	{	    save();	    QPen pen(Qt::NoPen);	    pen.setColor(cl);	    setPen(pen);	    	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    break;	}    case 2:	{	    save();	    QPen pen(Qt::SolidLine);	    pen.setColor(cl);	    setPen(pen);	    	 	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    	    break;	    	}    case 3:    case 4:	{	    save();	    QPen pen(Qt::DotLine);	    pen.setColor(cl);	    setPen(pen);	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    break;	    	}    case 5:	{	    save();	    QPen pen(Qt::DashLine);	    pen.setColor(cl);	    pen.setWidth(2);	    setPen(pen);	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    break;	    	}    case 6:	{	    save();	    QPen pen(Qt::DashLine);	    pen.setColor(cl);	    pen.setWidth(3);	    setPen(pen);	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    break;	    	}    case 7:	{	    save();	    QPen pen(Qt::DashLine);	    pen.setColor(cl);	    setPen(pen);	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }	    	    restore();	    break;	    	}	    case 63:    case 64:	{	    int x1,y1,x2,y2;	    double dist;	        	    save();	    	    QPen pen(Qt::SolidLine);	    pen.setColor(cl);	    setPen(pen);	    	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		dist = sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) );		if(dist<2)		    continue;  //距离太短 ,舍去这一点		if( !lineInPaintRect(x1,y1,x2,y2,pRect) )		{			x1= x2;			y1= y2;			continue;		}				LineBox lnBox;		getLineBox(x1,y1,x2,y2,lnBox);		//getLineBox(ax,ay,bx,by,lnBox);		drawLine(lnBox.leftStart,lnBox.leftEnd);		drawLine(lnBox.rightStart,lnBox.rightEnd);		x1= x2;		y1= y2;	    }	    	 restore();   	 break;	}    case 73:    case 74:    case 75:    case 76:    case 77:	{	 int x1,y1,x2,y2;	 bool bBlack = TRUE;	 int  space = 40;	 int  lastDrwLength = space;	 save();	 	 	 a.point(0,&x1,&y1);	 for(int i=1;i<a.size();i++)	 {	     a.point(i,&x2,&y2);	     if( (x1==x2)&&(y1==y2) )		 continue;	     double dist = sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1) );	     if(dist<2)		 continue;	     LineBox lnBox;	     getLineBox(x1,y1,x2,y2,lnBox);	     int px0,py0;	     px0 = x1;	     py0 = y1;   	     int offset =0;	  	     if(lastDrwLength!=space)	     {		 if(bBlack)		 {		     QPen pen(QColor("black"),3);		     setPen(pen);		 }		 else		 {		     QPen pen(QColor("white"),3);		     setPen(pen);		 }		 if(dist>space-lastDrwLength)		 {		     int px,py;		     px = int(x1 +(space-lastDrwLength)*(x2-x1)/dist);		     py = int(y1 +(space-lastDrwLength)*(y2-y1)/dist);		     drawLine(x1,y1,px,py);		     offset = space-lastDrwLength;    		     px0 = px;		     py0 = py;		     lastDrwLength = space;		     bBlack = !bBlack;		 }		 else		 { 		     drawLine(x1,y1,x2,y2);		     lastDrwLength +=dist;		     offset = dist;		     		 }		 	     }	     int count = int((dist-offset)/space);	     for(int i=1;i<=count;i++)	     {		 int px,py;		 px = int(x1 +(offset+i*space)*(x2-x1)/dist);		 py = int(y1 +(offset+i*space)*(y2-y1)/dist);		 if(bBlack)		 {		     QPen pen(QColor("black"),3);		     setPen(pen);		 }		 else		 {		     QPen pen(QColor("white"),3);		     setPen(pen);		 }		 		 drawLine(px0,py0,px,py);		 px0 = px;		 py0 = py;		 lastDrwLength = space;		 bBlack = !bBlack;	     }	     int lastLen = int(dist-offset-count*space);	     if(lastLen>0)	     {		 if(bBlack)		 {		     QPen pen(QColor("black"),3);		     setPen(pen);		 }		 else		 {		     QPen pen(QColor("white"),3);		     setPen(pen);		 }		 drawLine(px0,py0,x2,y2);		 lastDrwLength = lastLen;	     }	     	     QPen pen(QColor("black"),0);	     setPen(pen);	     drawLine(lnBox.leftStart,lnBox.leftEnd);	     drawLine(lnBox.rightStart,lnBox.rightEnd);	  	     x1= x2;	     y1= y2;	 }	 restore();	 break;        }    default: //默认的用2号线型		{	    save();	    QPen pen(Qt::SolidLine);	    pen.setColor(cl);	    setPen(pen);	    	 	    int x1,y1,x2,y2;	    a.point(0,&x1,&y1);	    for(int i=1;i<a.size();i++)	    {		a.point(i,&x2,&y2);		if( (x1==x2)&&(y1==y2) )		    continue;		if( lineInPaintRect(x1,y1,x2,y2,pRect) )		{			drawLine(x1,y1,x2,y2);		}				x1= x2;		y1= y2;	    }   	    restore();	    break;	    	}    }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -