Scan 算法

class Line
{
public:
 CCPoint start;
 CCPoint end;
 double k;
 double c;
 double minX,maxX;
 double minY,maxY;
 double getX(double y)
 {
  return  k == 0 ? c : (y - c)/k;
 }
 double getY(double x)
 {
  return k * x + c;
 }
 Line(CCPoint x,CCPoint y)
 {
  start = x;
  end = y;
  k = end.x == start.x ? 0 : (end.y - start.y) / (end.x - start.x);

  c = start.y - k * start.x;
  if (start.x > end.x)
  {
   minX = end.x;
   maxX = start.x;
  }
  else
  {
   maxX = end.x;
   minX = start.x;

  }
  if (start.y > end.y)
  {
   minY = end.y;
   maxY = start.y;
  }
  else
  {
   maxY = end.y;
   minY = start.y;
  }
  //k = atan(k);
 }
};
vector<CCPoint> g_vecPoint;
vector<CCPoint> g_vecPoint2;
vector<Line> g_line,g_line2;
int minY = 0,maxY = 0;

 

 

g_vecPoint.push_back(ccp(900,600));
 g_vecPoint.push_back(ccp(70,70));
 g_vecPoint.push_back(ccp(400,500));


 g_vecPoint2.push_back(ccp(900,30));
 g_vecPoint2.push_back(ccp(70,50));
 g_vecPoint2.push_back(ccp(400,400));
 g_vecPoint2.push_back(ccp(300,30));
 g_vecPoint2.push_back(ccp(400,30));

 initPoly(g_vecPoint,g_line);
 initPoly(g_vecPoint2,g_line2);


vector<Line> vLineTmp;
void buildPoly(vector<Line>& vecLine,int maxGroup,int group,int maxNum,int index,double angel)

 if (vLineTmp.size() == maxGroup - 1)
 {
  double tmp = atan( abs((vLineTmp[0].k - vLineTmp[1].k)/(1 + vLineTmp[0].k*vLineTmp[1].k)));
  for(int i = 0; i < maxNum; ++i)
  {
   vLineTmp.push_back(vecLine[i + group * maxNum]);   
   double tmp2 = atan( abs((vLineTmp[1].k - vLineTmp[2].k)/(1 + vLineTmp[1].k*vLineTmp[2].k)));
   double tmp3 = atan( abs((vLineTmp[2].k - vLineTmp[0].k)/(1 + vLineTmp[0].k*vLineTmp[2].k)));
   double t = tmp + tmp2 + tmp3;
   if (abs(t - 3.14/2) < 0.01  )   
   {
    return ;
   }
   vLineTmp.pop_back();
  }
 }
 else
 {
  for (int i = 0; i < maxNum; ++i)
  {
   vLineTmp.push_back( vecLine[i  + group * maxNum ]);
   buildPoly(vecLine,maxGroup,group + 1,maxNum,index + 1,angel + vecLine[i + group * maxNum].k);
   vLineTmp.pop_back();
  }
 } 
}


void initPoly(vector<CCPoint>& vecPoint,vector<Line>& vecLine)
{
 CCPoint begin,end;
 vector<CCPoint> vec;
 CCPoint pt =  CCPointZero;
 vector<CCPoint>::iterator tmp,tmp2;
 double minK = 0;
 vector<CCPoint>::iterator it = vecPoint.begin();
 tmp2 = it;
 double lastK = -1;
 vector<Line> retLine;
 vector<double> vecK;
 int size = vecPoint.size();
 //while (1)
 {
  for(vector<CCPoint>::iterator it = vecPoint.begin();it != vecPoint.end();it++)
  {
   for(vector<CCPoint>::iterator it2 = vecPoint.begin();it2 != vecPoint.end();it2++)
   {
    if (it != it2)
    {
     Line line(*it,*it2);
     vecLine.push_back(line);
    }
   }
  }
  double angel = 0;
  buildPoly(vecLine,size,0,size-1,0,angel);
  //vecLine.push_back(Line(*it,*tmp));
  tmp2 = it;
  it = tmp;  
  tmp = vecPoint.end(); 
  lastK = -minK;
  minK = 0;
  if (vecLine.size() == vecPoint.size())
  {
   //break;
  }
 } 

 for(vector<CCPoint>::iterator it = vecPoint.begin();it != vecPoint.end(); ++it)
 {
  if (minY == 0 || it->y < minY)
  {
   minY = it->y;
  }
  if (maxY == 0 || it->y > maxY)
  {
   maxY = it->y;
  }
  if (begin.x == 0 && begin.y == 0 )
  {
   begin = *it;
   end = *it;
   continue;
  } 
 }
}

void drawPoly(vector<Line>& vecLine,int i)
{
 if (i == 0)
 {
  ccDrawColor4F(255,255,255,255);
 }
 else
 {
  ccDrawColor4F(0,255,255,255);
 }
 for(vector<Line>::iterator it = vecLine.begin();it != vecLine.end(); ++it)
 {
  ccDrawLine(it->start,it->end);
 }
}

void scanPoly(vector<Line>& vecLine,vector<CCPoint>& ret)
{
 double x = 0;
 CCPoint pt;
 for(int y = minY; y < maxY; ++y)
 {
  for(vector<Line>::iterator it = vecLine.begin();it != vecLine.end(); ++it)
  {
   if ( y > it->minY && y < it->maxY )
   {
    x = it->getX(y);
    if (x > it->minX &&  x < it->maxX)
    {
     pt = ccp(x,y);
     ret.push_back(pt);
    }
   }   
  }
 } 
}

void drawCon()
{
 ccDrawColor4F(255,0,0,255);
 vector<CCPoint> ret,ret2;
 scanPoly(g_line,ret);
 scanPoly(g_line2,ret2);
 vector<CCPoint>::iterator it = ret.begin();
 vector<CCPoint>::iterator it2 = ret2.begin();
 int minX1,maxX1,minX2,maxX2;
 for(int y = minY; y < maxY; ++y)
 {
  minX1 = maxX1 = minX2 = maxX2 = 0;
  while( it != ret.end() && y == it->y)
  {
   if (minX1 == 0 || minX1 > it->x)
   {
    minX1 = it->x;
   }
   if (maxX1 == 0 || maxX1 < it->x)
   {
    maxX1 = it->x;
   }
   it++;
  }
  while( it2 != ret2.end() && y == it2->y)
  {
   if (minX2 == 0 || minX2 > it2->x)
   {
    minX2 = it2->x;
   }
   if (maxX2 == 0 || maxX2 < it2->x)
   {
    maxX2 = it2->x;
   }
   it2++;
  }
  if ( (minX1 > minX2 && minX1 < maxX2))
  { 
   ccDrawPoint(ccp(minX2,y));    
  } 
  if (maxX1 > minX2 && maxX1 < maxX2)
  {
   ccDrawPoint(ccp(maxX1,y));
  }
  if ( (minX2 > minX1 && minX2 < maxX1))
  { 
   ccDrawPoint(ccp(minX2,y));    
  } 
  if (maxX2 > minX1 && maxX2 < maxX1)
  {
   ccDrawPoint(ccp(maxX2,y));
  }
 }
 
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值