#define EPS 1 e-7
#define MAX_VERTICES 100

inline void addVertexToOutPolygon ( Vector2D * outPolygon, int& outVertices, int lastVertex, float x, float у )

{

if ( outVertices == 0 И

(fabs (x - outPolygon [outVertices- 1].x) > EPS || fabs (y - outPolygon [outVertices-1].y) > EPS ) && (llastVertex || fabs (x - outPolygon [0].x) > EPS || fabs (y - outPolygon [0].y) > EPS ))
{

outPolygon [outVertices].x = x; outPolygon [outVertices],у = у; outVertices++;

}

8. Основные алгоритмы вычислительной геометр

}
Polygon * clipPolygon (const Polygon& poly, const Polygon& clipPoly) {
Vector2D outPolyl [MAX_VERTICES]; Vector2D outPoly2 [MAX_VERTICES]; Vector2D * inPolygon = poly.vertices; Vector2D * outPolygon = outPolyl; int outVertices = 0; jnt inVertices = poly.numVertices;
for (int edge = 0; edge < clipPoly.numVertices; edge++ ) {
int lastVertex = 0; // is this the last vertex
int next = edge == clipPoly.numVertices-1 ? 0 : edge + 1;
float px = inPolygon [0].x;
float py = inPolygon [0].y;
float dx = clipPoly.vertices [next].x -
clipPoly.vertices [edge].x; float dy = clipPoly.vertices [next].y -
clipPoly.vertices [edge].y; int prevVertexInside = (px-clipPoly.vertices [edge].x)*dy
- (py - clipPoly.vertices [edge].y)*dx > 0; int intersectionCount = 0;
outVertices = 0;
for (int i = 1; i <= inVertices; i++ ) {
float cx, cy;
if (i < inVertices ) {
cx = inPolygon [i].x; cy = inPolygon [ij.y;
}
else
{
cx = inPolygon [0].x; cy = inPolygon [0].y; lastVertex = 1;
}
// if starting vertex is visible then put it // into the output array if (prevVertexInside )
addVertexToOutPolygon (outPolygon, outVertices, lastVertex, px, py );
int curVertexlnside =
(cx-clipPoly.vertices [edge].x)*dy -(cy - clipPoly.vertices [edge].y)*dx > 0;
// if vertices are on different sides of the edge // then look where we're intersecting if ( prevVertexInside != curVertexlnside )

Компьютерная графика. Полигональные модели

double denominator = (cx-px)*dy - (cy-py)*dx;
if ( denominator != 0.0 ) {

⇐ Предыдущая| |Следующая ⇒