![逐渐改变磁盘的不透明度(pgfplots)](https://linux22.com/image/316074/%E9%80%90%E6%B8%90%E6%94%B9%E5%8F%98%E7%A3%81%E7%9B%98%E7%9A%84%E4%B8%8D%E9%80%8F%E6%98%8E%E5%BA%A6%EF%BC%88pgfplots%EF%BC%89.png)
我想制作一个透明度逐渐变化的圆盘。我希望红色圆盘的黑线上的透明度为 1,并让该透明度沿着圆盘的两个方向逐渐降至零,并在圆盘的另一端相遇。可以制作吗?如果可以,怎么做?
该图片是使用此代码创建的:
\documentclass[a4paper,14pt]{scrartcl}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}
\usetikzlibrary{calc,decorations.text}
\usepgfplotslibrary{fillbetween}
\usepackage{calc}
\pgfplotsset{compat=newest}
\usepackage[active,tightpage]{preview}
\setlength\PreviewBorder{2pt}
\definecolor{green}{rgb}{0.0, 0.5, 0.0}
\begin{document}
\begin{preview}
\begin{tikzpicture}
\filldraw [red,opacity=0.2] (0,0) circle(1);
\filldraw [blue] (0,0) circle (0.1);
\draw (0,0) -- (0,1);
\end{tikzpicture}
\end{preview}
\end{document}
答案1
像这样?我用不同透明度的直线填充了圆圈:
代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\radius{3cm}
\begin{tikzpicture}
\foreach \Angle in {0,0.5,...,180}
{
\draw[draw=red!30,opacity=1-(1/180)*\Angle] (0,0) -- (90+\Angle:\radius);
\draw[draw=red!30,opacity=1-(1/180)*(180-\Angle)] (0,0) -- (-90+\Angle:\radius);
}
\draw[thick] (0,0) -- (90:\radius);
\filldraw[blue] (0,0) circle [radius=4pt];
\end{tikzpicture}
\end{document}
变化;现在变化更平滑了,因为我使用了不同透明度的扇形来填充圆圈;使用 \step 可以控制使用的圆弧数量(= 180/\step
)。使用\step=5
,如示例中所示,产生的效果不如上一个选项那么酷,但更改\step
为 1(扇形几乎变成直线)你就会恢复酷炫。
代码:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\radius{3cm}
\def\step{5}
\begin{tikzpicture}
\def\iterations{\numexpr180/\step\relax}
\foreach \Valor in {1,2,...,\numexpr\iterations-1\relax}
{
\fill[red!60,opacity=(-1/\iterations)*\Valor+1]
(0,0) -- ({90+(\Valor-0.5)*\step}:\radius)
arc [start angle=90+(\Valor-0.5)*\step,end angle=90+(\Valor+0.5)*\step,radius=\radius] -- cycle;
\fill[red!60,opacity=(-1/\iterations)*\Valor+1]
(0,0) -- ({90-(\Valor-0.5)*\step}:\radius)
arc [start angle=90-(\Valor-0.5)*\step,end angle=90-(\Valor+0.5)*\step,radius=\radius] -- cycle;
}
\filldraw[red!60,opacity=1]
(0,0) -- ({90-0.5*\step}:\radius)
arc [start angle=90-0.5*\step,end angle=90+0.5*\step,radius=\radius] -- cycle;
\draw[thick] (0,0) -- (90:\radius);
\filldraw[blue] (0,0) circle [radius=4pt];
\end{tikzpicture}
\end{document}
或者,使用圆圈和阴影:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\radius{3cm}
\begin{tikzpicture}
\shade[top color=red!30,bottom color=white,middle color=red!10]
(0,0) circle [radius=3cm];
\draw[thick] (0,0) -- ++(90:\radius);
\filldraw[blue] (0,0) circle [radius=4pt];
\end{tikzpicture}
\end{document}
\step=6
为什么不呢,使用第二种方法的动画:
动画的代码:
\documentclass{beamer}
\usepackage{tikz}
\tikzset{
invisible/.style={opacity=0,text opacity=0},
visible on/.style={alt=#1{}{invisible}},
alt/.code args={<#1>#2#3}{%
\alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}}
}
}
\begin{document}
\begin{frame}
\centering
\def\radius{3cm}
\def\step{6}
\begin{tikzpicture}
\def\iterations{\numexpr180/\step\relax}
\foreach \Valor [count=\xi from 3] in {1,2,...,\numexpr\iterations-1\relax}
{
\fill[red!60,opacity=(-1/\iterations)*\Valor+1,visible on=<\xi->]
(0,0) -- ({90+(\Valor-0.5)*\step}:\radius)
arc [start angle=90+(\Valor-0.5)*\step,end angle=90+(\Valor+0.5)*\step,radius=\radius] -- cycle;
\fill[red!60,opacity=(-1/\iterations)*\Valor+1,visible on=<\xi->]
(0,0) -- ({90-(\Valor-0.5)*\step}:\radius)
arc [start angle=90-(\Valor-0.5)*\step,end angle=90-(\Valor+0.5)*\step,radius=\radius] -- cycle;
}
\filldraw[red!60,opacity=1,visible on=<2->]
(0,0) -- ({90-0.5*\step}:\radius)
arc [start angle=90-0.5*\step,end angle=90+0.5*\step,radius=\radius] -- cycle;
\draw[thick] (0,0) -- (90:\radius);
\filldraw[blue] (0,0) circle [radius=4pt];
\end{tikzpicture}
\end{frame}
\end{document}
在终端中运行
convert -verbose -delay 12 -loop 0 -density 300 b.pdf b.gif
答案2
\documentclass{article}
\usepackage{tikz}
\begin{document}
\def\radius{3cm}
\begin{tikzpicture}
\clip (0,0) circle (\radius);
\shade [inner color=white, outer color=red!30] (0,-\radius) circle (2*\radius);
\draw [thick, ] (0,0) -- ++(90:\radius);
\filldraw [blue] (0,0) circle [radius=4pt];
\end{tikzpicture}
\end{document}