// START OF ROTATINGMULTICURSOR RotatingMultiCursor::RotatingMultiCursor(double xx, double yy, double rr, int s, const vector<GradientCircleCursor>& in) : CursorContainer{in}, x{ xx }, y{ yy }, r{ rr }, speed{ s }{ initAngles(); }
void SightLightScene::onTouchMoved(cocos2d::Touch *touch,cocos2d::Event* event) { Point tar(0,0); //光线的端点 Point cur(0,0); //光线与线段的交点 float distance = 0; //光源与交点的距离 _touchDraw->clear(); auto pos = touch->getLocation(); //_touchDraw->drawDot(pos,5,Color4F::RED); //计算极角,并添加两个偏移1e-4的极角 initAngles(pos); //极角排序 std::sort(_angles.begin(), _angles.end(), [](float x,float y){ return x < y; }); std::vector<cocos2d::Vec2> vertex; //找最近的交点 // std::vector<Point> vertex; for (auto angle:_angles) { Vec2 dlt(cos(angle),sin(angle)); float closest = -1; for (auto s:_segments) { if (getIntersection(Line(pos,pos + dlt),s,cur,distance)) { if (closest == -1 || closest > distance) { closest = distance; tar = cur; } } } if (closest != -1) { vertex.push_back(tar); } } //画三角形 //下面2个循环第3个参数可以写为vertex[(i+1) % vertex.size()],合并成1个循环 //但是显然,取余操作效率太低,分开写更好一些 int limit = vertex.size() - 1; for (int i = 0; i < limit; i++) { _touchDraw->drawTriangle(pos, vertex[i], vertex[i + 1], Color4F::WHITE); } if(limit > 0) { _touchDraw->drawTriangle(pos, vertex[limit], vertex[0], Color4F::WHITE); } // 画三角形的边,Debug用 // for(auto v : vertex) { // _touchDraw->drawSegment(pos, v, 0.5f, Color4F::RED); // _touchDraw->drawDot(v, 3, Color4F::RED); // } }
void RotatingMultiCursor::chCursors(const vector<GradientCircleCursor>& cs_in){ cs = cs_in; initAngles(); update(); }