我有以下代码:
\documentclass{article}
\usepackage{tikz}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
\draw (0,2)--(0,-2)--(-3.464,0)--(3.464,0)--(0,-2);
\draw (-3.464,0)--(0,2)--(3.464,0);
\node[below left](0,0) {$O$};
\draw [red,thick,domain=30:150] plot ({4*cos(\x)}, {-2+4*sin(\x)});
\draw [red,thick,domain=-150:-30] plot({4*cos(\x)},{2+4*sin(\x)});
\draw[<->] (4.5,-2)--node[right]{4 cm}(4.5,2);
\draw (0,2) -- node[sloped] {$|$} (3.464,0)
(3.464,0) -- node[sloped] {$|$} (0,-2)
(0,-2) -- node[sloped] {$|$} (-3.464,0)
(-3.464,0) -- node[sloped] {$|$} (0,2);
\node[above] at (0,2) {$A$};
\node[below] at (0,-2) {$C$};
\node[left] at (-3.464,0) {$B$};
\node[right] at (3.464,0) {$D$};
\node at (1.5,0.78) {4 cm};
\fill[yellow] (30:4) arc (30:90:4);
\fill[yellow] (90:4) arc (90:150:4);
\fill[yellow] (-30:4) arc (-30:-90:4);
\fill[yellow] (-90:4) arc (-90:-150:4);
\end{tikzpicture}
\end{document}
我想知道是否有一种简单的方法可以转换我得到的黄色区域,以便它们适合我的图表(红色弧线内)。如果没有,有人对如何为菱形和红色弧线之间的区域着色有什么建议吗?
答案1
我建议不要将某些区域填满白色。在这种情况下,它会让事情变得不必要地复杂,如果你有背景,你会后悔的。你可以通过命名坐标来简化很多事情。(siunitx
借用自 Zarko 的答案。)
\documentclass{article}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}[every label/.append style={text=black}]
\draw [red,thick,domain=30:150,fill=yellow] plot ({4*cos(\x)}, {-2+4*sin(\x)})
coordinate[label=left:$B$] (B)
-- (0,2) coordinate[label=above:$A$] (A) -- cycle;
\draw [red,thick,domain=-150:-30,fill=yellow] plot({4*cos(\x)},{2+4*sin(\x)})
coordinate[label=right:$D$] (D)
-- (0,-2) coordinate[label=below:$C$] (C) -- cycle;
\draw[<->] (4.5,0|-A)--node[right]{\SI{4}{cm}} (4.5,0|-C);
\draw (B) -- node[sloped] {$|$} (A)
-- node[sloped] {$|$} node[below=0.2em] {\SI{4}{cm}} (D)
-- node[sloped] {$|$} (C) -- node[sloped] {$|$} cycle;
\draw (A) -- (C) coordinate[midway,label=below left:$O$] (O) (B) -- (D);
\end{tikzpicture}
\end{document}
答案2
如果我正确理解了你的问题,那么你会喜欢获得以下图像:
我几乎完全重写了你的 mwe。首先我添加了 TikZ 库quotes
,定义了两种样式(用于用黄色填充的红线)和用于边缘标签),使用coordinate
标签确定坐标 A、B、C 和 D,并更改绘制顺序。首先绘制用黄色填充的红线,然后用白色填充的线,最后是其他线:
\documentclass{article}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}[
every edge quotes/.style = {sloped},
line/.style = {red,fill=yellow, thick}
]
\draw [line,domain= 30:150] plot ({4*cos(\x)}, {-2+4*sin(\x)});
\draw [line,domain=-150:-30] plot ({4*cos(\x)}, { 2+4*sin(\x)});
%
\coordinate[label=$A$] (A) at (0,2);
\coordinate[label=below:$C$] (C) at (0,-2);
\coordinate[label= left:$B$] (B) at (-3.464,0);
\coordinate[label=right:$D$] (D) at (3.464,0);
%
\draw[fill=white]
(A) to ["$|$"] (B)
to ["$|$"] (C)
to ["$|$"] (D)
to ["\vphantom{|}\\$|$\\ \SI{4}{cm}"] cycle; % <---
%
\draw (A) -- (C) (B)--(D);
\node[below left](0,0) {$O$};
%
\draw[<->] (4.5,-2) -- node [right] {\SI{4}{cm}} (4.5,2);
\end{tikzpicture}
\end{document}
编辑:补充的是被遗忘的“AD”线长度测量。