Beruflich Dokumente
Kultur Dokumente
Neste tópico falaremos sobre os diversos tipos de curvas feitos pelo TikZ, sendo eles os operadores:
to, bend, smooth e Bezier.
Nota: Use o KtikZ para fazer seus testes, por oferecer um preview em tempo real. Para isso, basta
digitar \begin{tikzpicture} e \end{tikzpicture} e começar seu desenho.
Rounded corners
Antes de iniciarmos nosso tutorial sobre curvas vejamos uma opção muito útil: rounded corners.
Esta opção permite que os cantos da figura sejam arredondados a partir de um valor numérico.
\ begin { tikzpicture }
\ draw (0 ,0) -- ++(5 ,0) -- ++( -2 ,2) -- ++( -3 ,0) -- cycle ;
\ draw [ rounded corners ] (6 ,0) -- ++(5 ,0) -- ++( -2 ,2) -- ++( -3 ,0) -- cycle ;
\ draw [ rounded corners =5 mm ] (12 ,0) -- ++(5 ,0) -- ++( -2 ,2) -- ++( -3 ,0) -- cycle ;
\ end { tikzpicture }
\ begin { tikzpicture }
\ draw (0 ,0) rectangle ++(2 ,2) ;
\ draw [ rounded corners ] (3 ,0) rectangle ++(2 ,2) ;
\ draw [ rounded corners =1 cm ] (6 ,0) rectangle ++(2 ,2) ;
\ end { tikzpicture }
180◦ 135◦
B
B B
90◦
45◦
A A A
Veja o código:
\ begin { tikzpicture }
\ tikzset { >= latex }
\ tikzstyle { ponto }=[ fill = blue , circle , scale =.25]
\ coordinate [ ponto , label = left : A ] ( A ) at (0 ,0) ;
\ coordinate [ ponto , label = left : B ] ( B ) at (2 ,3) ;
\ coordinate [ ponto , label = left : A ] ( C ) at (3 ,0) ;
\ coordinate [ ponto , label = below : B ] ( D ) at (5 ,3) ;
\ coordinate [ ponto , label = left : A ] ( E ) at (6 ,0) ;
\ coordinate [ ponto , label = below : B ] ( F ) at (8 ,3) ;
% to
\ draw ( A ) to ( B ) ;
\ draw [ - >] ( C ) to [ out =90 , in =180] ( D ) ;
\ draw [ - >] ( E ) to [ out =45 , in =135] ( F ) ;
\ end { tikzpicture }
G F
J
H E
A D
B C K
E o código:
\ begin { tikzpicture }
\ tikzstyle { ponto }=[ fill = blue , circle , scale =.25]
\ coordinate [ ponto , label = left : A ] ( A ) at (0 ,1) ;
\ coordinate [ ponto , label = below : B ] ( B ) at (1 ,0) ;
\ coordinate [ ponto , label = below : C ] ( C ) at (3 ,0) ;
\ coordinate [ ponto , label = right : D ] ( D ) at (4 ,1) ;
\ coordinate [ ponto , label = right : E ] ( E ) at (4 ,3) ;
\ coordinate [ ponto , label = above : F ] ( F ) at (3 ,4) ;
\ coordinate [ ponto , label = above : G ] ( G ) at (1 ,4) ;
\ coordinate [ ponto , label = left : H ] ( H ) at (0 ,3) ;
\ coordinate [ ponto , label = left : I ] ( I ) at (6 ,2) ;
\ coordinate [ ponto , label = above : J ] ( J ) at (8 ,3) ;
\ coordinate [ ponto , label = below : K ] ( K ) at (8 ,0) ;
% to
\ draw ( A ) to [ out = -90 , in =180] ( B ) ;
\ draw ( C ) to [ out =0 , in = -90] ( D ) ;
\ draw ( E ) to [ out =90 , in =0] ( F ) ;
\ draw ( G ) to [ out =180 , in =90] ( H ) ;
\ draw [ dashed ] ( B ) -- ( C ) ( D ) -- ( E ) ( F ) -- ( G ) ( H ) -- ( A ) ;
\ draw ( I ) to [ out =45 , in =225] ( J ) to [ out =45 , in =0] ( K ) to [ out =180 , in =225] ( I ) ;
\ end { tikzpicture }
\ begin { tikzpicture }
\ draw [ blue ] (0 ,0) to [ out =90 , in =225] (1 ,2) to [ out = -45 , in =90] (2 ,0) ;
\ draw [ brown ] (3 ,0) to [ out =90 , in = -90] (4 ,2) to [ out = -90 , in =90] (5 ,0) ;
\ end { tikzpicture }
Veja também os exemplos Clusters of atoms e Beamer arrows.
Bend
Existem dois operadores: bend left e bend right. Ambos são usados como opção para o operador
to. O bend left funciona da seguinte forma: se você digitar bend left=45, o resultado será equivalente
a out=45,in=135. Ou seja, out=<^ angulo>,in=180 - <^ angulo>. O bend right funciona de forma
contrária.
45◦ 90◦
A B C D E F
H J L N
G I
K M
\ begin { tikzpicture }
\ tikzstyle { ponto }=[ fill = blue , circle , scale =.25]
\ coordinate [ ponto , label = below : A ] ( A ) at (0 ,3) ;
\ coordinate [ ponto , label = below : B ] ( B ) at (2 ,3) ;
\ coordinate [ ponto , label = below : C ] ( C ) at (3 ,3) ;
\ coordinate [ ponto , label = below : D ] ( D ) at (5 ,3) ;
\ coordinate [ ponto , label = below : E ] ( E ) at (6 ,3) ;
\ coordinate [ ponto , label = below : F ] ( F ) at (8 ,3) ;
\ coordinate [ ponto , label = left : G ] ( G ) at (0 ,0) ;
\ coordinate [ ponto , label = left : H ] ( H ) at (0 ,2) ;
\ coordinate [ ponto , label = left : I ] ( I ) at (2 ,0) ;
\ coordinate [ ponto , label = left : J ] ( J ) at (2 ,2) ;
\ coordinate [ ponto , label = below : K ] ( K ) at (3 ,0) ;
\ coordinate [ ponto , label = right : L ] ( L ) at (5 ,2) ;
\ coordinate [ ponto , label = below : M ] ( M ) at (6 ,0) ;
\ begin { tikzpicture }
\ draw ( -1 ,0) -- (5 ,0) ;
% postes
\ fill (0 ,0) -- ++(0 ,2) -- ++( -.1 ,0) -- ++(0 ,.1) -- ++(.4 ,0) -- ++(0 , -.1) --
++( -.1 ,0) -- ++(0 , -2) -- cycle ;
\ fill (2 ,0) -- ++(0 ,2) -- ++( -.1 ,0) -- ++(0 ,.1) -- ++(.4 ,0) -- ++(0 , -.1) --
++( -.1 ,0) -- ++(0 , -2) -- cycle ;
\ fill (4 ,0) -- ++(0 ,2) -- ++( -.1 ,0) -- ++(0 ,.1) -- ++(.4 ,0) -- ++(0 , -.1) --
++( -.1 ,0) -- ++(0 , -2) -- cycle ;
% fios
\ draw [ blue ] (.25 ,2) to [ bend right ] (1.95 ,2) ;
\ draw [ blue ] (2.25 ,2) to [ bend right ] (3.95 ,2) ;
\ end { tikzpicture }
Veja mais exemplos do uso de bend em EPC Flow Charts e Mobile ad-hoc network.
No caso eu usei 4 pontos, mas pode ser qualquer quantidade a partir de 2 pontos.
Temos também a opção tension, que permite um ajuste na curva suavizando os cantos. Veja na
figura a seguir um exemplo com 4 pontos (esquerda) e um exemplo da mesma curva com várias tensões
diferentes.
tension=1.5
tension=1
B D F
tension=default
tension=0
A C E G
B F J
A C E G I K
D H L
\ begin { tikzpicture }
\ tikzstyle { ponto }=[ fill = blue , circle , scale =.25]
\ coordinate [ ponto , label = left : A ] ( A ) at (0 ,0) ;
\ coordinate [ ponto , label = above : B ] ( B ) at (1 ,1) ;
\ coordinate [ ponto , label = right : C ] ( C ) at (2 ,0) ;
\ coordinate [ ponto , label = below : D ] ( D ) at (1 , -1) ;
\ coordinate [ ponto , label = left : E ] ( E ) at (4 ,0) ;
\ coordinate [ ponto , label = above : F ] ( F ) at (5 ,1) ;
\ coordinate [ ponto , label = right : G ] ( G ) at (6 ,0) ;
\ coordinate [ ponto , label = below : H ] ( H ) at (5 , -1) ;
\ coordinate [ ponto , label = left : I ] ( I ) at (8 ,0) ;
\ coordinate [ ponto , label = above : J ] ( J ) at (9 ,1) ;
\ coordinate [ ponto , label = right : K ] ( K ) at (10 ,0) ;
\ coordinate [ ponto , label = below : L ] ( L ) at (9 , -1) ;
% smooth cycle
\ draw [ smooth cycle ] plot [ tension =0.2] coordinates {( A ) ( B ) ( C ) ( D ) };
\ draw [ smooth cycle , yshift = -2.25 cm ]
plot [ tension =0.5] coordinates {( E ) ( F ) ( G ) ( H ) };
\ draw [ smooth cycle , yshift = -4.5 cm ]
plot [ tension =1] coordinates {( I ) ( J ) ( K ) ( L ) };
% texto
\ node at (1 , -2) { tension =0.2};
\ node at (5 , -2) { tension =0.5};
\ node at (9 , -2) { tension =1};
\ end { tikzpicture }
bend right
out,in smooth
\ begin { tikzpicture }
\ tikzset { >= latex , inner sep =0 pt , outer sep =2 pt }
% bend
\ draw [ red ] (0 ,0) to [ bend right ] (2 ,2) ;
% out , in
\ draw [ green ] (0 ,0) to [ out =0 , in = -90] (2 ,2) ;
% smooth
\ draw [ blue , smooth , tension =.5] plot coordinates {(0 ,0) (1.75 ,.25) (2 ,2) }
% textos e flechas
\ draw [ - >] (.5 ,1.5) node [ above ] {\ tiny bend right }
to [ out = -90 , in =135] (1.3 ,.7) ;
\ draw [ - >] (0 ,1) node [ left ] {\ tiny out , in }
to [ out =0 , in =135] (1.2 ,.4) ;
\ draw [ - >] (2.5 ,1) node [ right ] {\ tiny smooth }
to [ out =180 , in =0] (1.9 ,.5) ;
% bolinhas azuis
\ fill [ blue ] (0 ,0) circle (1 pt ) ;
\ fill [ blue ] (1.75 ,.25) circle (1 pt ) ;
\ fill [ blue ] (2 ,2) circle (1 pt ) ;
\ end { tikzpicture }
Coordenadas Polares
Considere os pontos P1 e P2 da figura a seguir como os pontos de inı́cio e fim da curva, e considere
os pontos C1 e C2 como pontos de controle que definem a curvatura da curva. Suponha uma reta
suporte passando por P1 e C1 e outra passando por P2 e C2. Essas retas tangenciam a curva em
P1 e P2 definindo assim a curvatura da curva.
ponto de
Curva Bézier controle
C2
raio
ponto de
controle r2
C1 reta tangente a
r1 curva em P2
ponto ângulo
inicial α β
P1 P2
ponto
final
reta tangente a
curva em P1
A sintaxe para gerar uma curva Bézier usando coordenadas polares é a seguinte:
onde P1 é o ponto inicial, P2 é o ponto final, α e β são os ângulos de inclinação das retas tangentes
a P1 e P2, respectivamente; e r1 e r2 é o comprimento de cada reta tangente, ou seja, a distância de
P1 à C1 e P2 à C2, respectivamente.
C2
C1
45◦ 60◦
P1 P2
resultado final
A seguir um código mais completo com o uso de coordenadas previamente definidas e estilos para
visualização dos pontos. Mas lembrando que, o código anterior é suficiente para gerar a curva.
\ begin { tikzpicture }
% definicao de estilos
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = below : P 2] ( p 2) at (2 ,0) ;
\ coordinate [ controle , label = above : C 1] ( c 1) at (45:1) ;
\ coordinate ( c 2) at (60:2) ;
% coordenada necessaria para posicao correta do ponto cc 2 para desenhar a reta
tangente .
% Nao eh obrigatorio .
\ coordinate [ controle , , label = above : C 2 , xshift =4 cm ] ( cc 2) at ( c 2) ; % xshift com o
dobro da posicao do ponto .
% retas
\ draw [ blue ] ( p 1) -- ( c 1) ( p 2) -- ( cc 2) ;
% bezier
\ draw ( p 1) .. controls +( c 1) and +( c 2) .. ( p 2) ;
\ end { tikzpicture }
pontos de
controle
C1 C2
ponto
inicial Curva Bézier ponto
final
P1 P2
0 x
A sintaxe para gerar uma curva Bézier usando coordenadas cartesianas é a seguinte:
C1
P1 P2
resultado final
A seguir um código mais completo com o uso de coordenadas previamente definidas e estilos para
visualização dos pontos.
C1 C2
P1 P2
resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 ,0) grid (4 ,3) ;
C1 C2
P3
P1 P2
C3 C4 resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 , -2) grid (6 ,2) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = below left : P 2] ( p 2) at (3 ,0) ;
\ coordinate [ ponto , label = above : P 3] ( p 3) at (6 ,0) ;
C1 C2 C5 C6
P1 P2 P3 P4
C3 C4 resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 , -2) grid (9 ,2) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = below left : P 2] ( p 2) at (3 ,0) ;
\ coordinate [ ponto , label = below right : P 3] ( p 3) at (6 ,0) ;
\ coordinate [ ponto , label = below : P 4] ( p 4) at (9 ,0) ;
\ coordinate [ controle , label = above : C 1] ( c 1) at (1 ,1) ;
\ coordinate [ controle , label = above : C 2] ( c 2) at (2 ,1) ;
\ coordinate [ controle , label = below : C 3] ( c 3) at (4 , -1) ;
\ coordinate [ controle , label = below : C 4] ( c 4) at (5 , -1) ;
\ coordinate [ controle , label = above : C 5] ( c 5) at (7 ,1) ;
\ coordinate [ controle , label = above : C 6] ( c 6) at (8 ,1) ;
% retas
\ draw [ blue ] ( p 1) -- ( c 1) ( p 2) -- ( c 2) ( p 2) -- ( c 3) ( p 3) -- ( c 4) ( p 3) -- ( c 5)
( p 4) -- ( c 6) ;
% tres curvas bezier
\ draw ( p 1) .. controls ( c 1) and ( c 2) .. ( p 2)
.. controls ( c 3) and ( c 4) .. ( p 3)
.. controls ( c 5) and ( c 6) .. ( p 4) ;
\ end { tikzpicture }
P2 P2
C2 C2
C1 C1
P1 P1
resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 ,0) grid (2 ,4) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (1 ,0) ;
\ coordinate [ ponto , label = above : P 2] ( p 2) at (1 ,4) ;
\ coordinate [ controle , label = left : C 1] ( c 1) at (0 ,1) ;
\ coordinate [ controle , label = left : C 2] ( c 2) at (0 ,3) ;
% retas
\ draw [ red , dashed ] ( p 1) -- ( c 1) -- ( c 2) -- ( p 2) ;
% bezier
\ draw ( p 1) .. controls ( c 1) and ( c 2) .. ( p 2) ;
\ end { tikzpicture }
P3 P3
C4 C4
C3 C3
P2 P2
C2 C2
C1 C1
P1 P1
resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] ( -2 ,0) grid (2 ,6) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = above right : P 2] ( p 2) at (0 ,3) ;
\ coordinate [ ponto , label = above : P 3] ( p 3) at (0 ,6) ;
\ coordinate [ controle , label = right : C 1] ( c 1) at (1 ,1) ;
\ coordinate [ controle , label = right : C 2] ( c 2) at (1 ,2) ;
\ coordinate [ controle , label = left : C 3] ( c 3) at ( -1 ,4) ;
\ coordinate [ controle , label = left : C 4] ( c 4) at ( -1 ,5) ;
% retas
\ draw [ red , dashed ] ( p 1) -- ( c 1) -- ( c 2) -- ( p 2) -- ( c 3) -- ( c 4) -- ( p 3) ;
% bezier
\ draw ( p 1) .. controls ( c 1) and ( c 2) .. ( p 2)
.. controls ( c 3) and ( c 4) .. ( p 3) ;
\ end { tikzpicture }
P2
P1
C2 resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 , -1) grid (4 ,2) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = above : P 2] ( p 2) at (4 ,0) ;
\ coordinate [ controle , label = above : C 1] ( c 1) at (2 ,2) ;
\ coordinate [ controle , label = below : C 2] ( c 2) at (1 , -.5) ;
% retas
\ draw [ blue ] ( p 1) -- ( c 1) ( p 2) -- ( c 2) ;
% bezier
\ draw ( p 1) .. controls ( c 1) and ( c 2) .. ( p 2) ;
\ end { tikzpicture }
C2
C1
P1
resultado final
\ begin { tikzpicture }
\ tikzset { >= latex ,
ponto /. style ={ draw , fill = blue , rectangle , scale =.5} ,
controle /. style ={ draw , blue , circle , scale =.5}
}
% grade
\ draw [ color = lightgray , dashed ] (0 ,0) grid (2 ,3) ;
% coordenadas
\ coordinate [ ponto , label = below : P 1] ( p 1) at (0 ,0) ;
\ coordinate [ ponto , label = above : P 2] ( p 2) at (0 ,3) ;
\ coordinate [ controle , label = above : C 1] ( c 1) at (2 ,0) ;
\ coordinate [ controle , label = below : C 2] ( c 2) at (0 ,2) ;
% retas
\ draw [ blue ] ( p 1) -- ( c 1) ( p 2) -- ( c 2) ;
% bezier
\ draw ( p 1) .. controls ( c 1) and ( c 2) .. ( p 2) ;
\ end { tikzpicture }
P1
P1
Figura 23: Carro desenhado com smooth (à esquerda) e com Bézier (à direita).
\ begin { tikzpicture }
\ fill [ cyan ] (0 ,0) .. controls (3 ,0) and (8 , -5) .. (10 ,0)
.. controls (9 , -.5) and (8.5 ,.5) .. (8 ,0)
.. controls (8 ,.4) and (9.5 ,.3) .. (10 ,.2)
.. controls (8 ,3) and (0 ,2) .. ( -1 ,1.5)
.. controls ( -3 ,.7) and ( -2 ,0) .. ( -4 , -1)
.. controls ( -4 , -1) and ( -2 , -2) .. (0 ,0) ;
\ fill [ cyan ] (0 ,1.7) .. controls ( -1 ,3) and ( -2 ,3.5) .. ( -3 ,3.5)
.. controls ( -2.5 ,3) and ( -2.2 ,2) .. ( -1.5 ,1.2) ;
\ fill (8 ,.8) circle (1 mm ) ;
\ end { tikzpicture }
Preserve a natureza
Figura 30: Folha
\ begin { tikzpicture }[ x =2 pt , y =2 pt ]
\ fill [ green ](45.95 ,48.67) .. controls (45.95 ,48.67) and (46.45 ,13.05)
..(34.32 ,7.12)
.. controls (22.09 ,1.15) and (2.76 ,7.9) .. (12.04 ,27)
.. controls (18.9 ,41.13) and (38.33 ,32) .. (45.95 ,48.67) -- cycle ;
\ draw [ green !75! black , line width =2 pt ] (6.82 ,5.22) .. controls (9 ,5) and
(35.12 ,14.85) .. (41.95 ,36.7) ;
\ node at (20 ,0) {\ Large Preserve a natureza };
\ end { tikzpicture }
Veja três videos no youtube: bezier01, bezier02 e drawing android in LaTeX with KTikz.
Veja também o tópico Desenhando no TikZ com imagem de referência, que é uma continuação
deste tópico.