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

📄 line.cpp

📁 C语言库函数(包括所有的C语言库函数)
💻 CPP
字号:
// Line.cpp: implementation of the Line class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PainterUsePattern.h"
#include "Line.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//=================================================================
//构造函数;
//=================================================================
Line::Line()
{

}
//=================================================================
//析构函数;
//=================================================================
Line::~Line()
{

}
//=================================================================
//接受访问者访问接口;
//=================================================================
void Line::Accept( AbstractVisitor& v , CDC* pDC)
{
	v.VisitLine( this ,pDC);
}
//=================================================================
//检测指定点p是否在线段上 : 根据点P的坐标范围和斜率进行判断; 
//=================================================================
bool Line::Intersects( CPoint p )
{	
	//点p在线段起点或者终点;
	if( p == m_startPoint || p == m_endPoint )
		return true;

	double slope;	//线段斜率;
	double slope1;  //终点和点p连线的斜率;
    
	int max = ( m_endPoint.x >= m_startPoint.x )? m_endPoint.x : m_startPoint.x;
	int min = ( m_endPoint.x <  m_startPoint.x )? m_endPoint.x : m_startPoint.x;
    
	//直线走向为 : 右上角到左下角
	if( m_endPoint.y >= m_startPoint.y )
	{
		//直线接近于垂直, 斜率比较大的情况;
		if( max - min < 5 )
		{
			//不根据斜率判断, 直接根据p点坐标进行判断;
			if( p.x > m_endPoint.x - 5 && p.x < m_endPoint.x + 5 && p.y >= m_startPoint.y && p.y <= m_endPoint.y )
				return true;
			else 
				return false;
		}
		//斜率比较小的情况;
		else
			//计算直线斜率;
         	slope  = ( m_endPoint.y - m_startPoint.y )/(float)( m_endPoint.x - m_startPoint.x );
	}
	//直线走向为 : 左上角到右下角
    else
	{
		//直线接近于垂直, 斜率比较大的情况;
		if( max - min < 5 )
		{
			//不根据斜率判断, 直接根据p点坐标进行判断;
			if( p.x > m_endPoint.x - 5 && p.x < m_endPoint.x + 5 && p.y >= m_endPoint.y && p.y <= m_startPoint.y )
				return true;
			else 
				return false;
		}
		//斜率比较小的情况;
		else
			//计算直线斜率;
    		slope  = ( m_startPoint.y - m_endPoint.y )/(float)( m_startPoint.x - m_endPoint.x  );
	}

	//线段终点于p点的连线走向 : 右上角到左下角;
    if( m_endPoint.y >= p.y )
		//计算连线的斜率;
        slope1 = ( m_endPoint.y - p.y )/(float)( m_endPoint.x - p.x );

	//线段终点于p点的连线走向 : 右上角到左下角;
 	else
		//计算连线的斜率;
    	slope1 = ( p.y - m_endPoint.y  )/(float)(p.x - m_endPoint.x );
	
	//取得直线横坐标,纵坐标的取值范围;
	// x 轴 : (xMin,xMax) ;
	// y 轴 : (yMin,yMax) ;
	int xMin   = ( m_endPoint.x <= m_startPoint.x )?m_endPoint.x : m_startPoint.x ;
	int xMax   = ( m_endPoint.x >  m_startPoint.x )?m_endPoint.x : m_startPoint.x ;
	int yMin   = ( m_endPoint.y <= m_startPoint.y )?m_endPoint.y : m_startPoint.y ; 
	int yMax   = ( m_endPoint.y > m_startPoint.y  )?m_endPoint.y : m_startPoint.y ;
    
	//取斜率的绝对值;
	slope  = ( slope  > 0 )?slope :-slope;
	slope1 = ( slope1 > 0 )?slope1:-slope1;

	//求两斜率放大后的偏差;
	int  balance = (int)(slope*10) - (int)(slope1*10) ;

    //误差允许范围 : (-error, +error); 
	int  error = 0 ;

 
	//根据斜率决定误差范围;
	if( slope >= 1000 )
		error = 3000;

	else if( slope >= 100 && slope < 1000 )
		error = 2000;

	else if( slope >= 10 && slope < 100 )
		error = 100;

	else if( slope >= 1 && slope < 10 )
		error = 2;
	else
		error = 1;

    //斜率偏差是否在误差范围之内;
	bool flag1 = ( balance > -error && balance < error )?true : false;

	//坐标范围是否在直线的坐标取值范围之内;
	bool flag2 = ( p.x <= xMax && p.x >= xMin )?true : false;
    bool flag3 = ( p.y <= yMax && p.y >= yMin )?true : false;
	
	if( flag1 && flag2 && flag3 )
		return true;
	return false;
}
//=================================================================
//设置直线位置的偏移量;
//=================================================================
void Line::SetOffset( int offsetX , int offsetY )
{
	m_startPoint.x += offsetX;
	m_startPoint.y += offsetY;

	m_endPoint.x += offsetX;
	m_endPoint.y += offsetY;
}

⌨️ 快捷键说明

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