我有两条路径,第一条要完全绘制,但第二条仅使用与第一条的交点绘制一部分。
这里有一个例子:我想只绘制正方形的上部(虚线)作为与交点 1 和 3 的闭合循环。但我不知道该怎么做。我通常使用 metapost,这样做很容易。我希望 pgf-tikz 也一样 ;-)
\documentclass[a4paper]{article}
\usepackage[marginparsep=3pt, top=2cm, bottom=1.5cm, left=3cm, right=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\tikzset{small dot/.style={fill=black,circle,scale=0.3},}
\begin{document}
\begin{tikzpicture}
% drawed just for explanation
\draw[thin,dashed] (-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle ;
\path[name path=carre] (-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle ;
\draw[name path=paral,blue] (-1.5,-.5)--(.5,-.5)--(1.5,.5)--(-.5,.5)--cycle ;
\fill [red,
name intersections={of=carre and paral,
name=i,sort by=carre,
total=\t}]
[every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}
{(i-\s) circle (1pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
答案1
像这样?
我\clip
在里面用过scope
\documentclass[a4paper]{article}
\usepackage[marginparsep=3pt, top=2cm, bottom=1.5cm, left=3cm, right=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\tikzset{small dot/.style={fill=black,circle,scale=0.3},}
\begin{document}
\begin{tikzpicture}
% drawed just for explanation
%\draw[thin,dashed] (-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle ;
\begin{scope}
\clip (-1,1) rectangle (1,0);
\draw[dashed,thin,fill=olive!40,name path global=carre] (-1,-1)--(1,-1)--(1,1)--(-1,1)--cycle ;
\end{scope}
\draw[name path=paral,blue] (-1.5,-.5)--(.5,-.5)--(1.5,.5)--(-.5,.5)--cycle ;
\fill [red,
name intersections={of=carre and paral,
name=i,sort by=carre,
total=\t}]
[every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}
{(i-\s) circle (1pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
没有\clip
\documentclass[a4paper]{article}
\usepackage[marginparsep=3pt, top=2cm, bottom=1.5cm, left=3cm, right=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,backgrounds}
\tikzset{small dot/.style={fill=black,circle,scale=0.3},}
\begin{document}
\begin{tikzpicture}
\draw[name path=paral,blue] (-1.5,-.5)--(.5,-.5)--node[coordinate,midway](a){}(1.5,.5)--(-.5,.5)-- node[coordinate,midway](b){}cycle ;
\begin{scope}[on background layer]
\draw[dashed,thin,fill=olive!40,name path global=carre] (b)-- ++(0,1)-- ++(2,0)--(a)-- cycle ;
\end{scope}
\fill [red,
name intersections={of=carre and paral,
name=i,sort by=carre,
total=\t}]
[every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}
{(i-\s) circle (1pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
在其他时候
使用pos=<value>
而不是midway
像
\draw[name path=paral,blue] (-1.5,-.5)--(.5,-.5)--node[coordinate,pos=0.8](a){}(1.5,.5)--(-.5,.5)-- node[coordinate,pos=0.2](b){}cycle ;
你得到
我在这里使用了相对坐标++(x,y)
语法。请注意,++
与单个 不同, 将相对原点更改为当前点+
。
现在某个任意平面:
\documentclass[a4paper]{article}
\usepackage[marginparsep=3pt, top=2cm, bottom=1.5cm, left=3cm, right=1.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,backgrounds,positioning}
\tikzset{small dot/.style={fill=black,circle,scale=0.3},}
\begin{document}
\begin{tikzpicture}
\draw[name path=paral,blue] (-1.5,-.5)--node[coordinate,pos=0.8](a){}(.5,-.5)--(1.5,.5)--(-.5,.5)-- node[coordinate,pos=0.2](b){}cycle ;
\begin{scope}[on background layer]
\coordinate[above= 1cm of a] (c);
\coordinate[above= 1cm of b] (d);
\draw[dashed,thin,fill=olive!40,name path global=carre] (b)-- (d)-- (c)--(a)-- cycle ;
\end{scope}
\fill [red,
name intersections={of=carre and paral,
name=i,sort by=carre,
total=\t}]
[every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}
{(i-\s) circle (1pt) node {\footnotesize\s}};
\end{tikzpicture}
\end{document}
PS:我没有研究交叉点,因为它与问题无关。