Anda di halaman 1dari 16

Triangulacin de Polgonos

comp-420

Triangulacin de polgonos
Triangulacin: descomposicin de un polgono simple en tringulos.
Cualquier triangulacin de un polgono simple con n vertices tiene n-2
tringulos.
El algoritmo ms simple, llamado de corte de orejas (ear clipping) es de
orden O(n2).
El mtodo antes visto para particionar polgonos en partes monotonas es
til porque podemos triangularlas con complejidad O(n).

Ear clipping triangulation


Una oreja de un polgono es un tringulo formado por tres vertices consecutivos
dentro del cual no hay otro vrtice del polgono.
El segmento de recta entre vi-1 e vi+1 se llama diagonal.
El vrtice vi se llama punta de la oreja.
Un tringulo consiste en una sola oreja aunque se puede poner como punta de la
oreja cualquiera de los tres vrtices.
Un polgono de cuatro o ms lados siempre tiene al menos dos orejas que no
traslapan ( probado por G.H. Meisters, Polygons have ears, Amer. Math. Monthly). Esto
sugiere un algoritmo recursivo para la triangulacin.
Si podemos encontrar una oreja en un polgono con n4 vertices y removerla,
tendremos un polgono de n-1 vertices y podemos repetir el proceso.
Una implementacion inmediata tomara O(n3).
D. Eberly. Geometric Tools. (www.geometrictools.com)

Ear clipping triangulation


Almacenar el polgono como una lista doblemente ligada de manera a poder eliminar
rapidamente las puntas de las orejas:
construir la lista toma O(n).
Iterar sobre los vertices para encontrar orejas.
para cada vrtice vi y su tringulo correspondiente (vi-1, vi, vi+1) (el indexado es
mdulo n por lo que vn=v0 y v-1 = vn-1) probar todos los otros vrtices para ver si
hay alguno dentro del tringulo.
Si no hay ninguno dentro del tringulo es una oreja, si hay al menos uno no lo es.
D. Eberly. Geometric Tools. (www.geometrictools.com)

Ear clipping triangulation


Es suficiente considerar solamente los vertices reflex
para ver si estan contenidos en el triangulo.
vertice reflex - el ngulo interior formado entre las
dos aristas que lo comparten es mayor a 180 grados.
vertex convex - el ngulo interior formado entre las
dos aristas que lo comparten es menor a 180 grados.
La estructura de datos de polgono mantiene 4 listas
doblemente ligadas simultneamente:
los vertices del polgono en una lista ligada cclica,
los vertices convex en una lista lineal,
los vertices reflex en una lista lineal,
las puntas de orejas en una lista ligada cclica.
D. Eberly. Geometric Tools. (www.geometrictools.com)

convex
reflex

Ear clipping triangulation


Una vez construida la lista inicial de vertices reflex y orejas, se eliminan las orejas una
a una.
si vi es una oreja que se elimina, la configuracion de la arista en los vertices
adyacentes puede cambiar. Si el vertice adyacente es...
convexo es facil convencerse que va a seguir siendo convexo.
oreja, no se queda oreja necesariamente despues de eliminar a vi.
reflex, es posible que se vuelva convex o posiblemente oreja despus de eliminar
a vi.
Despues de eliminar a vi, si un vertice adyacente es convex, probar si es una oreja
iterando sobre los vertices reflex y probando si estan contenidos en el triangulo de
ese vertex. Hay O(n) orejas, hay que verificar O(n) veces por oreja. El proceso total
de eliminacin de orejas toma O(n2).
D. Eberly. Geometric Tools. (www.geometrictools.com)

Ear clipping triangulation


6
9
5
8

Vertices reflex R = { 2, 5, 7, 8 }

Vertices convex C = { 0, 1, 3, 4, 6, 9 }

Vertices oreja E = { 3, 4, 6, 9 }
3

D. Eberly. Geometric Tools. (www.geometrictools.com)

Ear clipping triangulation


Vertices convex C = { 0, 1, 3, 4, 6, 9 }

6
9
5
8

Vertices reflex R = { 2, 5, 7, 8 }

Vertices oreja E = { 3, 4, 6, 9 }

2
3
1

Eliminar oreja 3 :
Primer triangulo en la triangulacin T0 =
2, 3, 4.

D. Eberly. Geometric Tools. (www.geometrictools.com)

