| 1 |
% TeXdraw toolbox macros, useful for extended TeXdraw commands |
| 2 |
|
| 3 |
% $Id: txdtools.tex 1.11 2019/04/18 TeXdraw-v2r3 $ |
| 4 |
|
| 5 |
% Copyright (C) 1991-2019 Peter Kabal |
| 6 |
|
| 7 |
% This work is licensed under the Creative Commons Attribution (CC-BY) |
| 8 |
% License, any version. To view the licenses, visit |
| 9 |
% creativecommons.org/licenses/by or send a letter to |
| 10 |
% Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. |
| 11 |
|
| 12 |
% Peter Kabal |
| 13 |
% Department of Electrical & Computer Engineering |
| 14 |
% McGill University |
| 15 |
|
| 16 |
% peter dot kabal at mcgill dot ca |
| 17 |
% http://www-mmsp.ece.mcgill.ca/MMSP/Documents/Software/ |
| 18 |
|
| 19 |
% =============================================================== |
| 20 |
|
| 21 |
% These macros use temporary count registers defined by TeXdraw |
| 22 |
% \t@counta \t@pixa |
| 23 |
% \t@countb \t@pixb |
| 24 |
% \t@countc \t@pixc |
| 25 |
% \t@pixd |
| 26 |
|
| 27 |
\chardef\catamp=\the\catcode`\@ |
| 28 |
\catcode`\@=11 |
| 29 |
|
| 30 |
|
| 31 |
% ===== Real arithmetic |
| 32 |
% Real addition |
| 33 |
% #1 - summand |
| 34 |
% #2 - summand |
| 35 |
% #3 - macro name to capture the real result |
| 36 |
\def\realadd #1#2#3{\dimen0=#1pt |
| 37 |
\dimen2=#2pt |
| 38 |
\advance \dimen0 by \dimen2 |
| 39 |
\edef #3{\expandafter\c@lean\the\dimen0}} |
| 40 |
|
| 41 |
% Real division |
| 42 |
% #1 - numerator |
| 43 |
% #2 - denominator (divisor) |
| 44 |
% #3 - macro name to capture the real result |
| 45 |
\def\realdiv #1#2#3{\dimen0=#1pt |
| 46 |
\t@counta=\dimen0 |
| 47 |
\dimen0=#2pt |
| 48 |
\t@countb=\dimen0 |
| 49 |
\intdiv \t@counta \t@countb #3} |
| 50 |
|
| 51 |
% ===== Integer arithmetic |
| 52 |
|
| 53 |
% Length of the hypotenuse |
| 54 |
% Find the length of a vector, lenhyp = sqrt(dx*dx + dy*dy) |
| 55 |
% #1 - integer value, dx |
| 56 |
% #2 - integer value, dy |
| 57 |
% #3 - count register to capture the integer value |
| 58 |
\def\lenhyp #1#2#3{\t@counta=#1% |
| 59 |
\multiply \t@counta by \t@counta |
| 60 |
\t@countb=#2% |
| 61 |
\multiply \t@countb by \t@countb |
| 62 |
\advance \t@counta by \t@countb |
| 63 |
\sqrtnum \t@counta #3} |
| 64 |
|
| 65 |
% Square root of an integer |
| 66 |
% Newton-Raphson iteration to find the square root, integer argument |
| 67 |
% Let the current estimate of the square root of x be b(k). |
| 68 |
% Form an error function, e(k)=b(k)*b(k)-x. Follow the gradient of the |
| 69 |
% error to calculate the next guess, |
| 70 |
% |
| 71 |
% e(k) - 0 d e(k) b(k) + x/b(k) |
| 72 |
% ---------- = ------ = 2*b(k) ==> b(k+1) = ------------- |
| 73 |
% b(k)-b(k+1) d b(k) 2 |
| 74 |
% |
| 75 |
% Note this iteration does not work for x=0, since the guess is then b(k)=0. |
| 76 |
% Rename the count registers to have more suggestive names |
| 77 |
\let\bk=\t@counta |
| 78 |
\let\bn=\t@countb |
| 79 |
\let\xval=\t@countc |
| 80 |
\def\sqrtnum #1#2{\xval=#1% |
| 81 |
\bk=\xval |
| 82 |
\loop |
| 83 |
\bn=\xval |
| 84 |
\divide \bn by \bk |
| 85 |
\advance \bn by \bk |
| 86 |
\advance \bn by 1 % rounding |
| 87 |
\divide \bn by 2 |
| 88 |
\ifnum \bn < \bk |
| 89 |
\bk=\bn |
| 90 |
\repeat |
| 91 |
#2=\bn} |
| 92 |
|
| 93 |
% ===== Coordinate macros |
| 94 |
|
| 95 |
% Return the coordinates of the current position |
| 96 |
% #1 - macro name to capture the x-coordinate |
| 97 |
% #2 - macro name to capture the y-coordinate |
| 98 |
\def\currentpos #1#2{\t@pixa=\x@pix |
| 99 |
\advance \t@pixa by -\x@segoffpix |
| 100 |
\pixtocoord \t@pixa #1 |
| 101 |
\t@pixa=\y@pix |
| 102 |
\advance \t@pixa by -\y@segoffpix |
| 103 |
\pixtocoord \t@pixa #2} |
| 104 |
|
| 105 |
% Length of a vector |
| 106 |
% Find the length of the vector between coordinate (#1 #2) and |
| 107 |
% coordiante (#3 #4). The length is expressed relative to the |
| 108 |
% current scaling. |
| 109 |
% (#1 #2) - vector start coordinates |
| 110 |
% (#3 #4) - vector end coordinates |
| 111 |
% #5 - macro name to receive the length |
| 112 |
\def\vectlen (#1 #2)(#3 #4)#5{\getpos (#1 #2)\x@arga\y@arga |
| 113 |
\getpos (#3 #4)\x@argb\y@argb |
| 114 |
\coordtopix \x@arga \t@pixa |
| 115 |
\coordtopix \x@argb \t@pixb |
| 116 |
\advance \t@pixb by -\t@pixa |
| 117 |
\coordtopix \y@arga \t@pixc |
| 118 |
\coordtopix \y@argb \t@pixd |
| 119 |
\advance \t@pixd by -\t@pixc |
| 120 |
\lenhyp \t@pixb \t@pixd \t@pixc |
| 121 |
\pixtocoord \t@pixc #5} |
| 122 |
|
| 123 |
% Cossine and sine |
| 124 |
% Find the cosine and sine of the angle of a vector directed from |
| 125 |
% the coordinate (#1 #2) to the coordinate (#3 #4). |
| 126 |
% (#1 #2) - start coordinates |
| 127 |
% (#3 #4) - end coordinates |
| 128 |
% #5 - macro name to receive the cosine of the angle |
| 129 |
% #6 - macro name to receive the sine of the angle |
| 130 |
\def\cossin (#1 #2)(#3 #4)#5#6{\getpos (#1 #2)\x@arga\y@arga |
| 131 |
\getpos (#3 #4)\x@argb\y@argb |
| 132 |
\coordtopix \x@arga \t@pixa |
| 133 |
\coordtopix \x@argb \t@pixb |
| 134 |
\advance \t@pixb by -\t@pixa |
| 135 |
\coordtopix \y@arga \t@pixc |
| 136 |
\coordtopix \y@argb \t@pixd |
| 137 |
\advance \t@pixd by -\t@pixc |
| 138 |
\lenhyp \t@pixb \t@pixd \t@pixc |
| 139 |
\intdiv \t@pixb\t@pixc #5% |
| 140 |
\intdiv \t@pixd\t@pixc #6} |
| 141 |
|
| 142 |
\catcode`\@=\catamp |