I am trying to draw a cannon with a superposition of a triangle, circle and rectangle. I need it rotated, so I realized that when I draw a rotated shape, it doesn't read the old coordinate system, but a new rotated one instead.
I thought that a simple math would keep track on the desired coordinate. So I defined \rotx
and \roty
functions to help in the transformations. When I do \rot{0}
, everything is fine. But when I do \rot{30}
or any other angle, the shape assumes undesired form.
My question is: how to handle rotated coordinates in Tikz?
My code as example:
\begin{tikzpicture}
%-> USEFUL DEFINITIONS
\def\fs{0.1} %... fence width and height
\def\th{2} %... tower height
\def\wh{1} %... wall height
\def\cp{4.5} %... canon position
\def\cr{0.2} %... canon radius
%-> x-rotation (#1=x, #2=y, $3=angle)$
\def\rotx#1#2#3{#1*cos(#3)+#2*sin(#3)}
\def\roty#1#2#3{-#1*sin(#3)+#2*cos(#3)}
\def\rot{30}
%----------> END DEFINITIONS <----------%
%-> DRAW THE FLOOR
\fill[gray!50]
(-5,-\fs) rectangle(2,0)
;
%-> DRAW THE CANNON
\fill[gray!50]
(-\cp-\cr,0)--
(-\cp+\cr,0)--
(-\cp,\cr)--cycle
(-\cp-\cr/2,3*\cr/2) circle(\cr)
;
\fill[gray!50,rotate=\rot]
({\rotx{-4.5}{0.2}{\rot}},{\roty{-4.5}{0.2}{\rot}})
rectangle({\rotx{-4}{0.6}{\rot}},{\roty{-4}{0.6}{\rot}})
% ({\rotx{-\cp}{3*\cr/2}{0}},
% {\roty{-\cp}{3*\cr/2}{0}})
% rectangle({\rotx{-\cp+3*\cr}{5*\cr/2}{0}},
% {\roty{-\cp+3*\cr}{5*\cr/2}{0}})
;
%-> DRAW THE CASTLE
\fill[gray!50]
%... tower
(0,0) rectangle(5*\fs,\th)
;
\foreach \x in {0,0.2,...,0.4}
\fill[gray!50]
(\x,\th) rectangle(\x+\fs,\th+\fs)
;
%... wall
\fill[gray!50]
(5*\fs,0) rectangle(20*\fs,\wh)
;
\foreach \x in {0.6,0.8,...,1.8}
\fill[gray!50]
(\x,\wh) rectangle(\x+\fs,\wh+\fs)
;
\end{tikzpicture}
transform shape
and nest the transformations in scopes (or, if you are confident enough, add them successively).\rotx
and\roty
are just coordinates of a given angle at an elliptic arc. This can be done much easier. Could you perhaps try to explain more precisely what you want? (And sorry, I will decouple soon but get back in 2h.) BTW, nice castle, +1 for that. ;-)