本文整理汇总了C++中Polygon2d类的典型用法代码示例。如果您正苦于以下问题:C++ Polygon2d类的具体用法?C++ Polygon2d怎么用?C++ Polygon2d使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Polygon2d类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: barycenter
// http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
vec2 barycenter(const Polygon2d& P) {
gx_assert(P.size() > 0) ;
double A = signed_area(P) ;
if(::fabs(A) < 1e-30) {
return P[0] ;
}
double x = 0.0 ;
double y = 0.0 ;
for(unsigned int i=0; i<P.size(); i++) {
unsigned int j = (i+1) % P.size() ;
const vec2& t1 = P[i] ;
const vec2& t2 = P[j] ;
double d = (t1.x * t2.y - t2.x * t1.y) ;
x += (t1.x + t2.x) * d ;
y += (t1.y + t2.y) * d ;
}
return vec2(
x / (6.0 * A),
y / (6.0 * A)
) ;
}
示例2: SingleTileCollision
bool GD_EXTENSION_API SingleTileCollision(std::map<gd::String, std::vector<RuntimeObject*>*> tileMapList,
int layer,
int column,
int row,
std::map<gd::String, std::vector<RuntimeObject*>*> objectLists,
bool conditionInverted)
{
return TwoObjectListsTest(tileMapList, objectLists, conditionInverted, [layer, column, row](RuntimeObject* tileMapObject_, RuntimeObject * object) {
RuntimeTileMapObject *tileMapObject = dynamic_cast<RuntimeTileMapObject*>(tileMapObject_);
if(!tileMapObject || tileMapObject->tileSet.Get().IsDirty())
return false;
//Get the tile hitbox
int tileId = tileMapObject->tileMap.Get().GetTile(layer, column, row);
if(tileId < 0 || tileId >= tileMapObject->tileSet.Get().GetTilesCount())
return false;
Polygon2d tileHitbox = tileMapObject->tileSet.Get().GetTileHitbox(tileId).hitbox;
tileHitbox.Move(tileMapObject->GetX() + column * tileMapObject->tileSet.Get().tileSize.x,
tileMapObject->GetY() + row * tileMapObject->tileSet.Get().tileSize.y);
//Get the object hitbox
std::vector<Polygon2d> objectHitboxes = object->GetHitBoxes();
for(std::vector<Polygon2d>::iterator hitboxIt = objectHitboxes.begin(); hitboxIt != objectHitboxes.end(); ++hitboxIt)
{
if(PolygonCollisionTest(tileHitbox, *hitboxIt).collision)
{
return true;
}
}
return false;
});
}
示例3: GenerateHitboxes
std::vector<Polygon2d> GenerateHitboxes(TileSet &tileSet, TileMap &tileMap)
{
std::vector<Polygon2d> hitboxes;
const int tileWidth = tileSet.tileSize.x;
const int tileHeight = tileSet.tileSize.y;
if(tileSet.IsDirty())
return hitboxes;
for(int layer = 0; layer < 3; layer++)
{
for(int col = 0; col < tileMap.GetColumnsCount(); col++)
{
for(int row = 0; row < tileMap.GetRowsCount(); row++)
{
//Note : a hitbox is also added for empty/non-collidable tiles to ease the hitbox update when changing a tile
Polygon2d newPolygon;
if(tileMap.GetTile(layer, col, row) != -1 && tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).collidable)
{
newPolygon = tileSet.GetTileHitbox(tileMap.GetTile(layer, col, row)).hitbox;
}
newPolygon.Move(col * tileWidth, row * tileHeight);
hitboxes.push_back(newPolygon);
}
}
}
return hitboxes;
}
示例4: GetHeight
std::vector<Polygon2d> RuntimeObject::GetHitBoxes() const {
std::vector<Polygon2d> mask;
Polygon2d rectangle = Polygon2d::CreateRectangle(GetWidth(), GetHeight());
rectangle.Rotate(GetAngle() / 180 * 3.14159);
rectangle.Move(GetX() + GetCenterX(), GetY() + GetCenterY());
mask.push_back(rectangle);
return mask;
}
示例5: minimum_area_enclosing_rectangle
void minimum_area_enclosing_rectangle(
const Polygon2d& PP,
vec2& S, vec2& T
) {
// Note: this implementation has O(n2) complexity :-(
// (where n is the number of vertices in the convex hull)
// If this appears to be a bottleneck, use a smarter
// implementation with better complexity.
Polygon2d P ;
convex_hull(PP, P) ;
int N = P.size() ;
// Add the first vertex at the end of P
P.push_back(P[0]) ;
double min_area = Numeric::big_double ;
for(int i=1; i<=N; i++) {
vec2 Si = P[i] - P[i-1] ;
if( ( Si.length2() ) < 1e-20) {
continue ;
}
vec2 Ti(-Si.y, Si.x) ;
normalize(Si) ;
normalize(Ti) ;
double s0 = Numeric::big_double ;
double s1 = -Numeric::big_double ;
double t0 = Numeric::big_double ;
double t1 = -Numeric::big_double ;
for(int j=1; j<N; j++) {
vec2 D = P[j] - P[0] ;
double s = dot(Si, D) ;
s0 = gx_min(s0, s) ;
s1 = gx_max(s1, s) ;
double t = dot(Ti, D) ;
t0 = gx_min(t0, t) ;
t1 = gx_max(t1, t) ;
}
double area = (s1 - s0) * (t1 - t0) ;
if(area < min_area) {
min_area = area ;
if((s1 - s0) < (t1 - t0)) {
S = Si ;
T = Ti ;
} else {
S = Ti ;
T = Si ;
}
}
}
}
示例6: convex_clip_segment
void convex_clip_segment(
Segment2d& S, const Polygon2d& window
) {
gx_parano_assert(polygon_is_convex(window)) ;
bool invert = (signed_area(window) < 0) ;
for(unsigned int i=0; i<window.size(); i++) {
unsigned int j = ((i+1) % window.size()) ;
clip_segment_by_half_plane(S, window[i], window[j], invert) ;
}
}
示例7: signed_area
// http://astronomy.swin.edu.au/~pbourke/geometry/polyarea/
double signed_area(const Polygon2d& P) {
double result = 0 ;
for(unsigned int i=0; i<P.size(); i++) {
unsigned int j = (i+1) % P.size() ;
const vec2& t1 = P[i] ;
const vec2& t2 = P[j] ;
result += t1.x * t2.y - t2.x * t1.y ;
}
result /= 2.0 ;
return result ;
}
示例8:
Polygon2d Polygon2d::scaleaboutcentroid(float scale) {
Polygon2d n;
V2d cen=centroid();
for (int i=1;i<=vs.len;i++) {
V2d x=vs.num(i);
x=x-cen;
x=x*scale;
x=x+cen;
n.add(x);
}
return n;
}
示例9: save_polygon
void save_polygon(const Polygon2d& P, const std::string& file_name) {
std::ofstream out(file_name.c_str()) ;
{for(unsigned int i=0; i<P.size(); i++) {
out << "v " << P[i].x << " " << P[i].y << std::endl ;
out << "vt " << P[i].x << " " << P[i].y << std::endl ;
}}
out << "f " ;
{for(unsigned int i=0; i<P.size(); i++) {
out << i+1 << "/" << i+1 << " " ;
}}
out << std::endl ;
}
示例10: vertices_barycenter
vec2 vertices_barycenter(const Polygon2d& P) {
gx_assert(P.size() != 0) ;
double x = 0 ;
double y = 0 ;
for(unsigned int i=0; i<P.size(); i++) {
x += P[i].x ;
y += P[i].y ;
}
x /= double(P.size()) ;
y /= double(P.size()) ;
return vec2(x,y) ;
}
示例11: clip_polygon_by_half_plane
void clip_polygon_by_half_plane(
const Polygon2d& P,
const vec2& q1,
const vec2& q2,
Polygon2d& result,
bool invert
) {
result.clear() ;
if(P.size() == 0) {
return ;
}
if(P.size() == 1) {
if(point_is_in_half_plane(P[0], q1, q2, invert)) {
result.push_back(P[0]) ;
}
return ;
}
vec2 prev_p = P[P.size() - 1] ;
Sign prev_status = point_is_in_half_plane(
prev_p, q1, q2, invert
) ;
for(unsigned int i=0; i<P.size(); i++) {
vec2 p = P[i] ;
Sign status = point_is_in_half_plane(
p, q1, q2, invert
) ;
if(
status != prev_status &&
status != ZERO &&
prev_status != ZERO
) {
vec2 intersect ;
if(intersect_segments(prev_p, p, q1, q2, intersect)) {
result.push_back(intersect) ;
} else {
}
}
switch(status) {
case NEGATIVE:
break ;
case ZERO:
result.push_back(p) ;
break ;
case POSITIVE:
result.push_back(p) ;
break ;
}
prev_p = p ;
prev_status = status ;
}
}
示例12: convex_hull
void convex_hull(const Polygon2d& PP, Polygon2d& result) {
result.clear() ;
int n = PP.size() ;
vec2* P = new vec2[n+1] ;
{ for(int i=0; i<n; i++) {
P[i] = PP[i] ;
}}
int u = make_chain(P, n, cmpl);
P[n] = P[0];
int ch = u+make_chain(P+u, n-u+1, cmph);
{for(int i=0; i<ch; i++) {
result.push_back(P[i]) ;
}}
delete[] P ;
}
示例13: polygon_is_convex
bool polygon_is_convex(const Polygon2d& P) {
Sign s = ZERO ;
for(unsigned int i=0; i<P.size(); i++) {
unsigned int j = ((i+1) % P.size()) ;
unsigned int k = ((j+1) % P.size()) ;
Sign cur_s = orient(P[i],P[j],P[k]) ;
if(s != ZERO && cur_s != ZERO && cur_s != s) {
return false ;
}
if(cur_s != ZERO) {
s = cur_s ;
}
}
return true ;
}
示例14:
PolygonShape::PolygonShape(const Polygon2d& polygon, Material& attrs):
m_polygon(polygon) {
b2Vec2* vertecies = new b2Vec2[polygon.VerteciesCount()];
const Vertecies& polygonVertecies = polygon.GetVertecies();
for (size_t vertexNo = 0; vertexNo < polygonVertecies.size(); ++vertexNo) {
vertecies[vertexNo] = ToBox2dVec(polygonVertecies[vertexNo]);
}
m_polygonShapePtr = new b2PolygonShape();
m_polygonShapePtr->Set(vertecies, polygon.VerteciesCount());
delete[] vertecies;
Construct(m_polygonShapePtr, attrs);
}
示例15: point_is_in_kernel
bool point_is_in_kernel(const Polygon2d& P, const vec2& p) {
Sign sign = ZERO ;
for(unsigned int i=0 ; i<P.size() ; i++) {
unsigned int j = (i+1) % P.size() ;
const vec2& p1 = P[i] ;
const vec2& p2 = P[j] ;
Sign cur_sign = orient(p, p1, p2) ;
if(sign == ZERO) {
sign = cur_sign ;
} else {
if(cur_sign != ZERO && cur_sign != sign) {
return false ;
}
}
}
return true ;
}