pgf-tikz / pgf

A Portable Graphic Format for TeX
https://pgf-tikz.github.io/
1.15k stars 109 forks source link

Full/More operational `/pgf/trig format=rad` #1270

Open muzimuzhi opened 1 year ago

muzimuzhi commented 1 year ago

Brief outline of the bug

While trigonometric math functions already work with /pgf/trig format=rad, typically some interfaces that accept a math expression as an angle are not yet. Currently /pgf/trig format=rad is documented as experimental https://github.com/pgf-tikz/pgf/blob/42bd29bdd61beae8d9b46f8f83505d15e301ae02/doc/generic/pgf/pgfmanual-en-math-parsing.tex#L1047-L1052

Tikz arc path operation arc [start angle=<angle>, end angle=<angle>, radius=<radius>] or arc (<angle>:<angle>:<radius>) (old grammar) and its beneath pgf path command \pgfpatharc{<start angle>}{<end angle>}{<radius> [and <y-radius>]} are a pair of such examples.

Minimal working example (MWE)

(adapted from https://tex.stackexchange.com/q/685949)

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw[trig format=rad, red]
    (0:1) arc[start angle=0, end angle=pi, radius=1];
  \draw[trig format=rad, red!50, xshift=-.1cm]
    (0:1) arc (0:pi:1);

  \draw[blue, xshift=.1cm] (0:1) arc (0:180:1);
\end{tikzpicture}
\end{document}

image

Proposal

Based on @Qrrbrbirlbel's answer https://tex.stackexchange.com/a/685953

From 7ac6e4254bad4e5e23b3daaf9f86c286496b2363 Mon Sep 17 00:00:00 2001
From: muzimuzhi <muzimuzhi@gmail.com>
Date: Wed, 2 Aug 2023 02:04:18 +0800
Subject: [PATCH] Make `\pgfpatharc` work with `trig format=rad`

Signed-off-by: muzimuzhi <muzimuzhi@gmail.com>
---
 .../pgf/basiclayer/pgfcorepathconstruct.code.tex     | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex b/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex
index cafc5329..376cdc70 100644
--- a/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex
+++ b/tex/generic/pgf/basiclayer/pgfcorepathconstruct.code.tex
@@ -288,16 +288,14 @@

 \def\pgfpatharc#1#2#3{%
   {%
-    \pgfmathparse{#1}\let\pgf@temp@a=\pgfmathresult%
-    \pgfmathparse{#2}\let\pgf@temp@b=\pgfmathresult%
+    \pgf@arc@get@angle\pgf@arc@local@angle@a{#1}%
+    \pgf@arc@get@angle\pgf@arc@local@angle@b{#2}%
     \pgfutil@in@{and }{#3}%
     \ifpgfutil@in@%
       \pgf@arc@get@radii#3\pgf@arc@stop%
     \else
       \pgf@arc@get@radii#3and #3\pgf@arc@stop%
     \fi%
-    \pgf@arc@local@angle@a=\pgf@temp@a pt%
-    \pgf@arc@local@angle@b=\pgf@temp@b pt%
     \loop%
       \pgfutil@tempdima=\pgf@arc@local@angle@a%
       \advance\pgfutil@tempdima by-\pgf@arc@local@angle@b\relax%
@@ -336,6 +334,11 @@
 \dimendef\pgf@arc@local@angle@b=1
 \dimendef\pgf@arc@temp=2

+\def\pgf@arc@get@angle#1#2{%
+  \pgfmathsetlength{#1}{#2}%
+  \pgfmathiftrigonometricusesdeg{}{\pgfmath@scale@rad@to@deg#1}%
+}
+
 \def\pgf@arc@get@radii#1and #2\pgf@arc@stop{%
   \pgfmathparse{#1}\let\pgf@arc@radius@a=\pgfmathresult%
   \pgfmathparse{#2}\let\pgf@arc@radius@b=\pgfmathresult%
@@ -344,6 +347,7 @@

 \def\pgf@arc{%
   {%
+  \pgfset{/pgf/trig format=deg}%
   \pgfutil@tempdima=\pgf@arc@radius@a pt%
   \pgfutil@tempdimb=\pgf@arc@radius@b pt%
   %
-- 
2.41.0

\pgf@arc@get@angle<length>{<math expr>} may be generalized to something like \pgf@get@angle.