我正在尝试创建简单的工作表,让学生可以:(a)看到一个随机生成的线性图并且必须确定方程,或者(b)给出一个随机生成的线性方程并且必须绘制线条。
如何使图形停留在平面的给定部分?即 x 和 y 均为 -10 到 10。
是否有一些“简单”的方法来做到这一点,或者我必须使用将随机生成的斜率\m
和 y 截距\b
与适当的域或范围关联起来的公式?
\documentclass{article}
\usepackage{pgf}
\pgfmathsetseed{\number\pdfrandomseed}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}
\newcommand{\LinearEquation}
{%
%slope
\pgfmathsetmacro{\m}{int(random(0,8)-4)}%
%y-intercept
\pgfmathsetmacro{\b}{int(random(0,8)-4)}%
% Linear Equation
\(y={\m}x+\b\)
}
\LinearEquation
\begin{tikzpicture}[scale=0.3]
\draw[help lines, gray, thin] (-10,-10) grid (10,10);
\draw[very thick,<->] (-10.3,0)--(10.3,0);
\draw[very thick,<->] (0,-10.3)--(0,10.3);
\draw[red, very thick, , range=-10:10, <->] plot (\x,\m*\x+\b); %this did not work
\end{tikzpicture}
\end{document}
答案1
假设您不需要线条的箭头提示,只需\clip (-10,-10) rectangle (10,10);
在 之前添加plot
。并且在绘图选项中,您想要domain
,而不是range
。
还要注意\b
已经定义了(参见宏的简称),所以最好避免使用它。
\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\pagestyle{empty}
\newcommand{\LinearEquation}
{%
%slope
\pgfmathsetmacro{\Slope}{int(random(0,8)-4)}%
%y-intercept
\pgfmathsetmacro{\Intercept}{int(random(0,8)-4)}%
% Linear Equation
\(y={\Slope}x+\Intercept\)
}
\begin{document}
\LinearEquation
\begin{tikzpicture}[scale=0.3]
\draw[help lines, gray, thin] (-10,-10) grid (10,10);
\draw[very thick,<->] (-10.3,0)--(10.3,0);
\draw[very thick,<->] (0,-10.3)--(0,10.3);
\clip (-10,-10) rectangle (10,10);
\draw[red, very thick, , domain=-10:10] plot (\x,\Slope*\x+\intercept);
\end{tikzpicture}
\end{document}
所有内容都在一个宏中
\documentclass{article}
\usepackage{tikz}
\pgfmathsetseed{\number\pdfrandomseed}
\pagestyle{empty}
\newcommand{\LinearEquation}
{%
%slope
\pgfmathsetmacro{\Slope}{int(random(0,8)-4)}%
%y-intercept
\pgfmathsetmacro{\Intercept}{int(random(0,8)-4)}%
\begin{tikzpicture}[scale=0.25]
\draw[help lines, gray, thin] (-10,-10) grid (10,10);
\node [above right] at (-10,10) {\(y={\Slope}x+\Intercept\)};
\draw[very thick,<->] (-10.3,0)--(10.3,0);
\draw[very thick,<->] (0,-10.3)--(0,10.3);
\clip (-10,-10) rectangle (10,10);
\draw[red, very thick, , domain=-10:10] plot (\x,\Slope*\x+\Intercept);
\end{tikzpicture}%
}
\begin{document}
\LinearEquation
\LinearEquation
\LinearEquation
\LinearEquation
\LinearEquation
\LinearEquation
\end{document}