О // File clip.cpp

Nine void swap (int& a, int& b )
int c;

с = a; a = b; b = c;

Рис. 8.1

0011 0010 0110
0001 0000 0100
1001 1000 1100

Рис. 8.2

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

int outCode ( int x, int y, int X1, int Y1, int X2, int Y2 ) {
int code = 0;
if ( x < X1 )
code |= 0x01;
if ( y < Y1 )
code |= 0x02;
if ( x > X2 )
code |= 0x04;
if ( y > Y2 )
code |= 0x08;
return code;
}
void clipLine ( int x1, int y1, int x2, int y2, int X1, int Y1, int X2, int Y2 )
{
int codel = outCode ( x1, y1, X1, Y1, X2, Y2 ); int code2 = outCode ( x2, y2, X1, Y1, X2, Y2 ); int inside = ( codel I code2 ) == 0; int outside = ( codel & code2 ) != 0;
while ( loutside && linside ) {
if ( codel == 0 ) {
swap ( x1, x2 ); swap ( y1, y2 ); swap ( codel, code'2 );
}
if (codel &0x01 ) //clip left {
y1 += (long)(y2-y1)*(X1-x1)/(x2-x1); x1 =X1;
}
else
if ( codel & 0x02 ) // clip above {
x1 += (long)(x2-x1)*(Y1-y1)/(y2-y1); y1 =Y1;
}
else
if ( codel & 0x04 ) /7 clip right {
y1 += (long)(y2-y1)*(X2-x1)/(x2-x1); x1 =X2;
}
else
if ( codel & 0x08 ) // clip below

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

{

х1 += (long)(x2-x1)*(Y2-y1)/(y2-y1); у1 =Y2;

}

codel = outCode (х1, у1, Х1, Y1, Х2, Y2); code2 = outCode (х2, у2, Х1, Y1, Х2, Y2); inside = (codel I code2) == 0; outside = (codel & code2) != 0;

}
line ( x1, y1, x2, y2 ); }
Is

8.2. Классификация точки относительно отрезка Рассмотрим следующую задачу: на плоскости заданы точка и на-. правленный отрезок. Требуется определить положение точки относительно этого отрезка (рис. 8.3).

Возможными значениями являются LEFT (слева), RIGHT (справа), BEHIND (позади), BEYOND (впереди), BETWEEN (между), ORIGIN (начало) и DESTINATION (конец).

0 // File Vector2D.cpp #include "vector2d.h"
////////////////////// member functions //////////////////
int Vector2D :: classify ( Vector2D& p, Vector2D& q )
{
Vector2D a = q - p;
Vector2D b = *this - p;

float s = a.x * b.y - а.у * b.x;

if ( s > 0.0 ) return LEFT;
if ( s < 0.0 )

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