Tikz 在交叉点后删除线的其余部分

Tikz 在交叉点后删除线的其余部分

我创建了下图:

\documentclass[border=18pt]{standalone}
\usepackage{tikz}


\begin{document}

\begin{tikzpicture}

\begin{scope}[>=latex]
\draw [->] (-3,0) -- (3,0);
\draw [->] (0,-1.5) -- (0,6.5);
\end{scope}

\filldraw [black] (0,0) circle (2pt);

\def\centerx{0}
\def\centery{4}

\def\side{3}
\def\rot{40}
\def\sidePerc{0.25}
\pgfmathparse{\side * sqrt(2)/2 }\let\veclen\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\llx\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\lly\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\lrx\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\lry\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\urx\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\ury\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\ulx\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\uly\pgfmathresult


\pgfmathparse{\llx*cos(\rot) - \lly*sin(\rot)}\let\llxr\pgfmathresult
\pgfmathparse{\llx*sin(\rot) + \lly*cos(\rot)}\let\llyr\pgfmathresult
\pgfmathparse{\lrx*cos(\rot) - \lry*sin(\rot)}\let\lrxr\pgfmathresult
\pgfmathparse{\lrx*sin(\rot) + \lry*cos(\rot)}\let\lryr\pgfmathresult
\pgfmathparse{\urx*cos(\rot) - \ury*sin(\rot)}\let\urxr\pgfmathresult
\pgfmathparse{\urx*sin(\rot) + \ury*cos(\rot)}\let\uryr\pgfmathresult
\pgfmathparse{\ulx*cos(\rot) - \uly*sin(\rot)}\let\ulxr\pgfmathresult
\pgfmathparse{\ulx*sin(\rot) + \uly*cos(\rot)}\let\ulyr\pgfmathresult