Triangulacin de un polgono
monnoto
Sea P un polgono montono respecto a y con n vrtices.
Suponemos que P es estrictamente montono (que no contiene aristas
horizontales)
Siempre podemos ir hacia abajo al caminar en las fronteras izquierda o
derecha del polgono a partir del vrtice ms alto de P hasta el ms bajo.
Podemos seguir a P desde arriba hacia abajo en ambas cadenas en su
frontera agregando diagonales cuando sea posible.
Este es un algoritmo glotn: conectamos la mayor parte de vrtices
posible con diagonales en cada iteracin.

Triangulacin de un polgono
monnoto
El algoritmo maneja los vrtices en orden decreciente de su coordenada y.
Se requiere una estructura auxiliar: una pila (stack)
Inicialmente, esta pila est vaca, luego contiene los vrtices de P que se
han encontrado (al paso de la lnea de barrido) pero que necesitan ms
diagonales.
Al manejar un vrtice se le aaden tantas diagonales con los vrtices de la
pila como sea posible.
Estas diagonales dividen al polgono en tringulos.
El vrtice ms bajo ser el que est arriba en la pila.

Triangulacin de un polgono
monnoto

not contain horizontal edges. Thus we always go down when we walk on the
left or right boundary chain of P from the highest vertex of P to the lowest
POLYGON TRIANGULATION
one. This is the property that makes triangulating a monotone polygon easy:
top toque
bottom
both chains, adding
our way
through P from
La porcin de P que we
nocanhawork
sido
triangulada
pero
seonencuentra
arriba
diagonals whenever this is possible. Next we describe the details of this greedy
del ltimo vrtice visitado
tiene forma de embudo.
triangulation algorithm.
Chapter 3

triangles
split off

not yet
triangulated

popped

The algorithm handles the vertices in order of decreasing y-coordinate. If two


Una
frontera
dely-coordinate,
embudo
deoneparte
defirst.
una
vertices
have the same
thenconsta
the leftmost
is handled
The
algorithm
requires
a stack
sola
arista
de P
. S as auxiliary data structure. Initially the stack is
empty; later it contains the vertices of P that have been encountered but may
still need more diagonals. When we handle a vertex we add as many diagonals
Lafrom
otra
frontera
esonuna
cadena
queThese
consta
desplit
losoff
this vertex
to vertices
the stack
as possible.
diagonals
P. The (reflex),
vertices thates
have
been handled
but notcuyo
split offthe
triangles from
vrtices
reflejo
decir,
aquellos
ngulo
vertices on the stackare on the boundary of the part of P that still needs to be
interior
al menos
180.
triangulated.es
Thede
lowest
of these vertices,
which is the one encountered last, is
on top of the stack, the second lowest is second on the stack, and so on. The
P that
still needs
to bealto
triangulated,
and lies
above the last
that
part ofel
Solo
vrtice
ms
(el ms
profundo
envertex
la pila)
has been encountered so far, has a particular shape: it looks like a funnel turned
esupside
convexo.
down. One boundary of the funnel consists of a part of a single edge of
P, and the other boundary is a chain consisting of reflex vertices, that is, the
. Only the highest vertex, which is
interior
angle at these vertices
is at least 180durante
Esta
propiedad
se mantiene
toda la
at the bottom of the stack, is convex. This property remains true after we have
ejecucin
delvertex.
algoritmo
handled the next
Hence, it is (invariante).
an invariant of the algorithm.
Now, lets see which diagonals we can add when we handle the next vertex.
We distinguish two cases: v j , the next vertex to be handled, lies on the same
chain as the reflex vertices on the stack, or it lies on the opposite chain. If v j
lies on the opposite chain, it must be the lower endpoint of the single edge e

triangulation algorithm.

The algorithm handles the vertices in order of decreasing y-co


