2

enter image description here

I'm not getting how I can connect the tilted rectangle center to my origin (left corner of the rectangle). Help me pls

\begin{figure}[!h]
    \centering
    \begin{tikzpicture}
        \draw[black,thick] (0,0) -- (5,0);
        \draw[black,thick]  (0,0) -- (0,3);
        \draw[black,thick]  (0,3) -- (5,0);
        % Adding the angle
        \draw (3.5,0) arc (180:135:1) node[midway,anchor=east,black] {$\theta$};
        \draw[rotate around={-30.96:(1.5,2.1)}] (1.5,2.1) rectangle (3,3);
        \draw[rotate around={-30.96:(1.5,2.1)},black,->,thick] (2.25,2.55) -- (2.25,3.55)node[black,anchor=west] {$\vec{N}$};
        \draw[rotate around={-30.96:(1.5,2.1)}, black, ->, thick]
        (2.25,2.55) -- ({2.25 + sin(30.96)}, {2.55 - cos(30.96}) node[black,anchor= west] {$\vec{P}$};
        \draw[rotate around={-30.96:(1.5,2.1)},black,->,thick] (2.25,2.55) -- (1.25,2.55) node[black,anchor=south east] {$\vec{F}_a$};
        \draw[blue,->,rotate around={-30.96:(1.5,2.1)}] (4,3) -- (5,3) node[black,anchor=north west] {$x$};
        \draw[blue,->,rotate around={-30.96:(1.5,2.1)}] (4,3) -- (4,4) node[black,anchor=west] {$y$};
        \draw[red,dashed,thick,rotate around={-30.96:(1.5,2.1)}]
        (2.25,2.55) -- ({sin(30.96)},{-cos(30.96)});%just a try
    \end{tikzpicture}
\end{figure}
Sandy G
  • 42,558
Rui Silva
  • 19
  • 1
  • 1
    Welcome. TikZ has polar and relatives coordinates. You can replace (2.25,2.55) -- ({2.25 + sin(30.96)}, {2.55 - cos(30.96}) with (2.25,2.55) -- + (180-30.96:1), I believe. – Qrrbrbirlbel Aug 17 '23 at 23:06
  • Thanks for posting some code this time, but can you please post a complete MWE (minimal working example) we can copy-paste-compile? – cfr Aug 18 '23 at 04:44

2 Answers2

5

I think, a pic could help you here. Let TikZ do the trigonometry for you.

When a pic is placed along a path segment with the sloped option, the drawing of the pic will be rotated and shifted so that its x axis is tangential to the path at that point. Its origin also lies on that path.

Unfortunately, some of these settings also apply for the paths inside the pic which is why we need to reset them again (that's why you see sloped = false inside them).

For the angle markings, I'm also going to use a pic but that is not placed along the path, it uses the three coordinates given and calculates the needs angles.
I'm not very happy about its implementation which is why added the shortening amount to its definition so that the lines won't cross our triangles. (Other better but longer solutions exist for this as well.)

I'm also naming the coordinates so I can actually reference them later without having to worry about transformations.


Let's create this diagram step by step. First, we start with just the triangle and naming its corners:

\draw (0, 0) coordinate (O)
   -- (0, 3) coordinate (A)
   -- (5, 0) coordinate (B)
   -- cycle;

Notice that this one path and a closed on (that's the cycle's doing). The corners will look differently than with your code.

We can place a pic along the AB edge by placing it either directly after the -- (i.e. “implicitly”) or after the target coordinate “explicitly“ with a position specified.

Since I'm using sloped here, the pic is placed so that it is rotated so that its x axis follows the slope of the line.

Now, I'm just drawing the box and the vector lines:

\draw[midway, thin] (left:.75) rectangle coordinate () +(1.5,.9);
\path[->, at end, sloped=false]
              () edge["\vec F_a" above left]            +(left:1)
              () edge["\vec N"   right]                 +(  up:1)
   [reset cm] () edge["\vec P"   right] coordinate (P') +(down:1);

Since I've named the pic box all coordinates and nodes named inside the pic will get box prefixed. Thus, the coordinate in the center of the rectangle I draw with the first path which is just named nothing will actually be named box.

Now, since the pic is rotated the direction up and left art orthogonally and parallel to the line we place the box on.

For the truely vertical arrow P I use reset cm to reset the transformation matrix. This will not necessary enact the coordinate system that was in effect before we entered the pic but in this case it is the same.

The second pic is placed very similar but draws just a coordinate system. Since y = 0 is on the line I'll draw it between y = 1 and y = 2 to have it float above the line.

Now, it's just the three angle pics.

And the additional orange dashed lines. This are placed behind path because I think they look better if they are behind the triangle lines.

To draw the vertical dashed line I added a coordinate P' (which will be named boxP') at the tip of the vertical vector.

The coordinate specification (boxP'|-O) specifies the intersection between a vertical line through boxP' and a horizontal line through O.


If you need to draw more than one such diagrams, it is recommended to define your own named pic. In this case, we could do something like

\tikzset{
  pic node/left/.style=above left,
  pic node/up/.style=right,
  pic node/right/.style=below right,
  pic node/vertical/.style=right,
  pics/box with vectors/.style args={#1:#2:#3}{code={
    \draw[midway, thin] (left:.75) rectangle coordinate () +(1.5,.9);
    \path[->, at end, sloped=false, pic actions]
                  () edge["{\vec #1}" pic node/left]     +(left:1)
                  () edge["{\vec #2}" pic node/up  ]     +(  up:1)
       [reset cm] () edge["{\vec #3}" pic node/vertical]
                                                 coordinate (P') +(down:1);}},
  pics/coordinate system/.default={x:y},
  pics/coordinate system/.style args={#1:#2}{code={
    \draw[<->, sloped=false, pic actions]
      (up:1) |- node[at start, pic node/up]{$#2$}
                node[at end,   pic node/right]{$#1$} (right:1);}}}

and on the path, you just do

pic[pos=.4, sloped] (box)                        {box with vectors=F_a:N:P}
pic[near end, sloped, thin, Green, shift=(up:1)] {coordinate system}

while having the various pic node/… styles available to adjust the nodes' positions or appearance.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{angles, arrows.meta, quotes}
\tikzset{math nodes/.style={execute at begin node=$, execute at end node=$}}
\colorlet{Green}{green!75!black}
\begin{document}
\tikz[
  pics/angle/.append style={/tikz/shorten >=+.4pt, /tikz/shorten <=+.4pt},
  >={Stealth[round]}, % I like this arrow tip better
  thick, angle radius=.75cm, angle eccentricity=1.2,
  every edge quotes/.append style={math nodes},
  every pic quotes/.append style={math nodes, node font=\footnotesize}]
\draw (0, 0) coordinate (O)
   -- (0, 3) coordinate (A)
   -- (5, 0) coordinate (B)
  pic[pos=.4, sloped] (box) {code={
    \draw[midway, thin] (left:.75) rectangle coordinate () +(1.5,.9);
    \path[->, at end, sloped=false]
                  () edge["\vec F_a" above left]            +(left:1)
                  () edge["\vec N"   right]                 +(  up:1)
       [reset cm] () edge["\vec P"   right] coordinate (P') +(down:1);
  }}
  pic[near end, sloped, thin, Green] {code={% yeah, the same trick again
    \draw[<->, sloped=false] (0,2) to[at start, "y" right]       ++( down:1)
                                   to[at end,   "x" below right] ++(right:1);
  }}
  -- cycle
  %
  pic[draw, Green, thin, "\theta"]                     {angle=A--B--O}
  pic[draw, blue,  thin, "\theta"]                     {angle=O--box--boxP'}
  pic[draw, blue,  thin, "\frac{\pi}{2}-\theta" right] {angle=B--O--box}
  % I think the dashed lines look better if they're behind the other stuff
  [behind path]
    (box)   edge[orange, densely dashed] (O)
    (boxP') edge[orange,         dashed] (boxP'|-O)
  ;
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
  • Your code is givving me an error, an undefined control sequence because of some braces. And I'm new to tikz, I don't undersantd what you did at all? Could you do a simpler version like me? Or explain me your's? – Rui Silva Aug 18 '23 at 00:35
  • 1
    Interesting. I didn't know you could use pics like that. @RuiSilva Did you try compiling the code on its own in a new document? It compiles fine for me. If you're using babel for a language with accents, you might need to add the babel library to the list of tikz libraries. (Don't know if you'd get that error particularly, just guessing.) – cfr Aug 18 '23 at 04:53
  • @RuiSilva I've edited my answer and added some explanation. You can always remove or comment out parts of the drawing to figure out what's happening. – Qrrbrbirlbel Aug 18 '23 at 16:06
0

If you only tilde the first coordinate of your dashed line, you know that second coordinate is (0,0):

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{figure}[!h] \centering \begin{tikzpicture} \draw[black,thick] (0,0) -- (5,0); \draw[black,thick] (0,0) -- (0,3); \draw[black,thick] (0,3) -- (5,0); % Adding the angle \draw (3.5,0) arc (180:135:1) node[midway,anchor=east,black] {$\theta$}; \draw[rotate around={-30.96:(1.5,2.1)}] (1.5,2.1) rectangle (3,3); \draw[rotate around={-30.96:(1.5,2.1)},black,->,thick] (2.25,2.55) -- (2.25,3.55)node[black,anchor=west] {$\vec{N}$}; \draw[rotate around={-30.96:(1.5,2.1)}, black, ->, thick] (2.25,2.55) -- ({2.25 + sin(30.96)}, {2.55 - cos(30.96}) node[black,anchor= west] {$\vec{P}$}; \draw[rotate around={-30.96:(1.5,2.1)},black,->,thick] (2.25,2.55) -- (1.25,2.55) node[black,anchor=south east] {$\vec{F}_a$}; \draw[blue,->,rotate around={-30.96:(1.5,2.1)}] (4,3) -- (5,3) node[black,anchor=north west] {$x$}; \draw[blue,->,rotate around={-30.96:(1.5,2.1)}] (4,3) -- (4,4) node[black,anchor=west] {$y$}; \draw[red,dashed,thick] ([rotate around={-30.96:(1.5,2.1)}]2.25,2.55) -- (0,0);%just a try \end{tikzpicture} \end{figure}

\end{document}

enter image description here