pgfmathanglebetweenlines の使用初心者の質問

pgfmathanglebetweenlines の使用初心者の質問

tikz を使い始めたばかりで、 を使いたいのです\pgfmathanglebetweenlinesが、明らかに理解できていません。次のコードがパラメータの角度を報告することを期待していましたが、毎回0 o (または、毎回 45 oまたは 90 o\tryになることもあります)と出力されます。何を間違えたのでしょうか?

%
% preamble
%
\documentclass[10pt]{article}
\usepackage{amssymb,amsmath}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
%
% drawing.
%
\begin{document}
\def\try#1{
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$](O)at(0,0);
\coordinate[label=$X$](X)at(1,0);
\coordinate[label=$Y$](Y)at({cos(#1)},{sin(#1)});
\draw(Y)--(O)--(X);
\pgfmathanglebetweenlines{(O)}{(Y)}{(O)}{(X)}                                                                
\coordinate[label=right:{$\angle YOX=\pgfmathresult^\circ$}](label)at(X);             
\end{tikzpicture}
\\} 
\try{20}\try{45}\try{60}\try{180}\try{405}\try{-45}
\end{document}   

答え1

ここで問題がいくつかあります。まず、 には\pgfmathanglebetweenlines基本的なレイヤー座標の指定が必要です (この場合は\pgfpointanchor{Y}{center})。次に、\pgfmathresultは数学エンジンが使用されるたびに再定義されるため、タイプセットされるまでに角度の測定が失われます。

解決法を以下に示します。鋭角の補正(ドキュメントは\pgfmathanglebetweenlines正確ではないと思います)と角度を最も近い整数に丸める補正も追加しました。

\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\def\try#1{%
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$] (O) at (0,0);
\coordinate[label=$X$]       (X) at (1,0);
\coordinate[label=$Y$]       (Y) at (cos #1, sin #1);
\draw (Y) -- (O) -- (X);
\pgfmathanglebetweenlines%
  {\pgfpointanchor{O}{center}}{\pgfpointanchor{Y}{center}}
  {\pgfpointanchor{O}{center}}{\pgfpointanchor{X}{center}}
\pgfmathparse{int(round(min(\pgfmathresult, 360-\pgfmathresult))}% Correction
\let\angleyox=\pgfmathresult                                                              
\coordinate [label=right:{$\angle YOX=\angleyox^\circ$}] (label) at (X);             
\end{tikzpicture}
\\}
\begin{document}
\foreach \a in {20, 45, 60, 180, 405,-45}{\try{\a}}
\end{document}  

しかし、基本的なレイヤーは必要ありません。calcライブラリは必要なものをすべて提供します。

\documentclass[varwidth,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\try#1{%
\begin{tikzpicture}[scale=4]
\coordinate[label=below:$O$] (O) at (0,0);
\coordinate[label=$X$]       (X) at (1,0);
\coordinate[label=$Y$]       (Y) at (cos #1, sin #1);
\draw (Y) -- (O) -- (X);
\path let \p1=(O),\p2=(Y),\p3=(X),
   \n1={atan2(\y2-\y1,\x2-\x1)},
   \n2={atan2(\y3-\y1,\x3-\x1)} in
   node at (X) [right] {\pgfmathparse{int(abs(\n2-\n1))}% Correction
     $\angle YOX=\pgfmathresult^\circ$};
\end{tikzpicture}
\\}
\begin{document}
\foreach \a in {20, 45, 60, 180, 405,-45}{\try{\a}}
\end{document} 

ここに画像の説明を入力してください

関連情報