vertices have the same y-coordinate, then the leftmost one is h
algorithm requires a stack S as auxiliary data structure. Init
empty; later it contains the vertices of P that have been enco
still need more diagonals. When we handle a vertex we add as
from this vertex to vertices on the stack as possible. These di
triangles
triangles from P. The vertices that have been handled but n
manejar un vrtice vj distinguimos
entre dos casos.
split off
vertices on the stackare on the boundary of the part of P tha
triangulated. The lowest of these vertices, which is the one en
vj se encuentra en la misma cadena
queof los
vrtices
reflejo
dethe stack
on top
the stack,
the second
lowest(dentro
is second on
still needs to be triangulated,
and lies above th
part of P that
la pila). Se puede triangular a los vrtices
inmediatamente
superiores
has been encountered so far, has a particular shape: it looks lik
sobre la mismanotcadena.
upside down. One boundary of the funnel consists of a part of
yet
P, and the other boundary is a chain consisting of reflex ver
triangulated
vj se encuentra en la cadena opuesta
a los
reflejo
debe
elthe highes
interior
anglevrtices
at these vertices
is at least
180ser
. Only
at the
bottom
of the stack,
convex. This
property remains tr
vrtice inferior de la arista frontera
del
embudo.
Se ispueden
agregar
to Hence,
which aitdiagonal
was added
or, algorit
if no d
handled the last
nextvertex
vertex.
is an invariant
of the
diagonales a todos vrtices de la pilaNow,
excepto
estar
added,
it isal
theltimo
neighbor
ofque
v jwe
onya
the boundary
Psee
lets see
which
diagonals
can
add whenofwe
handlF
popped
conectado con vj.
We distinguish two cases: v j , the next vertex to be handled,
popped and
chain as the reflex vertices on pushed
the stack, or it lies on the oppo
lies on the opposite chain, it must be the lower endpoint of t
bounding the funnel. Due to the shape of the funnel, we can ad
popped
e
v j to all vertices currently on the stack,
except for the last one
at the bottom of the stack); the last vertex on the stack is the u
pushed
so it is already connected to v j . All these vertices are poppe
The untriangulated part of the polygon above v j is bounded
that connects v j to the vertex previously
on top of the stack a
vj
vj
popped and
extending downward from
this vertex, so it looks like a funnel
pushed
pushed
is preserved. This vertex and v j remain part of the not yet trian

Dnde agregar diagonales?


Al

pushed

pushed

Triangulacin de un polgono
monnoto
has been done we push v j onto the stack. In both cases the invariant is restored:
one side of the funnel is bounded by a part of a single edge, and the other side
is bounded by a chain of reflex vertices. We get the following algorithm. (The
algorithm is actually similar to the convex hull algorithm of Chapter 1.)

Algorithm T RIANGULATE M ONOTONE P OLYGON(P)


Input. A strictly y-monotone polygon P stored in a doubly-connected edge
list D.
Output. A triangulation of P stored in the doubly-connected edge list D.
1. Merge the vertices on the left chain and the vertices on the right chain of P
into one sequence, sorted on decreasing y-coordinate. If two vertices have
the same y-coordinate, then the leftmost one comes first. Let u1 , . . . , un
denote the sorted sequence.
2. Initialize an empty stack S, and push u1 and u2 onto it.
3. for j 3 to n 1
4.
do if u j and the vertex on top of S are on different chains
5.
then Pop all vertices from S.
Insert into D a diagonal from u j to each popped vertex,
6.
except the last one.
7.
Push u j1 and u j onto S.
8.
else Pop one vertex from S.
9.
Pop the other vertices from S as long as the diagonals from
u j to them are inside P. Insert these diagonals into D. Push
the last vertex that has been popped back onto S.
10.
Push u j onto S.
11. Add diagonals from un to all stack vertices except the first and the last one.
How much time does the algorithm take? Step 1 takes linear time and Step 2

the same side as the


the stack

0
1
2
3
4

5
7

10

11
12

13

14

15
16

18

17

19

21

20
22

23
24

1
2

2
3

3
4

7
8

10

10

11
12

13

14

11
12

13

15

14

16
18

17

15
16

18

17
19

19

20

21

20

21
22

22

23

23
24

24

1
2

2
3

3
4

7
8

10

10

11

11

12
14 15

13

12
14 15

13
16

16

18

17

18

17
19

21

19

20

21
22

20
22

23

23
24

24

Triangulacin de un polgono
monnoto
El paso 1 toma tiempo lineal
El paso 2 toma tiempo constante
El ciclo for se ejecuta n-3 veces, una ejecucin puede tomar tiempo lineal.
En cada ejecucin del ciclo for se insertan a lo ms dos vrtices en el
stack, entonces el nmero total de inserciones en el algoritmo est
acotado por 2n-4.
Como el nmero de operaciones pop no puede exceder el nmero de
operaciones push, el tiempo total de ejecuciones para el ciclo for es O(n).
El ltimo paso del algoritmo toma a lo ms tiempo lineal.
El algoritmo se ejecuta en O(n).

Anda mungkin juga menyukai