我正在尝试填充两条虚线之间的区域。我尝试了很多选项,但似乎都没有用。
我用特定颜色标记了需要填充的区域。我当前的代码是:
\documentclass[10pt,a4paper,twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage[centering,includeheadfoot,top=25mm, left=40mm, right=25mm, bottom=30mm]{geometry}
\usepackage{fancyhdr}
\usepackage{color}
\usepackage{thmbox}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\fill[color=yellow] (0, 0) circle (2); % Sonne im Ursprung (0, 0)
\shade[right color=lightgray] (7.5, 0) circle (0.5); % Sonne im Ursprung (5, 0)
\shade[shading=ball, ball color=blue] (10, 0) circle (1); % Erde im Ursprung (10, 0)
\draw[dashed, color=red ,shorten >=-1.6cm,shorten <=-0cm,name path = A] (0, 2) --node[near start,sloped,above]{\textcolor{red}{Randstrahl}} (7.5, 0.5);
\draw[dashed, color=red ,shorten >=-3cm,shorten <=-0cm] (0, 2) -- (7.5, -0.5);
\draw[dashed, color=red ,shorten >=-1.6cm,shorten <=-0cm,name path = B] (0, -2) -- (7.5, -0.5);
\draw[dashed, color=red ,shorten >=-3cm,shorten <=-0cm] (0, -2) -- (7.5, 0.5);
\end{tikzpicture}
\end{document}
如上所述,我尝试了很多方法。所以这或多或少是解决我问题的一次绝望的尝试。
答案1
欢迎使用 TeX.SE!您无法真正填充图片中路径之间的区域的原因是您使用了shorten >=...
一些负尺寸。因此路径比虚线短。我修复了这个问题,让它工作起来。尽管您可以使用 pgfplots 库来做到这一点fillbetween
,但这里没有必要,因为您只有直线。我使用backgrounds
并稍微改变了绘制事物的顺序,以免覆盖您的行星。
\documentclass[10pt,a4paper,twoside]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{intersections,backgrounds}
\begin{document}
\begin{tikzpicture}
\fill[color=yellow] (0, 0) circle (2); % Sonne im Ursprung (0, 0)
\shade[right color=lightgray] (7.5, 0) circle (0.5); % Sonne im Ursprung (5, 0)
\draw[dashed, color=red,name path = A] (0, 2) --
node[near start,sloped,above]{\textcolor{red}{Randstrahl}} (10, 0)
coordinate (X1);
\draw[dashed, color=red ,name path = C] (0, 2) --
(10, -1.33)
coordinate (X2);
\draw[dashed, color=red ,name path = B] (0, -2) -- (10, 0)
coordinate (X3);
\draw[dashed, color=red ,name path = D] (0, -2) -- (10, 1.33)
coordinate (X4);
\begin{scope}[on background layer]
\fill[gray,
name intersections={of=A and D,by=X5}] (X3) -- (X5) -- (X4);
\fill[gray,
name intersections={of=C and B,by=X6}] (X1) -- (X6) -- (X2);
\fill[black] (X6) -- (X3) -- (X5);
\end{scope}
\shade[shading=ball, ball color=blue] (10, 0) circle (1); % Erde im Ursprung (10, 0)
\end{tikzpicture}
\end{document}
这是使用切线的版本,由 esdd 在这个答案但实现方式略有不同/具体来说,我使用这个答案使虚线与两个圆相切。还有其他方法可以实现这一点,最值得注意的是使用tkz-euclide
库的方法,例如这里。此解决方案无需使用交叉点库即可找到交叉点,对于直线来说效果很好。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{backgrounds,calc}
\begin{document}
\begin{tikzpicture}
\pgfmathsetmacro{\rsun}{2}
\pgfmathsetmacro{\rmoon}{0.5}
\pgfmathsetmacro{\mid}{\rsun/(\rsun + \rmoon)}
\pgfmathsetmacro{\out}{\rsun/(\rsun - \rmoon)}
\node[circle,draw,inner color=yellow,outer color=orange!50!yellow,minimum
size=2*\rsun*1cm,alias=c1] (Sonne) at (0, 0){}; % Sonne
\node[left color=lightgray,right color=darkgray,circle,draw,minimum
size=2*\rmoon*1cm,alias=c2] (Mond) at (7.5, 0){}; % Mond
% from https://tex.stackexchange.com/a/7209/121799
\path (Sonne.center) -- coordinate[pos=\mid] (mid) (Mond.center);
\path (Sonne.center) -- coordinate[pos=\out] (out) (Mond.center);
\coordinate (Erdmittelpunkt) at (10,0);
\coordinate (Nordpol) at (10,1);
\foreach \i in {Sonne,Mond}
\foreach \j in {1,2}
\foreach \k in {mid,out}
\coordinate (\i-\k-\j) at (tangent cs:node=\i,point={(\k)},solution=\j);
\draw[dashed, color=red] (Sonne-mid-1) --
(intersection cs:first line={(Sonne-mid-1)--(Mond-out-1)},
second line={(Erdmittelpunkt)--(Nordpol)}) coordinate (aux1);
\draw[dashed, color=red] (Sonne-mid-2) --
(intersection cs:first line={(Sonne-mid-2)--(Mond-out-2)},
second line={(Erdmittelpunkt)--(Nordpol)}) coordinate (aux2);
\draw[dashed, color=red] (Sonne-out-1) --
(intersection cs:first line={(Sonne-out-1)--(Mond-mid-1)},
second line={(Erdmittelpunkt)--(Nordpol)}) coordinate (Oberhalb);
\draw[dashed, color=red] (Sonne-out-2) --
(intersection cs:first line={(Sonne-out-2)--(Mond-mid-2)},
second line={(Erdmittelpunkt)--(Nordpol)}) coordinate (Unterhalb);
% auxiliary coordinates
\path(intersection cs:first line={(Sonne-mid-1) --(aux1)},
second line={(Sonne-out-2)--(Unterhalb)}) coordinate (Mond1);
\path(intersection cs:first line={(Sonne-mid-2) --(aux2)},
second line={(Sonne-out-1)--(Oberhalb)}) coordinate (Mond2);
% fills on background in order not to distort Sonne' nor the tangents
\begin{scope}[on background layer]
\fill[gray] (Unterhalb) -- (Mond1) -- (aux1);
\fill[gray] (Oberhalb) -- (Mond2) -- (aux2);
\fill[black] (Mond1) -- (aux1) -- (aux2) -- (Mond2);
\end{scope}
\shade[shading=ball, ball color=blue] (Erdmittelpunkt) circle (1); % Erde im Ursprung (10, 0)
\end{tikzpicture}
\end{document}
答案2
只是为了好玩和学习,一个选项用于let
计算从太阳的顶部和底部到行星的一定距离的阴影线的角度(0,2) -- +(-\n1:9.5)
,然后将这个坐标保存为,coordinate (a)
另一个(0,-2) -- +(\n1:9.5)
保存为coordinate (b)
使用月球的坐标轻松绘制阴影,也可以将阴影放在行星上,坐标位于 A 和 B 的中间;对第二个阴影执行相同的过程。
为了添加更多细节,我在剪辑画布中将背景更改为黑色,并使用 foreach 指令添加了一些星星,然后使用范围内的剪辑绘制月亮,使用多个带有装饰的椭圆形状为月亮着色以获得不规则的阴影;对于行星,另一个剪辑范围使用另一组椭圆进行阴影处理;最后,我添加了一个节点(CountriesOfEurope
在本例中为希腊)来绘制我曾经在乳胶符号目录中看到的地面。
结果:
梅威瑟:
\documentclass[border=0pt,tikz]{standalone}
\usepackage[scaled]{CountriesOfEurope}
\usetikzlibrary{calc,decorations.pathmorphing}
\begin{document}
\pagecolor{black}
\begin{tikzpicture}
\clip (-3.5,-3.5) rectangle ++(18,7);
\shade[inner color=orange!90!black, outer color=black!80!orange] (0,0) circle (18);% Heliosphere
\foreach \i in {1,...,500} {\fill[white](-3.5,-3.5)++ (rnd*18cm, rnd*7cm) circle (0.45pt);}
\shade[inner color=orange!50!yellow, outer color=yellow!50!white] (0,0) circle (2);% Sun
\begin{scope}
\clip (7.5,0) circle (0.5); % The moon
\foreach \j [evaluate=\j as \jn using {\j*5}] in {1,...,20}{
\fill[white!\jn!black,decoration=random steps, segment length=1pt,decorate](8-0.05*\j,0) circle (0.2 and 0.5);
}
\end{scope}
\begin{scope}
\clip (10, 0) circle (1); % The Planet
\foreach \j [evaluate=\j as \jn using {\j*5}] in {1,...,20}{
\fill[cyan!\jn!black](11-0.12*\j,0) circle (0.4 and 1);
}
\node[text opacity=0.2] at (10,0){\color{white}\EUCountry{143}};
\end{scope}
\draw[dashed,line width=0.5pt,white]
let
\n1 = {atan(1.5/7.5)}, % atan(sun_rad-Moon_rad/distance_moon_sun)
\n2 = {atan(2.5/7.5)}% atan(sun_rad+Moon_rad/distance_moon_sun)
in
(0,2) -- +(-\n1:9.5) coordinate (a)node[near start,sloped,above]{\color{white}Randstrahl}
(0,-2) -- +(\n1:9.5) coordinate (b)
(0,2) -- +(-\n2:15) coordinate (c)
(0,-2) -- +(\n2:15) coordinate (d);
\fill[black,fill opacity=0.1] (7.5,0.5) -- ++(0,-1) -- (c) -- (d)-- cycle;
\fill[black,fill opacity=0.2] (7.5,0.5) -- ++(0,-1) -- (b) -- (a) node[midway](moon-shadow){}-- cycle
(moon-shadow) circle (0.08 and 0.15);
\fill[black,fill opacity=0.2](moon-shadow) circle (0.08 and 0.15);
\end{tikzpicture}
\end{document}
答案3
这是另一个建议,使用节点代替圆圈和tangent cs
:
\documentclass[a4paper,twoside]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\newcommand\winkel{75}
\path[nodes={circle,outer sep=0pt}]
(0,0) node(sonne)[fill=yellow,minimum size=4cm]{}
(sonne.\winkel) coordinate(sonne1)
([rotate around={-2*\winkel:(sonne.center)}]sonne.\winkel) coordinate(sonne2)
(7.5,0) node(mond)[shade,left color=orange!30,right color=brown!80!lightgray,minimum size=1cm]{}
;
\draw[dashed,red,thick]
(sonne1)
-- (tangent cs:node=mond,point={(sonne1)},solution=1) coordinate(s11)
-- ([turn=0]2.8,0) coordinate(r11)
(sonne1)
-- node [near start,sloped,above]{Randstrahl}
(tangent cs:node=mond,point={(sonne1)},solution=2) coordinate(s12)
-- ([turn=0]2,0) coordinate(r12)
(sonne2)
-- (tangent cs:node=mond,point={(sonne2)},solution=1) coordinate(s21)
-- ([turn=0]2,0)coordinate(r21)
(sonne2)
-- (tangent cs:node=mond,point={(sonne2)},solution=2) coordinate(s22)
-- ([turn=0]2.8,0) coordinate(r22)
;
\path (10,0) node[circle,shading=ball, ball color=blue,minimum size=2cm]{};
\begin{scope}[on background layer]
\fill[gray!80] (s11) -- (s22) -- (r22) -- (r11) -- cycle;
\fill[black] (s12) -- (s21) -- (r21) -- (r12) -- cycle;
\end{scope}
\end{tikzpicture}
\end{document}
结果:
\winkel
但我会将上面的代码更改为例如 75。
结果为\newcommand\winkel{75}
:
答案4
除了@marmot 和@esdd 已经给出的两种解决方案之外,还可以使用该calc
库来定义蓝球以外的外围半径。
注意:我删除了该包的加载,inputenc
因为它现在包含在 LaTeX 内核中。
\documentclass[10pt,a4paper,twoside]{article}
\usepackage[ngerman]{babel}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\fill[color=yellow] (0, 0) circle (2); % Sonne im Ursprung (0, 0)
\coordinate(o') at (7.5,.5);
\coordinate(o) at (7.5,-.5);
\draw[ dashed,red]($(7.5,-0.5)!-.4!(0,2)$)coordinate(a)-- (0, 2) --node[near start,sloped,above]{\textcolor{red}{Randstrahl}} ($(0,2)!1.4!(7.5,0.5)$)coordinate(a');
\draw[dashed,red]($(7.5,-.5)!-.4!(0,-2)$)coordinate(b)-- (0, -2) -- ($(0,-2)!1.4!(7.5, 0.5)$)coordinate(b');
\begin{scope}
\clip(7.5,-1.5)rectangle(10,1.5);
\fill[gray](a')--(o')--(b');
\fill[gray](a)--(o)--(b);
\fill(a')--(o')--(o)--(b);
\end{scope}
\shade[right color=lightgray] (7.5, 0) circle (0.5); % Sonne im Ursprung (5, 0)
\shade[shading=ball, ball color=blue] (10, 0) circle (1); % Erde im Ursprung (10, 0)
\end{tikzpicture}
\end{document}