TikZ: Drawing a rectangle along a sloped line

  • Follow


Hello,

I need to draw a rectangle along a sloped line (B to C) where the slope
degree needs computing.

I can't think of any existing TikZ library for doing (suggestions
welcome) this so I come up with the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \path (0,0) coordinate (B) (2,2) coordinate (C);
  \newcount\mydegree
  \path let \p1 = ($ (C) - (B) $),
    \n2 = {atan(\y1/\x1)}
  in
    \global\mydegree=\n2
  ;
\end{tikzpicture}
\end{document}

Unfortunately, this does not work. Any idea how to fix it?

Some suggested

  \path let \p1 = ($ (C) - (B) $),
    \n2 = {atan(\y1/\x1)}
  in
    \xdef\mydegree{\n2}
  ;

But it does not compile either.

If I do the drawings right insight the let scope, it works but I don't
know how to have more than one distinct path actions (draw, fill,
filldraw etc.) in one let scope.

Best wishes,
Leo
0
Reply Leo 12/30/2009 9:34:08 PM

On Dec 30, 9:34=A0pm, Leo <sdl....@gmail.com> wrote:

> I need to draw a rectangle along a sloped line (B to C) where the slope
> degree needs computing.
Howsagoin,

Is this what you are looking for?

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  % Sloped line.
  \draw[red]
       (0,0) coordinate (start) --
       (3,2) coordinate (end);
  \draw[blue]
        ($(start)!0.6!(end)$) coordinate (point 1)--
        ($(start)!0.8!(end)$) coordinate (point 2)--
        ($(point 2)!1cm!90:(end)$) coordinate --
        ($(point 1)!1cm!90:(end)$) coordinate --
        cycle;
\end{tikzpicture}
\end{document}

Regards,


Marc van Dongen
0
Reply Marc 12/31/2009 1:47:13 PM


On 31.12.2009 14:47, Marc van Dongen wrote:
> On Dec 30, 9:34 pm, Leo<sdl....@gmail.com>  wrote:
>
>> I need to draw a rectangle along a sloped line (B to C) where the slope
>> degree needs computing.
> Howsagoin,
>
> Is this what you are looking for?
>
> \documentclass{article}
>
> \usepackage{tikz}
> \usetikzlibrary{calc}
>
> \begin{document}
> \begin{tikzpicture}
>    % Sloped line.
>    \draw[red]
>         (0,0) coordinate (start) --
>         (3,2) coordinate (end);
>    \draw[blue]
>          ($(start)!0.6!(end)$) coordinate (point 1)--
>          ($(start)!0.8!(end)$) coordinate (point 2)--
>          ($(point 2)!1cm!90:(end)$) coordinate --
>          ($(point 1)!1cm!90:(end)$) coordinate --
>          cycle;
> \end{tikzpicture}
> \end{document}
>
> Regards,
>
>
> Marc van Dongen

if the problem is just draw rectangle on sloped line, there is another 
solution:

\documentclass{article}
\usepackage{tikz}% tested with recent CVS version

	\begin{document}
     	\begin{tikzpicture}
\draw (0,0) -- (3,2) node[rectangle,draw=blue, text width = 11ex, 
align=center, pos=.3,above,sloped]{line angle is 
\pgfmathatan{2/3}\pgfmathresult};
	\end{tikzpicture}
\end{document}

regards, zarko
0
Reply Zarko 12/31/2009 6:22:15 PM

On Dec 31 2009, 6:22=A0pm, "Zarko F. Cucej" <zarko.cu...@gmail.com>
wrote:

> if the problem is just draw rectangle on sloped line, there is another
> solution:

[snip]
> \draw (0,0) -- (3,2) node[rectangle,draw=3Dblue, text width =3D 11ex,
> align=3Dcenter, pos=3D.3,above,sloped]{line angle is \pgfmathatan{2/3}\pg=
fmathresult};
[snip]

That's a bit simpler.

Regards,


Marc van Dongen
0
Reply Marc 1/1/2010 10:12:58 AM

Hi,

You can use the markings decoration to put some drawing at any point
along the path. The drawing can be thought of as a little picture
(technically it is a scope not a picture) where the x axis is the
tangent to the path. As usual a lot of the decoration stuff can be
tied up in a style:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}

\tikzset{
	sloped picture/.style=3D{
		decoration=3D{
			markings,
			mark=3Dat position 0.5 with	{#1}
		},
		decorate
	}
}

\begin{tikzpicture}
	\draw [help lines] grid (3,2);
	\coordinate (A) at (0,0);
	\coordinate (B) at (3,2);
	\draw (A) -- (B);
	\path	(A) -- (B)
	[
		sloped picture=3D{
			\draw [red] (0,0) rectangle (1,1);
		}
	];
\end{tikzpicture}
\end{document}

Regards

Mark





On Dec 30 2009, 9:34=A0pm, Leo <sdl....@gmail.com> wrote:
> Hello,
>
> I need to draw a rectangle along a sloped line (B to C) where the slope
> degree needs computing.
>
> I can't think of any existing TikZ library for doing (suggestions
> welcome) this so I come up with the following:
>
> \documentclass{article}
> \usepackage{tikz}
> \usetikzlibrary{calc}
>
> \begin{document}
> \begin{tikzpicture}
> =A0 \path (0,0) coordinate (B) (2,2) coordinate (C);
> =A0 \newcount\mydegree
> =A0 \path let \p1 =3D ($ (C) - (B) $),
> =A0 =A0 \n2 =3D {atan(\y1/\x1)}
> =A0 in
> =A0 =A0 \global\mydegree=3D\n2
> =A0 ;
> \end{tikzpicture}
> \end{document}
>
> Unfortunately, this does not work. Any idea how to fix it?
>
> Some suggested
>
> =A0 \path let \p1 =3D ($ (C) - (B) $),
> =A0 =A0 \n2 =3D {atan(\y1/\x1)}
> =A0 in
> =A0 =A0 \xdef\mydegree{\n2}
> =A0 ;
>
> But it does not compile either.
>
> If I do the drawings right insight the let scope, it works but I don't
> know how to have more than one distinct path actions (draw, fill,
> filldraw etc.) in one let scope.
>
> Best wishes,
> Leo

0
Reply vibrovski 1/1/2010 8:50:53 PM

Thank you all for the fine answers.

Best,

Leo
0
Reply Leo 1/1/2010 10:12:48 PM

On Jan 1, 8:50=A0pm, vibrovski <vibrov...@googlemail.com> wrote:

> [ sloped picture ]

Thanks. That's a very nice feature. (I had to remove the blank line in
the \tikzset command to get it working.)

I looked for sloped in the [June 2, 2009 CVS manual] but couldn't find
anything  n combination with sloped. Is this documented in a more
recent manual?

Regards,


Marc van Dongen

0
Reply Marc 1/2/2010 12:16:44 PM

6 Replies
1304 Views

(page loaded in 0.105 seconds)

Similiar Articles:











7/23/2012 9:18:06 AM


Reply: