Skip to content

Reply To: How to create 2D triangle ?

#6117
Fany
Participant

Hi Adrian, I am creating the 2D triangle and cuboid without parallel to the X=0 and Y=0. But the computation with my geometry diverged. Could you please help check what the problems with the code below are?


// creating the cuboid with the theta of rotation around its center
template <typename S>
IndicatorCuboid2D<S>::IndicatorCuboid2D(S xLength, S yLength, Vector<S,2> center, S theta )
  : _center(center), _xLength(xLength), _yLength(yLength), _theta(theta)
{
  if (_theta>=0 && _theta <= M_PI2){
     this->_myMin = {_center[0] - (_xLength*std::cos(_theta)/2.+_yLength*std::sin(_theta)/2.), _center[1] - (_yLength*std::cos(_theta)/2.+_xLength*std::sin(_theta)/2.) };
     this->_myMax = {_center[0] + (_xLength*std::cos(_theta)/2.+_yLength*std::sin(_theta)/2.), _center[1] + (_yLength*std::cos(_theta)/2.+_xLength*std::sin(_theta)/2.) };
  } 
  if (_theta>=-M_PI2 && _theta < 0){
	 this->_myMin = {_center[0] - (_xLength*std::cos(_theta)/2.-_yLength*std::sin(_theta)/2.), _center[1] - (_yLength*std::cos(_theta)/2.-_xLength*std::sin(_theta)/2.) };
     this->_myMax = {_center[0] + (_xLength*std::cos(_theta)/2.-_yLength*std::sin(_theta)/2.), _center[1] + (_yLength*std::cos(_theta)/2.-_xLength*std::sin(_theta)/2.)};  
  }
}
template <typename S>
bool IndicatorCuboid2D<S>::operator()(bool output[], const S input[])
{
  S x, y;
  if ( !util::nearZero(_theta) ) {
    x = _center[0] + (input[0] - _center[0])*std::cos(_theta) + (input[1] - _center[1])*std::sin(_theta);  
    y = _center[1] + (input[1] - _center[1])*std::cos(_theta) - (input[0] - _center[0])*std::sin(_theta);
  } else {
    x = input[0];
    y = input[1];
  }

  output[0] = (  (fabs(_center[0] - x) < _xLength/2. || util::approxEqual(fabs(_center[0] - x),_xLength/2.) )
                 && (fabs(_center[1] - y) < _yLength/2. || util::approxEqual(fabs(_center[1] - y), _yLength/2.) ) );
  return true;
}

//  creating the triangle
template <typename S>
IndicatorTriangle2D<S>::IndicatorTriangle2D(Vector<S,2> PointA, Vector<S,2> PointB, Vector<S,2> PointC)
 : _PointA(PointA), _PointB(PointB), _PointC(PointC)
{
   
  this->_myMin = { std::min(_PointA[0], std::min(_PointB[0], _PointC[0])),  std::min(_PointA[1], std::min(_PointB[1], _PointC[1]))};
  this->_myMax = { std::max(_PointA[0], std::max(_PointB[0], _PointC[0])),  std::max(_PointA[1], std::max(_PointB[1], _PointC[1]))};

  _ab = _PointB - _PointA;
  _bc = _PointC - _PointB;
  _ca = _PointA - _PointC;
  
}

template <typename S>
bool IndicatorTriangle2D<S>::operator()(bool output[], const S input[])
{
  Vector<S, 2> _Point ;
  _Point[0] = input[0];
  _Point[1] = input[1]; //any point M
  Vector<S, 2> _am=_Point - _PointA ; 
  Vector<S, 2> _bm=_Point - _PointB ;
  Vector<S, 2> _cm=_Point - _PointC ;
    
  S S_ABM = fabs(_ab[1]*_am[0]-_ab[0]*_am[1])/2. ;   // the area of triangle ABM;
  S S_BCM = fabs(_bc[1]*_bm[0]-_ca[0]*_bm[1])/2. ;  // the area of triangle BCM;
  S S_CAM = fabs(_ca[1]*_cm[0]-_ca[0]*_cm[1])/2. ;  // the area of triangle CAM;
  S Sum = fabs(_ca[1]*_ab[0]-_ca[0]*_ab[1])/2. ;  // the area of triangle CAM;
  
  output[0] = util::approxEqual(S_ABM + S_BCM + S_CAM,Sum) ;  
  return true;   
}