答案1
这是一个无需任何数学运算的解决方案,只需简单使用库即可calc
:
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw (-5,0) -- (5,0)
(0,-5) -- (0,5);
\coordinate (I) at (0,2);
\coordinate (A) at (-4,0);
\coordinate (B) at (-2,0);
\draw (A) -- ($(A)!2!(I)$)
(B) -- ($(B)!2!(I)$);
\end{tikzpicture}
\end{document}
答案2
使用此代码:
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}[scale=2]
% axex and grid
\draw[gray!15] (-3.6,-3.6) grid [step=0.5] (3.6,3.6);
\draw[-latex] (-3.6,0) -- (3.6,0) node [right] {$x$};
\draw[-latex] (0,-3.6) -- (0,3.6) node [above] {$y$};
\foreach \i in {-3.5,-3.0,...,-.5,0.5,1,1.5,...,3.5}
\draw (\i,0.05) -- (\i,-0.05) node [below] {\tiny $\i$};
\foreach\i in {-3.5,-3.0,...,-.5,0.5,1,1.5,...,3.5}
\draw (0.05,\i) -- (-0.05,\i) node [left] {\tiny $\i$};
\draw[blue,line width=3pt] (-2,-1)--(1,2);
\draw[cyan,line width=3pt] (-1,-1)--(1,3);
\end{tikzpicture}
\end{document}
事实上,如果 y 轴上的点是 (0,1),则例如有直线 y=x+1 和 y= 2x+1 在 y 轴 (0,1) 处交叉。您必须为每条直线选择 2 个点才能绘制它们。