TransWikia.com

Drawing triangles in latex

TeX - LaTeX Asked on April 28, 2021

enter image description here

    newcommand{length}{4}
    newcommand{width}{4}
    begin{tikzpicture}
       
       %Coordinates for square 
       coordinate (O) at (0,0);
       coordinate (A) at (length,0);
       coordinate (B) at (0,width);
       coordinate (C) at (length,width);

       %Drawing square
       draw (O) -- (A) -- (C) -- (B) -- cycle;

       
       foreach x in {0.1,4.1}{
          foreach y in {width-0.1, -0.1}{
             draw (x,y) -- (x,y-1.2) -- (x+0.4,y-1.2+0.9) -- cycle;
           }
        }
        
        foreach x in {0.1,length + 0.1}{
           foreach y in {- 0.1, width  - 0.1}{
              draw (x,y) -- (x+1.2,y) -- (x+1.2-0.9,y-0.4) -- cycle;
           }
        }

   end{tikzpicture}

This is the code which i used to draw two similar triangles as shown in figure. But the two triangles overlap(slightly) which should not happen. What can i do for that?

One Answer

I post this as an answer as it is too long for a comment. Your triangles overlap because the third coordinate of each blue triangle is not the third coordinate of the corresponding red triangle. Then, you have to change the 0.4 into a 0.3.

Moreover, I took the liberty to clean the code a bit and to suggest a modification as to how to draw the triangles. Indeed, by drawing them separately, you will end up with a protrusion to the top left of each of your triangle shapes (that is present on the picture in the question). Then, I propose to first fill the triangle shapes and then to draw the encompassing shape:

documentclass{standalone}

usepackage{tikz}

begin{document}

    newcommand{length}{4}
    newcommand{width}{4}
    
    begin{tikzpicture}
        
        % Draw square if coordinates are needed, which is not the case here
%       draw (0, 0)                coordinate (O)
%               -- ++ (length, 0)  coordinate (A)
%               -- ++ (0, width)   coordinate (C)
%               -- ++ (-length, 0) coordinate (B)
%               -- cycle;

        % Draw square simply
        draw (0, 0) rectangle (length, width);

        foreach x in {0, length}{
            foreach y in {width, 0}{
                % Draw blue triangles 
                fill[blue] (x + 0.1, y - 0.1) -- ++ (0, -1.2) -- ++ (0.3, 0.9) -- cycle;
                % Draw red triangles
                fill[red] (x + 0.1, y - 0.1) -- ++ (1.2, 0) -- ++ (-0.9, -0.3) -- cycle;
                % Draw shape around
                draw (x + 0.1, y - 0.1) -- ++ (1.2, 0) -- ++ (-0.9, -0.3) -- ++ (-0.3, -0.9) -- cycle;
                % Draw line separating the triangles
                draw (x + 0.1, y - 0.1) -- ++ (0.3, -0.3);
            }
        }

   end{tikzpicture}
   
end{document} 

which yields:

enter image description here

Correct answer by KersouMan on April 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP