绘制圆弧命令问题

绘制圆弧命令问题

我在外面定义了以下代码\begin{document}

\newcommand{\point}[2] {
    ({#1}, {#2})
}

\newcommand{\arcpoints}[6] {
    ({#1}:{atan((#5 - #3)/(#4 - #2))}:{#6})
}

在线上绘制圆弧时出现错误多于 \end{tikzpicture}

\begin{tikzpicture}[background rectangle/.style={fill=black}, show background rectangle]
    \newcommand{\f}[1] {
        {#1*#1 - #1/2 + 1/2}
    }

    \draw[transparent] (-1, -1) grid (3, 3);
    \draw[->] (-1, 0) -- (3, 0);
    \draw[->] (0, -1) -- (0, 3);

    \draw[dotted] \point{1/4}{\f{1/4}} -- \point{1/4}{0};
    \draw[dotted] \point{3/2}{0} -- \point{3/2}{\f{3/2}};

    \draw[dotted] \point{0}{\f{1/4}} -- \point{1/4}{\f{1/4}};
    \draw[dotted] \point{0}{\f{3/2}} -- \point{3/2}{\f{3/2}};

    \node[scale=0.6] at \point{1/4}{-0.25} {$x$};
    \node[scale=0.6] at \point{3/2}{-0.25} {$x + h$};

    \node[scale=0.6, left] at \point{-0.1}{\f{1/4}} {$f\left(x\right)$};
    \node[scale=0.6, left] at \point{-0.1}{\f{3/2}} {$f\left(x + h\right)$};

    \draw[blue, thin] \point{1/4}{\f{1/4}} -- \point{3/2}{\f{1/4}};
    \draw[blue, thin] \point{3/2}{\f{1/4}} -- \point{3/2}{\f{3/2}};
    \draw[scale=1, domain={-1}:{1/4*(1 + 41^(1/2))}, smooth, variable=\x, thick] plot ({\x}, {\f{\x}});
    \draw[red, thin] \point{3/2}{\f{3/2}} -- \point{1/4}{\f{1/4}};

    \fill[red, smooth] \point{1/4}{\f{1/4}} circle (1.25pt);
    \fill[red, smooth] \point{3/2}{\f{3/2}} circle (1.25pt);

    \draw[green, smooth] \point{1/4 + 0.3}{\f{1/4}} arc \arcpoints{0}{1/4}{\f{1/4}}{3/2}{\f{3/2}}{0.3};
\end{tikzpicture}

但是,当我写下以下内容时它似乎有效:

\draw[green, smooth] \point{1/4 + 0.3}{\f{1/4}} arc \arcpoints{0}{1}{1}{2}{2}{0.3};

我该如何解决这个问题以及我的语法有什么问题?

答案1

如我所见,您的问题已由@rmano 回答(+1)解决,但是,我不建议您采用这种绘制图像的方法。

  • 在我看来,它会让图像代码变得更长、更不清晰。比较一下以下两个定义图片坐标的符号:
    \point{0}{f(14)}
          (0,{f(14)})

两者定义的是相同的坐标。

  • 用于绘制角度指定 TiZ 已经定义了库,angles您可以通过它简单地绘制它。比较两个代码:
\draw[green, smooth] \point{1/4 + 0.3}{(1/4)} arc \arcpoints{0}{1/4}{f(1/4)}{3/2}{f(3/2)}{0.3};
\pic [draw=green, semithick, radius=7mm] {angle = C--A--B};

其中坐标 A、B 和 C 可以在绘制蓝色虚线时定义(参见下方的代码和图像)。

考虑到上述情况并(重新)定义你的图片元素样式,我建议(学习 LaTeX 和 TiZ)从以下 MWE(最小工作示例)开始,重现您的图像:

\documentclass[border=3.142592]{standalone}
\usepackage{tikz}
\usetikzlibrary{angles,
                backgrounds
                }

\begin{document}
    \begin{tikzpicture}[
background rectangle/.style={fill=yellow!20},           % <---
show background rectangle,
tcklbl/.style = {inner sep=1pt, font=\tiny},      % <---
declare function = {f(\t)=((\t)^2 - \t/2 + 1/2);}       % <===
                        ]
% axis, grid
\draw[gray=50, very thin] (-1, -1) grid (3, 3);
\draw[->] (-1, 0) -- (3, 0);
\draw[->] (0, -1) -- (0, 3);
% function
\draw[thick] plot[samples=51, variable=\x,
                  domain=-1:1.8507] (\x, {f(\x)});
% points on function
\draw[densely dotted, blue] 
    (0,{f(1/4)}) node[tcklbl,left] {$f(x)$}     -| coordinate (A) (1/4,0) node[tcklbl,below] {$x$};
\draw[densely dotted, blue] 
    (0,{f(3/2)}) node[tcklbl,left] {$f(x{+}h)$} -| coordinate (B) (3/2,0) node[tcklbl,below] {$x{+}h$};
\draw[semithick, blue]      (B) |- coordinate (C) (A);
\draw[semithick, red,fill]  (A) circle[radius=1pt] -- (B) circle[radius=1pt];
%
\pic [draw=green, semithick, radius=7mm] {angle = C--A--B};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

附录: 在 MWE 的上限中,函数域的上限由计算器计算,但如果你喜欢练习,如何在图像代码中做到这一点,而不是探索math库(TiZ 和 PGF 手册,第 94 章数学表达式(第 1029 页)。

通过使用\pgfmathsetmacro{<macro name>}{<expression>}您可以计算如下:

  • 函数:$f(x) = x^2 - \frac{x}{2} + \frac{1}{2}$ \item $x$,其中 $f(x)=3$ 可通过以下公式计算:
    \begin{align*}
x^2 - \frac{x}{2} + \frac{1}{2} & = 3 \Rightarrow   \\
                   2x^2 - x - 5 & = 0 \Rightarrow   \\
                              x & = \frac{1 + \sqrt{1 - 4\cdot2\cdot (-5)}}{2\cdot2}    \\
                                & = \frac{1 + \sqrt{41}}{4} = \boxed{1.8507}
   \end{align*}

考虑到上述情况,应在 MWE 中进行以下更改:

  • tikzpicture选项添加后
% right limit of domain:
\pgfmathsetmacro{\rbd}{(1 + sqrt(1 - 4*2*(-5)))/4}% axis, grid
  • 绘制替换宽度函数的代码行:
% function
\draw[thick] plot[samples=51, variable=\x,
                  domain=-1:\rbd] (\x, {f(\x)});    % see \rbd

答案2

我不确定您在这里试图做什么,但是 --- 路径中的宏扩展并不简单 --- 宏不是函数,因此您可能会遇到奇怪的惊喜(例如,#1^2如果传递-2变成-2^2会给出 -4...)。

我确实会使用一个函数:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\newcommand{\point}[2] {
    ({#1}, {#2})
}

\newcommand{\arcpoints}[6] {
    ({#1}:{atan((#5 - #3)/(#4 - #2))}:{#6})
}
\begin{document}
\begin{tikzpicture}[declare function={
        f(\x)=(\x)*(\x)-(\x)/2+1/2;
    },]
\draw[dotted] \point{1/4}{f(1/4)} -- \point{1/4}{0};
    \draw[dotted] \point{3/2}{0} -- \point{3/2}{f(3/2)};
    \draw[green, smooth] \point{1/4 + 0.3}{(1/4)} arc \arcpoints{0}{1/4}{f(1/4)}{3/2}{f(3/2)}{0.3};
\end{tikzpicture}
\end{document}

...虽然我还是不明白...的重点\point(双关语 ;-))

相关内容