\coordinate (A') at ({\llxr+\centerx},{\llyr+\centery});
\coordinate (B') at ({\lrxr+\centerx},{\lryr+\centery});
\coordinate (C') at ({\urxr+\centerx},{\uryr+\centery});
\coordinate (D') at ({\ulxr+\centerx},{\ulyr+\centery});


\filldraw [black] (A') circle (2pt)
            (B') circle (2pt)
            (C') circle (2pt)
            (D') circle (2pt);

\draw (A')--(B')--(C')--(D')--(A');

\pgfmathparse{sqrt( (\sidePerc*\sidePerc+0.25)*\side*\side )}\let\pveclen\pgfmathresult
\pgfmathparse{asin(\sidePerc*\side/\pveclen)}\let\angle\pgfmathresult
\pgfmathparse{neg(\pveclen*sin(\angle))}\let\cblx\pgfmathresult
\pgfmathparse{neg(\pveclen*cos(\angle))}\let\cbly\pgfmathresult
\pgfmathparse{neg(\cblx)}\let\cbrx\pgfmathresult
\pgfmathparse{\cbly}\let\cbry\pgfmathresult
\pgfmathparse{neg(\cbry)}\let\crbx\pgfmathresult
\pgfmathparse{neg(\cbrx)}\let\crby\pgfmathresult
\pgfmathparse{\crbx}\let\crtx\pgfmathresult
\pgfmathparse{neg(\crby)}\let\crty\pgfmathresult
\pgfmathparse{\crty}\let\ctrx\pgfmathresult
\pgfmathparse{\crtx}\let\ctry\pgfmathresult
\pgfmathparse{neg(\ctrx)}\let\ctlx\pgfmathresult
\pgfmathparse{\ctry}\let\ctly\pgfmathresult
\pgfmathparse{neg(\crtx)}\let\cltx\pgfmathresult
\pgfmathparse{\crty}\let\clty\pgfmathresult
\pgfmathparse{neg(\crbx)}\let\clbx\pgfmathresult
\pgfmathparse{\crby)}\let\clby\pgfmathresult



\pgfmathparse{\cblx*cos(\rot) - \cbly*sin(\rot)}\let\cblxr\pgfmathresult
\pgfmathparse{\cblx*sin(\rot) + \cbly*cos(\rot)}\let\cblyr\pgfmathresult
\pgfmathparse{\cbrx*cos(\rot) - \cbry*sin(\rot)}\let\cbrxr\pgfmathresult
\pgfmathparse{\cbrx*sin(\rot) + \cbry*cos(\rot)}\let\cbryr\pgfmathresult
\pgfmathparse{\clbx*cos(\rot) - \clby*sin(\rot)}\let\clbxr\pgfmathresult
\pgfmathparse{\clbx*sin(\rot) + \clby*cos(\rot)}\let\clbyr\pgfmathresult
\pgfmathparse{\cltx*cos(\rot) - \clty*sin(\rot)}\let\cltxr\pgfmathresult
\pgfmathparse{\cltx*sin(\rot) + \clty*cos(\rot)}\let\cltyr\pgfmathresult
\pgfmathparse{\ctrx*cos(\rot) - \ctry*sin(\rot)}\let\ctrxr\pgfmathresult
\pgfmathparse{\ctrx*sin(\rot) + \ctry*cos(\rot)}\let\ctryr\pgfmathresult
\pgfmathparse{\ctlx*cos(\rot) - \ctly*sin(\rot)}\let\ctlxr\pgfmathresult
\pgfmathparse{\ctlx*sin(\rot) + \ctly*cos(\rot)}\let\ctlyr\pgfmathresult
\pgfmathparse{\crtx*cos(\rot) - \crty*sin(\rot)}\let\crtxr\pgfmathresult
\pgfmathparse{\crtx*sin(\rot) + \crty*cos(\rot)}\let\crtyr\pgfmathresult
\pgfmathparse{\crbx*cos(\rot) - \crby*sin(\rot)}\let\crbxr\pgfmathresult
\pgfmathparse{\crbx*sin(\rot) + \crby*cos(\rot)}\let\crbyr\pgfmathresult


\coordinate (AC') at ({\cblxr+\centerx},{\cblyr+\centery});
\coordinate (BC') at ({\cbrxr+\centerx},{\cbryr+\centery});
\coordinate (CC') at ({\crbxr+\centerx},{\crbyr+\centery});
\coordinate (DC') at ({\crtxr+\centerx},{\crtyr+\centery});
\coordinate (EC') at ({\ctrxr+\centerx},{\ctryr+\centery});
\coordinate (FC') at ({\ctlxr+\centerx},{\ctlyr+\centery});
\coordinate (GC') at ({\cltxr+\centerx},{\cltyr+\centery});
\coordinate (HC') at ({\clbxr+\centerx},{\clbyr+\centery});

\filldraw [black] (AC') circle (2pt)
            (BC') circle (2pt)
            (CC') circle (2pt)
            (DC') circle (2pt)
            (EC') circle (2pt)
            (FC') circle (2pt)
            (GC') circle (2pt)
            (HC') circle (2pt);

\draw [shorten >=-2cm, >=stealth](AC')--(0,0);
\draw [shorten >=-2cm](BC')--(0,0);
\draw [shorten >=-2cm](GC')--(0,0);
\draw [shorten >=-2cm](HC')--(0,0);

\draw [-] (-3,-1) -- (3,-1);

\node[draw] at (2,-1.2) {Focal Plane};

\coordinate (P1) at (-3,-1);
\coordinate (P2) at (3,-1);
\clip (P1) -- (P2);

\end{tikzpicture}
\end{document}

在此处输入图片描述

现在我想找到从矩形圆周“内部”的点发出的射线与焦平面的交点,这些射线穿过原点。我所做的是从这些点到原点创建线,然后缩短它们,使它们与焦平面相交。我如何在相交后切断缩短的线?另外这是我的第一个 tikz 人物,所以请保持低调 :P

答案1

添加 coordinate[pos=2](b);以获得足够长的线并得到一个交点。您需要使用交点库。

一些例子

shorten在第一种情况下,两条线没有交点。在第二种情况下,你可以使用一个交点,但路径仅定义在给定点之间,如第一种情况。在第三种情况下,您可以得到一个交点。

在此处输入图片描述

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
\draw[name path=d1] (0,0) -- (2,0);
\draw[name path=d2] (0,-2) -- (2,-0.5); 
%\fill [red, name intersections={of=d1 and d2, by=x}]
%       (x) circle (2pt); 
\end{tikzpicture}

\vspace{1cm}
\begin{tikzpicture}
\draw[name path=d1,shorten >=-2cm] (0,0) -- (2,0) ;
\draw[name path=d2,shorten >=-2cm] (0,-2) -- (2,-0.5); 
% \fill [red, name intersections={of=d1 and d2, by=x}]
%        (x) circle (2pt);  
\end{tikzpicture}

\vspace{1cm}  
\begin{tikzpicture}
\draw[shorten >=-2cm] (0,0) -- (2,0)  coordinate[pos=2](b); 
 \path[name path=d1] (0,0)--(b);         
\draw[shorten >=-2cm] (0,-2) -- (2,-0.5)  coordinate[pos=2](c);
 \path[name path=d2] (0,-2)--(c);   
 \fill [red, name intersections={of=d1 and d2, by=x}]
        (x) circle (2pt);  
\end{tikzpicture}  
\end{document}  

主要代码

\documentclass[11pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{%
  arrows,
  calc ,intersections
}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[scale=1]


%Draw axis
\begin{scope}[>=latex]
\draw [->] (-3,0) -- (3,0);
\draw [->] (0,-1.5) -- (0,6.5);
\end{scope}
%\draw[step=.5,gray,very thin] (-2.9,-0.9) grid (2.9,6.4);

\filldraw [black] (0,0) circle (2pt);


\def\centerx{0}
\def\centery{4}

\def\side{3}
\def\rot{40}
\def\sidePerc{0.25}
\pgfmathparse{\side * sqrt(2)/2 }\let\veclen\pgfmathresult

%llx : Lower left x
%lly : Lower left y
%lrx : Lower right x
%urx : Upper right x


%Vector of lower left point (Lower Left x and y)
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\llx\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\lly\pgfmathresult

%Vector of lower right point (Lower Left x and y)
\pgfmathparse{\veclen*sqrt(2)/2}\let\lrx\pgfmathresult
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\lry\pgfmathresult

%Vector of upper right point (Lower Left x and y)
\pgfmathparse{\veclen*sqrt(2)/2}\let\urx\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\ury\pgfmathresult

%Vector of upper left left point (Lower Left x and y)
\pgfmathparse{neg(\veclen)*sqrt(2)/2}\let\ulx\pgfmathresult
\pgfmathparse{\veclen*sqrt(2)/2}\let\uly\pgfmathresult

%Rotation of the above vectors
\pgfmathparse{\llx*cos(\rot) - \lly*sin(\rot)}\let\llxr\pgfmathresult
\pgfmathparse{\llx*sin(\rot) + \lly*cos(\rot)}\let\llyr\pgfmathresult

\pgfmathparse{\lrx*cos(\rot) - \lry*sin(\rot)}\let\lrxr\pgfmathresult
\pgfmathparse{\lrx*sin(\rot) + \lry*cos(\rot)}\let\lryr\pgfmathresult

\pgfmathparse{\urx*cos(\rot) - \ury*sin(\rot)}\let\urxr\pgfmathresult
\pgfmathparse{\urx*sin(\rot) + \ury*cos(\rot)}\let\uryr\pgfmathresult

\pgfmathparse{\ulx*cos(\rot) - \uly*sin(\rot)}\let\ulxr\pgfmathresult
\pgfmathparse{\ulx*sin(\rot) + \uly*cos(\rot)}\let\ulyr\pgfmathresult


%\coordinate (A) at ({\llx+\centerx},{\lly+\centery});
%\coordinate (B) at ({\lrx+\centerx},{\lry+\centery});
%\coordinate (C) at ({\urx+\centerx},{\ury+\centery});
%\coordinate (D) at ({\ulx+\centerx},{\uly+\centery});
%
%%Draw the points
%\filldraw [black] (A) circle (2pt)
%           (B) circle (2pt)
%           (C) circle (2pt)
%           (D) circle (2pt);
%
%%draw the square
%\draw (A)--(B)--(C)--(D)--(A);


\coordinate (A') at ({\llxr+\centerx},{\llyr+\centery});
\coordinate (B') at ({\lrxr+\centerx},{\lryr+\centery});
\coordinate (C') at ({\urxr+\centerx},{\uryr+\centery});
\coordinate (D') at ({\ulxr+\centerx},{\ulyr+\centery});

%Draw the points
\filldraw [black] (A') circle (2pt)
            (B') circle (2pt)
            (C') circle (2pt)
            (D') circle (2pt);

%draw the square
\draw (A')--(B')--(C')--(D')--(A');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%               Circle Points Section
%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%distance in side percentade from y axis or the lower left "circles" points
\pgfmathparse{sqrt( (\sidePerc*\sidePerc+0.25)*\side*\side )}\let\pveclen\pgfmathresult
\pgfmathparse{asin(\sidePerc*\side/\pveclen)}\let\angle\pgfmathresult

%cblx : Circle bottom left x
%clbx : Left bottom y


%Vector of lower left point (Lower Left x and y)
\pgfmathparse{neg(\pveclen*sin(\angle))}\let\cblx\pgfmathresult
\pgfmathparse{neg(\pveclen*cos(\angle))}\let\cbly\pgfmathresult

\pgfmathparse{neg(\cblx)}\let\cbrx\pgfmathresult
\pgfmathparse{\cbly}\let\cbry\pgfmathresult

\pgfmathparse{neg(\cbry)}\let\crbx\pgfmathresult
\pgfmathparse{neg(\cbrx)}\let\crby\pgfmathresult

\pgfmathparse{\crbx}\let\crtx\pgfmathresult
\pgfmathparse{neg(\crby)}\let\crty\pgfmathresult

\pgfmathparse{\crty}\let\ctrx\pgfmathresult
\pgfmathparse{\crtx}\let\ctry\pgfmathresult

\pgfmathparse{neg(\ctrx)}\let\ctlx\pgfmathresult
\pgfmathparse{\ctry}\let\ctly\pgfmathresult

\pgfmathparse{neg(\crtx)}\let\cltx\pgfmathresult
\pgfmathparse{\crty}\let\clty\pgfmathresult

\pgfmathparse{neg(\crbx)}\let\clbx\pgfmathresult
\pgfmathparse{\crby)}\let\clby\pgfmathresult


%\coordinate (AC) at ({\cblx+\centerx},{\cbly+\centery});
%\coordinate (BC) at ({\cbrx+\centerx},{\cbry+\centery});
%\coordinate (CC) at ({\crbx+\centerx},{\crby+\centery});
%\coordinate (DC) at ({\crtx+\centerx},{\crty+\centery});
%\coordinate (EC) at ({\ctrx+\centerx},{\ctry+\centery});
%\coordinate (FC) at ({\ctlx+\centerx},{\ctly+\centery});
%\coordinate (GC) at ({\cltx+\centerx},{\clty+\centery});
%\coordinate (HC) at ({\clbx+\centerx},{\clby+\centery});
%
%\filldraw [black] (AC) circle (2pt)
%           (BC) circle (2pt)
%           (CC) circle (2pt)
%           (DC) circle (2pt)
%           (EC) circle (2pt)
%           (FC) circle (2pt)
%           (GC) circle (2pt)
%           (HC) circle (2pt);

%%%%%%%%%%%%%% Rotations %%%%%%%%%%%%%

\pgfmathparse{\cblx*cos(\rot) - \cbly*sin(\rot)}\let\cblxr\pgfmathresult
\pgfmathparse{\cblx*sin(\rot) + \cbly*cos(\rot)}\let\cblyr\pgfmathresult

\pgfmathparse{\cbrx*cos(\rot) - \cbry*sin(\rot)}\let\cbrxr\pgfmathresult
\pgfmathparse{\cbrx*sin(\rot) + \cbry*cos(\rot)}\let\cbryr\pgfmathresult

\pgfmathparse{\clbx*cos(\rot) - \clby*sin(\rot)}\let\clbxr\pgfmathresult
\pgfmathparse{\clbx*sin(\rot) + \clby*cos(\rot)}\let\clbyr\pgfmathresult

\pgfmathparse{\cltx*cos(\rot) - \clty*sin(\rot)}\let\cltxr\pgfmathresult
\pgfmathparse{\cltx*sin(\rot) + \clty*cos(\rot)}\let\cltyr\pgfmathresult

\pgfmathparse{\ctrx*cos(\rot) - \ctry*sin(\rot)}\let\ctrxr\pgfmathresult
\pgfmathparse{\ctrx*sin(\rot) + \ctry*cos(\rot)}\let\ctryr\pgfmathresult

\pgfmathparse{\ctlx*cos(\rot) - \ctly*sin(\rot)}\let\ctlxr\pgfmathresult
\pgfmathparse{\ctlx*sin(\rot) + \ctly*cos(\rot)}\let\ctlyr\pgfmathresult

\pgfmathparse{\crtx*cos(\rot) - \crty*sin(\rot)}\let\crtxr\pgfmathresult
\pgfmathparse{\crtx*sin(\rot) + \crty*cos(\rot)}\let\crtyr\pgfmathresult

\pgfmathparse{\crbx*cos(\rot) - \crby*sin(\rot)}\let\crbxr\pgfmathresult
\pgfmathparse{\crbx*sin(\rot) + \crby*cos(\rot)}\let\crbyr\pgfmathresult


\coordinate (AC') at ({\cblxr+\centerx},{\cblyr+\centery});
\coordinate (BC') at ({\cbrxr+\centerx},{\cbryr+\centery});
\coordinate (CC') at ({\crbxr+\centerx},{\crbyr+\centery});
\coordinate (DC') at ({\crtxr+\centerx},{\crtyr+\centery});
\coordinate (EC') at ({\ctrxr+\centerx},{\ctryr+\centery});
\coordinate (FC') at ({\ctlxr+\centerx},{\ctlyr+\centery});
\coordinate (GC') at ({\cltxr+\centerx},{\cltyr+\centery});
\coordinate (HC') at ({\clbxr+\centerx},{\clbyr+\centery});

\filldraw [black] (AC') circle (2pt)
                  (BC') circle (2pt)
                  (CC') circle (2pt)
                  (DC') circle (2pt)
                  (EC') circle (2pt)
                  (FC') circle (2pt)
                  (GC') circle (2pt)
                  (HC') circle (2pt);
%[shorten >=-2cm]
\draw [ >=stealth](AC')--(0,0);
\draw (BC')--(0,0) coordinate[pos=2](b);
\draw (GC')--(0,0) coordinate[pos=2](g); 
\draw (HC')--(0,0) coordinate[pos=2](h); 

\path[name path=bc] (BC')--(b);
\path[name path=gc] (GC')--(g);
\path[name path=hc] (HC')--(h);
\draw [-,name path=FP] (-3,-1) -- (3,-1);

\node[draw] at (2,-1.2) {Focal Plane};
\fill [red, name intersections={of=FP and bc, by=xb}]
       (xb) circle (2pt);
\fill [red, name intersections={of=FP and gc, by=xg}]
       (xg) circle (2pt);  
\fill [red, name intersections={of=FP and hc, by=xh}]
       (xh) circle (2pt); 
\draw  (0,0) -- (xb); \draw  (0,0) --(xg); \draw  (0,0) --(xh) ;
\coordinate (P1) at (-3,-1);
\coordinate (P2) at (3,-1);
\clip (P1) -- (P2);


\end{tikzpicture}

\end{figure}
\end{document}   

在此处输入图片描述

相关内容