Forgot password
 Register account
View 1904|Reply 3

(tikz)变椭圆与带单位

[Copy link]

673

Threads

110K

Posts

218

Reputation

Show all posts

kuing posted 2019-7-11 19:29 |Read mode
默认情况下,tikz 的距离单位是 cm,所以:
  1. \tikz{\draw(0,0)circle(1);}
  2. vs
  3. \tikz{\draw(0,0)circle(1cm);}
Copy the Code
它们是没区别的:
ddy1.png

但是,如果改变坐标系的话,比如:
  1. \tikz[x=2cm,y=0.5cm]{\draw(0,0)circle(1);}
  2. vs
  3. \tikz[x=2cm,y=0.5cm]{\draw(0,0)circle(1cm);}
Copy the Code
效果将是:
ddy2.png

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2019-7-11 20:13
这个区别是下午和 585 聊斜二测坐标系作图时发现的,讨论过程中的发现还不止这一样,下面继续扯……

为方便起见先定义一个临时命令:
  1. \newcommand\tmpxieerce[2][]{%
  2. \begin{tikzpicture}[x={(-135:0.5cm)},y={(1cm,0)},z={(0,1cm)},#1]
  3. \draw[->](-2,0)--(2,0)node[right]{$x$};
  4. \draw[->](0,-2)--(0,2)node[below]{$y$};
  5. \draw[->](0,0,-0.7)--(0,0,1.2)node[left]{$z$};
  6. #2
  7. \end{tikzpicture}}
Copy the Code
这时用:
  1. \tmpxieerce{\draw(0,0)circle(1);}
  2. \tmpxieerce{\draw(0,0)circle(1cm);}
Copy the Code
得:
ddy3.png
或许你会觉得前者这圆有点怪?没问题,加个外切正方形就会很清楚。
但是,如果加上 \draw(-1,-1)rectangle(1,1); 的话,则会变成这样:
ddy4.png
可见 rectangle 依然会按原来的方法来画,不管你坐标系是怎样。
所以,只能写 \draw(-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle; 才会得出:
ddy5.png

另外,还扯到了画点的问题,如上所述,\fill ... circle ... 时也必须带单位,否则点也会变成椭圆。
但是,有时图形会使用 scale 来调整大小,这时点的大小也跟着变,我们希望点的大小尽量统一,为此我还想出了另一招:
  1. \newcommand\filld[2][2pt]{\node at (#2) {\tikz{\fill circle (#1);}};}
Copy the Code
这样,以下代码:
  1. \tmpxieerce{\draw(0,0)circle(1);
  2. \draw(-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle;
  3. \fill (0,0) circle (2pt);}
  4. \tmpxieerce[scale=2]{\draw(0,0)circle(1);
  5. \draw(-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle;
  6. \fill (0,0) circle (2pt);}
  7. \tmpxieerce[scale=2]{\draw(0,0)circle(1);
  8. \draw(-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle;
  9. \filld{0,0}}
Copy the Code
得到的是:
ddy6.png
也就是用 \filld 时的点默认是 2pt 大小,不管 scale 多少,而且,还可以用 \filld[...] 临时改变。

甚至可以这样:
  1. \newcommand\fillds[2][2pt]{\foreach \i in {#2} {
  2. \node at (\i) {\tikz{\fill circle (#1);}};
  3. }}
  4. \tmpxieerce[scale=2]{\draw(0,0)circle(1);
  5. \draw(-1,-1)coordinate(A)--(1,-1)coordinate(B)
  6. --(1,1)coordinate(C)--(-1,1)coordinate(D)--cycle;
  7. \filld{0,0}
  8. \fillds{A,B,C,D}
  9. }
Copy the Code
得到:
ddy7.png

673

Threads

110K

Posts

218

Reputation

Show all posts

original poster kuing posted 2019-7-11 22:24
既然 circle 是这样,那 arc 应该也类似,一试果然:
  1. \tmpxieerce{\draw (1,0) arc (0:180:1);}
  2. \tmpxieerce{\draw (1,0) arc (0:180:1cm);}
Copy the Code
得:
ddy8.png

764

Threads

4672

Posts

27

Reputation

Show all posts

isee posted 2019-7-11 22:36
矩形坐标和“绝对长度”不和,哈哈

不过,我以前都是这么画

\tikz[xslant=1,yscale=0.5]{\draw(0,0)circle(1);\draw(-1,-1)rectangle(1,1);}

细节就不记得了

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-20 06:11 GMT+8

Powered by Discuz!

Processed in 0.018079 seconds, 46 queries