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

📄 utils.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:



/*! This method calculates the x coordinate given the x coordinate

    \param y coordinate

    \return x coordinate on this line */

double Line::getXGivenY( double y )

{

 if( m_b == 0 )

 {

   cerr << "(Line::getXGivenY) Cannot calculate X coordinate\n" ;

   return 0;

 }

  // ay + bx + c = 0 ==> bx = -(a*y + c)/a

  return -(m_a*y+m_c)/m_b;

}



/*! This method creates a line given two points.

    \param pos1 first point

    \param pos2 second point

    \return line that passes through the two specified points. */

Line Line::makeLineFromTwoPoints( Vector3 pos1, Vector3 pos2 )

{

  // 1*y + bx + c = 0 => y = -bx - c

  // with -b the direction coefficient (or slope)

  // and c = - y - bx

  double dA=1.0, dB, dC;

  double dTemp = pos2.x - pos1.x; // determine the slope

  if( fabs(dTemp) < EPS )

  {

    // ay + bx + c = 0 with vertical slope=> a = 0, b = 1

    dA = 0.0;

    dB = 1.0;

  }

  else

  {

    // y = (-b)x -c with -b the slope of the line

    dA = 1.0;

    dB = -(pos2.y - pos1.y)/dTemp;

  }

  // ay + bx + c = 0 ==> c = -a*y - b*x

  dC =  - dA*pos2.y  - dB * pos2.x;

  return Line( dA, dB, dC );

}



/*! This method creates a line given a position and an angle.

    \param vec position through which the line passes

    \param angle direction of the line.

    \return line that goes through position 'vec' with angle 'angle'. */

Line Line::makeLineFromPositionAndAngle( Vector3 vec, Angle angle )

{

  // calculate point somewhat further in direction 'angle' and make

  // line from these two points.

  return makeLineFromTwoPoints( vec, vec+Vector3(1,angle,0,POLAR));

}



/*! This method returns the a coefficient from the line ay + bx + c = 0.

    \return a coefficient of the line. */

double Line::getACoefficient() const

{

  return m_a;

}



/*! This method returns the b coefficient from the line ay + bx + c = 0.

    \return b coefficient of the line. */

double Line::getBCoefficient() const

{

 return m_b;

}



/*! This method returns the c coefficient from the line ay + bx + c = 0.

    \return c coefficient of the line. */

double Line::getCCoefficient() const

{

 return m_c;

}



/******************************************************************************/

/********************** CLASS RECTANGLE ***************************************/

/******************************************************************************/



/*! This is the constructor of a Rectangle_. Two points will be given. The

    order does not matter as long as two opposite points are given (left

    top and right bottom or right top and left bottom).

    \param pos first point that defines corner of rectangle

    \param pos2 second point that defines other corner of rectangle

    \return rectangle with 'pos' and 'pos2' as opposite corners. */

Rectangle_::Rectangle_( Vector3 pos, Vector3 pos2 )

{

  setRectanglePoints( pos, pos2 );

}



/*! This method sets the upper left and right bottom point of the current

    rectangle.

    \param pos first point that defines corner of rectangle

    \param pos2 second point that defines other corner of rectangle */

void Rectangle_::setRectanglePoints( Vector3 pos1, Vector3 pos2 )

{

  m_posLeftTop.x = ( max( pos1.x, pos2.x ) );

  m_posLeftTop.y = ( min( pos1.y, pos2.y ) );

  m_posRightBottom.x = ( min( pos1.x, pos2.x ) );

  m_posRightBottom.y = ( max( pos1.y, pos2.y ) );

}



/*! This method prints the rectangle to the specified output stream in the

    format rect( top_left_point, bottom_right_point ).

    \param os output stream to which rectangle is written. */

void Rectangle_::show( ostream& os )

{

//  cout << "rect(" << m_posLeftTop << " " << m_posRightBottom << ")";

}



/*! This method determines whether the given position lies inside the current

    rectangle.

    \param pos position which is checked whether it lies in rectangle

    \return true when 'pos' lies in the rectangle, false otherwise */

bool Rectangle_::isInside( Vector3 pos )

{

  return pos.x > m_posRightBottom.x && pos.x < m_posLeftTop.x &&

         pos.y > m_posLeftTop.y && pos.y < m_posRightBottom.y;



}



/*! This method sets the top left position of the rectangle

    \param pos new top left position of the rectangle

    \return true when update was successful */

bool Rectangle_::setPosLeftTop( Vector3 pos )

{

  m_posLeftTop = pos;

  return true;

}



/*! This method returns the top left position of the rectangle

    \return top left position of the rectangle */

Vector3 Rectangle_::getPosLeftTop( Vector3 pos )

{

  return m_posLeftTop;

}



/*! This method sets the right bottom position of the rectangle

    \param pos new right bottom position of the rectangle

    \return true when update was succesfull */

bool Rectangle_::setPosRightBottom( Vector3 pos )

{

  m_posRightBottom = pos;

  return true;

}



/*! This method returns the right bottom position of the rectangle

    \return top right bottom of the rectangle */

Vector3 Rectangle_::getPosRightBottom( Vector3 pos )

{

  return m_posRightBottom;

}



/******************************************************************************/

/********************** TESTING PURPOSES *************************************/

/******************************************************************************/



/*

#include<iostream.h>



int main( void )

{

  double dFirst = 1.0;

  double dRatio = 2.5;

  double dSum   = 63.4375;

  double dLength = 4.0;



  printf( "sum: %f\n", Geometry::getSumGeomSeries( dFirst, dRatio, dLength));

  printf( "length: %f\n", Geometry::getLengthGeomSeries( dFirst, dRatio, dSum));

}



int main( void )

{

  Line l1(1,-1,3 );

  Line l2(1,-0.2,10 );

 Line l3 = Line::makeLineFromTwoPoints( Vector3(1,-1), Vector3(2,-2) );

 l3.show();

 cout << endl;

 l1.show();

 l2.show();

  l1.getIntersection( l2 ).show();

}





int main( void )

{

  Line l( 1, -1, 0 );

  Vector3 s1, s2;

  int i = l.getCircleIntersectionPoints( Circle( Vector3(1,1),1) &s1, &s2 );

  printf( "number of solutions: %d\n", i );

  if( i == 2 )

  {

    cout << s1 << " " << s2 ;

  }

  else if( i == 1 )

  {

    cout << s1;

  }

  cout << "line: " << l;

}



int main( void )

{

  Circle c11( Vector3( 10, 0 ), 10);

  Circle c12( Vector3( 40, 3 ), 40 );

  Circle c21( Vector3(  0,0 ), 5);

  Circle c22( Vector3(  3,0 ), 40 );



  Vector3 p1, p2;



  cout << c11.getIntersectionArea( c21 ) << endl;

  cout << c12.getIntersectionArea( c21 ) << endl;

  cout << c22.getIntersectionArea( c11 ) << endl;

  cout << c12.getIntersectionArea( c22 ) << endl;

  return 0;

}



int main( void )

{

  cout << getBisectorTwoAngles( -155.3, 179.0 ) << endl;

  cout << getBisectorTwoAngles( -179.3, 179.0 ) << endl;

}

*/

/**********************Added by Solarrain*****************************************/

/******************************************************************************/

/********************** CLASS RAY *********************************************/

/******************************************************************************/



Ray::Ray(const Vector3& orig, double ang)

{

		m_origin = orig;

		m_direction =  Vector3(1.0, ang,POLAR);

		m_angle = ang;

		Line tmp = makeLineFromPositionAndAngle(m_origin,ang);

		m_a = tmp.getACoefficient();

		m_b = tmp.getBCoefficient();

		m_c = tmp.getCCoefficient();

}



Ray::Ray(double a,double b, double c,Vector3 pos,Angle angle)

{

	m_a=a;

	m_b=b;

	m_c=c;

	m_origin=pos;

	m_angle=angle;

	m_direction=Vector3(1.0,angle,0, POLAR);

}



Angle Ray::Allowed_AngleDiff(double distance){

	return double(10 - 5*exp(-distance * 0.2f));

}



Angle Ray::Allowed_AngleDiff(Vector3 point){

	return Allowed_AngleDiff((point - m_origin).mod());

}



bool Ray::InRightDir(Vector3 point){

	return fabs(Normalize((point - m_origin).getDirection() - m_angle)) < Allowed_AngleDiff(point);

}



bool Ray::InRightDir(Angle ang){

	return fabs(Normalize(ang - m_angle)) < Allowed_AngleDiff();

}



bool Ray::InOppositeDir(Vector3 point){

	return fabs(Normalize((point - m_origin).getDirection() - m_angle + 180)) < Allowed_AngleDiff(point);

}



bool Ray::InOppositeDir(Angle ang){

	return fabs(Normalize(ang - m_angle + 180)) < Allowed_AngleDiff();

}



Ray	 Ray::MakeRayFromPositionAndAngle(Vector3 pos,Angle angle){

    Line line=Line::makeLineFromPositionAndAngle(pos,angle);

	return Ray(line.getACoefficient(),line.getBCoefficient(),line.getCCoefficient(),pos,angle);

}



double Ray::getDistanceFromOrigin(Vector3 pos){

	return (pos-this->m_origin).mod();

}



/* intersects a ray and a cricle */

/* return the number of solutions */

/* psol1 1 is not as far along the ray as psol2 */

int Ray::CircleIntersect(double radius, const Vector3& center, Vector3& psol1, Vector3& psol2) const

{

	double a,b,c,disc;

	double t1, t2;

	a = Sqr(m_direction.x) + Sqr(m_direction.y);

	b = 2.0f * ((m_origin.x-center.x) * m_direction.x + (m_origin.y-center.y) * m_direction.y);

	c = Sqr(m_origin.x-center.x) + Sqr(m_origin.y-center.y) - Sqr(radius);



	disc = Sqr(b) - 4 * a * c;

	if (disc < 0) {

		return 0; // No solutions

	}

  

	disc = (double) sqrt(disc);

	t1 = (-b + disc) / (2.0f * a);

	t2 = (-b - disc) / (2.0f * a);

  

	if (t1 > t2) {

		double temp = t1;

		t1 = t2;

		t2 = temp;

	}

	Vector3 tmp = m_direction;

 

	if (t1 > 0.0) {

		if (t2 > 0.0) {

			tmp = tmp * t1;

			psol1 = tmp + m_origin ;

			tmp = m_direction;

			tmp = tmp * t2;

			psol2 = tmp + m_origin;

			return 2; // Two solutions

		}

		else{

			return 0;

		}

	}

	else if (t2 > 0.0) {

		tmp = m_direction;

		tmp = tmp * t2;

		psol2 = tmp + m_origin;

		psol2 = psol1;

		return 1;

	}

	else

		return 0;

	return 0;

}

////==========================



CAngle::CAngle(Vector3 p,Vector3 begin,Vector3 end,Vector3 bisector)

{

	ang1 = (begin - p).getDirection();

	ang2 = (end - p).getDirection();

	this->p = p;

	this->bisector = (bisector-p).getDirection();

	

}

CAngle::CAngle(Vector3 p,double begin,double end,double bisector)

{

	ang1 = begin;

	ang2 = end;

	this->p = p;

	this->bisector = bisector;

	

}

int CAngle::getIntersection(CAngle angle,CAngle& angout1,CAngle& angout2)

{

	if((angle.p - this->p).mod() > EPS)

		return 0;

	Angle m1,n1,m2,n2;

	m1 = max(ang1,ang2);

	m2 = max(angle.ang1,angle.ang2);

	n1 = min(ang1,ang2);

	n2 = min(angle.ang1,angle.ang2);

	if(isNormal()&&angle.isNormal()){

		if(n1>m2||n2>m1)

			return 0;

		angout1.p = p;

		angout1.ang1 = max(n1,n2);

		angout1.ang2 = min(m1,m2);

		angout1.bisector = (angout1.ang1 + angout1.ang2)/2.0;

		return 1;

	}

	if(isNormal()&&!angle.isNormal()){

		if(n1>n2&&m1<m2)

			return 0;

		if(n1<n2&&m1<m2){

			angout1.p = p;

			angout1.ang1 = n1;

			angout1.ang2 = n2;

			angout1.bisector = (n1+n2)/2.0;

			return 1;

		}

		if(n1>n2&&m1>m2){

			angout1.p = p;

			angout1.ang1 = m1;

			angout1.ang2 = m2;

			angout1.bisector = (m1+m2)/2.0;

			return 1;

		}

		angout1.p = p;

		angout1.ang1 = m1;

		angout1.ang2 = m2;

		angout1.bisector = (m1+m2)/2.0;

		

		angout2.p = p;

		angout2.ang1 = n1;

		angout2.ang2 = n2;

		angout2.bisector = (n1+n2)/2.0;

		return 2;

			

	}

	if(!isNormal()&&angle.isNormal()){

		if(n2>n1&&m2<m1)

			return 0;

		if(n2<n1&&m2<m1){

			angout1.p = p;

			angout1.ang1 = n2;

			angout1.ang2 = n1;

			angout1.bisector = (n2+n1)/2.0;

			return 1;

		}

		if(n2>n1&&m2>m1){

			angout1.p = p;

			angout1.ang1 = m2;

			angout1.ang2 = m1;

			angout1.bisector = (m2+m1)/2.0;

			return 1;

		}

		angout1.p = p;

		angout1.ang1 = m2;

		angout1.ang2 = m1;

		angout1.bisector = (m2+m1)/2.0;

		

		angout2.p = p;

		angout2.ang1 = n2;

		angout2.ang2 = n1;

		angout2.bisector = (n2+n1)/2.0;

		return 2;

			

	}

	angout1.p = p;

	angout1.ang1 = min(n1,n2);

	angout1.ang2 = max(m1,m2);

	angout1.bisector = Normalize((angout1.ang1+angout1.ang2)/2.0+180);

	return 1;

}



bool CAngle::isNormal()

{

	if(bisector>min(ang1,ang2)&&bisector<max(ang1,ang2))

		return true;

	return false;

}

⌨️ 快捷键说明

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