TikZ learning note cycle

Write in front

Upper section( TikZ learning notes (I) basic graphics _zorchpcsdn blog )We said Ti k Z \text{Ti}k\text{Z} TikZ's simple graphic drawing method. This time, let's learn about the use of circular statements (foreach...in...) in graphics. With this powerful tool, we can pass Ti k Z \text{Ti}k\text{Z} TikZ can easily and quickly generate many exquisite vector graphs

P.S. for different types of graphics, many bosses have encapsulated some useful macro packages, such as plane geometry drawing package tikz Euclide. Using this package to draw common graphics of plane geometry is better than using it directly Ti k Z \text{Ti}k\text{Z} TikZ is much simpler, but it is recommended to study it first Ti k Z \text{Ti}k\text{Z} TikZ, because these other packages are based on Ti k Z \text{Ti}k\text{Z} TikZ} secondary development, which is also a simple understanding of "so ran"

Preparatory knowledge

Before introducing loops, let's introduce some basic drawing knowledge points, which are also very important, especially when used in the following examples

Bisection of line segments

Let's introduce it first Ti k Z \text{Ti}k\text{Z} calc, a sub package of TikZ , needs to add \ usetikzlibrary{calc} in the preamble to use the command of line segment bisection

The defau lt calling format is ($(starting point of line segment)! < proportion > (ending point of line segment) $), which is easy to understand. For example, the following statement divides line segment AB equally, and the dividing point takes D


        % Mark point A, B, C
        \coordinate[label=left:{$A$}] (A) at (0,2);
        \coordinate[label=right:{$B$}] (B) at (5,2);

        % Draw line segments AB
        \draw (A) -- (B);
        % line bisection , The parameter in the middle can be 0~1 Number between, Take it here`.5`
        \coordinate[label=below:{$D$}] (D) at ($(A)!.5!(B)$);
        % Fill with red circles D spot
        \filldraw[red] (D) circle (1pt);


Similarly, if you need to take the split point of AD, you can add the following two commands:

\coordinate[label=below:{$E$}] (E) at ($(A)!.25!(B)$);
\filldraw[green] (E) circle (1pt);

Marking of drawings

In the previous section, we drew basic graphics, but for each graphics, it exists independently. In order to understand the graphics intersection after this section, we need to know the graphics marking

For a straight line (line segment), the marking is still very simple. Just add \ abel to the parameter when drawing the straight line

Cycle (pgfmanual-chap88)

Adobe illustrator(AI), a very popular vector graph drawing software, can create vector graphs directly with the mouse, but when a lot of repetitive work is encountered, Ti k Z \text{Ti}k\text{Z} The foreach statement in TikZ can give full play to its power. The following is a small example. This example is what I learned before Ti k Z \text{Ti}k\text{Z} The first drawing of the ruler and gauge drawing series drawn by TikZ, TikZ drawing example - ruler and gauge drawing: arbitrary bisection of line segments_ Zorchp CSDN blog.

For the loop, we first introduce its main format, namely

\foreach \i in {...}

The first ellipsis can be defined in the following way, that is, the start value, the second value,..., and the end value, (the second value can not be written, and the default is 1) is equivalent to the definition of the for loop statement in C + +, which requires three parameters to generate the loop variable

The following are some values generated by \ foreach, which are available in pgfmanual to deepen your understanding of \ foreach

\foreach \x in {1,2,...,6} {\x, } yields 1, 2, 3, 4, 5, 6,
\foreach \x in {1,2,3,...,6} {\x, } yields 1, 2, 3, 4, 5, 6,
\foreach \x in {1,3,...,11} {\x, } yields 1, 3, 5, 7, 9, 11,
\foreach \x in {1,3,...,10} {\x, } yields 1, 3, 5, 7, 9,
\foreach \x in {0,0.1,...,0.5} {\x, } yields 0, 0.1, 0.20001, 0.30002, 0.40002,
\foreach \x in {a,b,9,8,...,1,2,2.125,...,2.5} {\x, } yields a, b, 9, 8, 7, 6, 5, 4, 3, 2, 1, 2, 2.125, 2.25, 2.375, 2.5,

\foreach \x in {1,...,6} {\x, } yields 1, 2, 3, 4, 5, 6, 
\foreach \x in {9,...,3.5} {\x, } yields 9, 8, 7, 6, 5, 4,

\foreach \x in {a,...,m} {\x, } yields a, b, c, d, e, f, g, h, i, j, k, l, m,
\foreach \x in {Z,X,...,M} {\x, } yields Z, X, V, T, R, P, N,

\foreach \x in {2^1,2^...,2^7} {$\x$, } yields 21, 22, 23, 24, 25, 26, 27,
\foreach \x in {0\pi,0.5\pi,...\pi,3\pi} {$\x$, } yields 0π, 0.5π, 1π, 1.5π, 2π, 2.5π, 3π, 
\foreach \x in {A_1,..._1,H_1} {$\x$, } yields A1, B1, C1, D1, E1, F1, G1, H1,

The following is a small example for drawing a coordinate axis


    	% Draw mesh
        \draw[gray!50, thin] (-2,-2) grid (10,10);
        % Draw arrow
        \draw[-latex] (-3,0) -- (11,0);
        \draw[-latex] (0,-3) -- (0,11);
        % Mark points and label
        \coordinate [label=below:$x$] (x) at (11,0);
        \coordinate [label=right:$y$] (y) at (0,11);
        \coordinate [label=below left:$O$] (O) at (0,0);
		% Start drawing scale
        \foreach \i in {1,...,10} {
        	% Mark scale: label To customize the font color
            \coordinate [label={[red] below right:$\i$}] (x\i) at (\i-.1,0);
            \coordinate [label={[green] below left:$\i$}] (y\i) at (0,\i+.1);
            % Fill with small ellipses
            \filldraw (\i,0) ellipse (.5pt and 1pt);
            \filldraw (0,\i) ellipse (1pt and .5pt);

An example

The simplest drawing in the ruler and gauge drawing series is explained as an example



        % Mark point A, B, C
        \coordinate[label=left:{$A$}] (A) at (0,2);
        \coordinate[label=right:{$B$}] (B) at (5,2);
        \coordinate[label=below right:{$C$}] (C) at (5,0);

        % Draw line segments AB, AC
        \draw (A) -- (B);
        \draw (A) -- (C);

        % Bisector AC
        \foreach \i in {1,...,5}
                \coordinate[label=below:{$\i'$}] (a\i) at ($(A)!\i/5!(4.5,.2)$);
                \coordinate[label=above:{$\i$}] (b\i) at ($(A)!\i/5!(B)$);
                \draw (a\i) -- (b\i);


Tags: Latex

Posted on Thu, 18 Nov 2021 21:16:30 -0500 by uancletus