void Server::GrahamScan(std::vector<Point> p) { std::vector<point2d> points; std::vector<point2d> convex_hull; for (unsigned int i=0; i<p.size();i++){ point2d tmp_pnt; tmp_pnt.x = p.at(i).x(); tmp_pnt.y = p.at(i).y(); points.push_back(tmp_pnt); } GrahamScanConvexHull()(points, convex_hull); for(unsigned int i=0; i<convex_hull.size();i++){ this->addVertex(Point(convex_hull.at(i).x,convex_hull.at(i).y),true); } }
int Physics::newPolygonShape(lua_State * L) { int argc = lua_gettop(L); int vcount = (int)(argc-1)/2; // 1 body + 3 vertices love::luax_assert_argc(L, 1 + (2 * 3)); Body * b = luax_checkbody(L, 1); b2PolygonDef def; def.friction = 0.5f; def.restitution = 0.1f; def.density = 1.0f; std::vector<point2d> points(def.vertexCount); std::vector<point2d> convex_hull; for(int i = 0;i<vcount;i++) { float x = (float)lua_tonumber(L, -2); float y = (float)lua_tonumber(L, -1); point2d tmp(x, y); points.push_back(tmp); lua_pop(L, 2); } // Compute convex hull. GrahamScanConvexHull()(points, convex_hull); def.vertexCount = (int32)convex_hull.size(); if(def.vertexCount < 3) return luaL_error(L, "Polygon degenerated to less than three points."); for(int i = 0;i<def.vertexCount;i++) def.vertices[def.vertexCount-i-1].Set((float)convex_hull[i].x, (float)convex_hull[i].y); PolygonShape * p = new PolygonShape(b, &def); luax_newtype(L, "PolygonShape", LOVE_PHYSICS_POLYGON_SHAPE_BITS, (void*)p); return 1; }