![如何用 TikZ 绘制弯曲的街道](https://linux22.com/image/322247/%E5%A6%82%E4%BD%95%E7%94%A8%20TikZ%20%E7%BB%98%E5%88%B6%E5%BC%AF%E6%9B%B2%E7%9A%84%E8%A1%97%E9%81%93.png)
我对使用 TikZ 绘制弯曲的街道感兴趣。最终的街道应如下所示:
目前我尝试使用贝塞尔曲线来解决我的问题。但结果并不令人满意。这是我的代码:
\documentclass[]{article}
\usepackage{tikz}
\usepackage{tikz,fullpage}
\begin{document}
\begin{center}
\begin{tikzpicture}
\draw [thin] (0,-3) -- (0,2);
\draw [thin] (0,4) -- (0,5);
\draw [thin] (2,-3) -- (2,5);
\draw [thin,dashed] (1,-3) -- (1,5);
\draw (-6,-2) .. controls (-4,5) and (-1,3.8) .. (0,4);
\draw (-4,-2) .. controls (-2,3) and (-1,1.8) .. (0,2);
\end{tikzpicture}
\end{center}
\end{document}
那么 TikZ 是否有一种解决方案,可以用一条曲线组合简单的两个点?
感谢您的帮助!
问候!
答案1
您还可以使用double
以下行:
\documentclass[tikz, border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[double =white,double distance=1cm,line width=1mm] (0,-4) -- (0,4) -- (0,0)
to[out=180, in=90, looseness=2] (-4,-4);
\end{tikzpicture}
\begin{tikzpicture}
\draw[double =black,double distance=1.1cm] (0,-4) -- (0,4) -- (0,0)
to[out=180, in=90, looseness=2] (-4,-4);
\draw[white,line width=1mm, loosely dashed] (0,-4) -- (0,4) ;
\draw[white,line width=1mm, loosely dashed] (-0.4,0) to[out=180, in=90, looseness=2] (-4,-4);
\end{tikzpicture}
\end{document}
答案2
您可以使用preaction
在白线前画一条很粗的黑线,甚至可以将它与 结合使用,从外向内画出黑色、白色、黑色。这并不完美,因为你必须考虑绘制的顺序,但你可以通过和postaction
使用图层来解决这个问题\pgfdeclarelayer{<name>}
\pgfsetlayers{<layer list>}
。
代码
\documentclass[tikz, border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[preaction={draw, line width=1cm, black}, white, line width=1mm] (0,-4) -- (0,4);
\draw[preaction={draw, line width=1cm, black}, white, line width=1mm] (-0.4,0) to[out=180, in=90, looseness=2] (-4,-4);
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}
[ road/.style={
preaction={draw, line width=10mm, black},
white, line width=8mm,
postaction={draw, line width=1mm, black, loosely dashed}}
]
\draw[road] (0,-4) -- (0,4);
\draw[road] (-0.4,0) to[out=180, in=90, looseness=2] (-4,-4);
\end{scope}
\end{tikzpicture}
\end{document}
输出
答案3
这是我的最终结果:
\documentclass[class=minimal,border=0pt]{standalone}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[double=white,double distance=2cm,ultra thick] (0,-4) -- (0,4) -- (0,0) to[out=180, in=90, looseness=2] (-4,-4);
\draw[thick,dashed] (-1,0) to[out=180, in=90, looseness=2] (-4,-4);
\draw[thick,dashed] (0,-4) -- (0,4);
\node (CAR1) at (-3.5,-3.2) [rectangle,draw,thick,minimum width=0.5cm,minimum height=0.9cm] {};
\node (CAR2) at (-1.7,-0.5) [rectangle,draw,thick,minimum width=0.5cm,minimum height=0.9cm,rotate=90] {};
\node (T1) at (-3.5,-3.1) [regular polygon,regular polygon sides=3,fill=black,minimum height=0.4cm] {};
\node (T2) at (-1.5,-0.5) [regular polygon,regular polygon sides=3,fill=black,minimum height=0.4cm,rotate=30] {};
\draw[thick,blue] (-1,-0.5) to[out=180, in=90, looseness=2] (-3.5,-2.8);
\draw[thick,red] (-3.5,-2.8) -- (-2.1,-0.5);
\draw[thick,dotted] (-3.5,-2.8) -| (-2.1,-0.5);
\end{tikzpicture}
\end{document}
但是我怎样才能删除这些行(?):