diff options
-rw-r--r-- | Master/texmf-dist/doc/metapost/byrne/README | 20 | ||||
-rw-r--r-- | Master/texmf-dist/doc/metapost/byrne/byrne-latex.pdf | bin | 0 -> 156788 bytes | |||
-rw-r--r-- | Master/texmf-dist/doc/metapost/byrne/byrne-latex.tex | 1244 | ||||
-rw-r--r-- | Master/texmf-dist/metapost/byrne/byrne.mp | 3852 | ||||
-rw-r--r-- | Master/texmf-dist/tex/latex/byrne/byrne.sty | 298 | ||||
-rwxr-xr-x | Master/tlpkg/bin/tlpkg-ctan-check | 2 | ||||
-rw-r--r-- | Master/tlpkg/tlpsrc/byrne.tlpsrc | 0 | ||||
-rw-r--r-- | Master/tlpkg/tlpsrc/collection-pictures.tlpsrc | 1 |
8 files changed, 5416 insertions, 1 deletions
diff --git a/Master/texmf-dist/doc/metapost/byrne/README b/Master/texmf-dist/doc/metapost/byrne/README new file mode 100644 index 00000000000..39cb5c1d21e --- /dev/null +++ b/Master/texmf-dist/doc/metapost/byrne/README @@ -0,0 +1,20 @@ +Name: byrne-latex +Version: 0.2.2 +License: GNU GPLv3 or later +Author: Sergey Slyusarev +Repository: https://github.com/jemmybutton/byrne-latex + +Description: +The MetaPost library used in this package was initially +written for ConTeXt version of Oliver Byrne's edition +of Euclid's "Elements." It provides functions for drawing basic +geometric primitives, such as lines, angles, polygons etc. +and for referencing them in text. LaTeX package provides +wrapper macros for MetaPost code. This package works with +LuaLaTeX. + +Contents: +byrne.mp — MetaPost library +byrne.sty — LaTeX package +byrne-latex.tex — documentation source code +byrne-latex.pdf — documentation pdf diff --git a/Master/texmf-dist/doc/metapost/byrne/byrne-latex.pdf b/Master/texmf-dist/doc/metapost/byrne/byrne-latex.pdf Binary files differnew file mode 100644 index 00000000000..d1b46ed3178 --- /dev/null +++ b/Master/texmf-dist/doc/metapost/byrne/byrne-latex.pdf diff --git a/Master/texmf-dist/doc/metapost/byrne/byrne-latex.tex b/Master/texmf-dist/doc/metapost/byrne/byrne-latex.tex new file mode 100644 index 00000000000..bbd12af27e0 --- /dev/null +++ b/Master/texmf-dist/doc/metapost/byrne/byrne-latex.tex @@ -0,0 +1,1244 @@ +\begin{filecontents}{byrne-latex.bib} +@book{Byrne1847, + author = {Byrne, Oliver}, + publisher = {William Pickering}, + title = {{The first six books of the elements of Euclid, in which coloured diagrams and symbols are used instead of letters for the greater ease of learners}}, + year = 1847 +} +@book{Tufte90, + author = {Tufte, Edward R.}, + isbn = {978-0-9613921-1-6}, + publisher = {Graphics Press}, + title = {{Envisioning Information}}, + year = 1990 +} +@misc{Slyusarev2017, + author = {Slyusarev, Sergey}, + title = {{MetaPost + ConTeXt rendition of Oliver Byrne's "The first six books of the Elements of Euclid"}}, + year = {2017}, + howpublished = {\url{https://github.com/jemmybutton/byrne-euclid}} +} +@misc{Rougeux2018, + author = {Rougeux, Nicholas}, + title = {{Byrne’s Euclid}}, + year = {2018}, + howpublished = {\url{https://www.c82.net/euclid/}} +} +@misc{Okabe2008, + author = {Okabe, Masataka and Ito, Kei}, + title = {{Color Universal Design (CUD) How to make figures and presentations that are friendly to Colorblind people}}, + year = {2008}, + howpublished = {\url{https://jfly.uni-koeln.de/color/}} +} +\end{filecontents} + +\documentclass{ltxdoc} +\usepackage{byrne} +\usepackage{ccicons,bxtexlogo,listings,hyperref} +\hypersetup{ + colorlinks=true, + linkcolor=blue, + filecolor=blue, + citecolor = black, + urlcolor=cyan, + } + +\lstset{ +language=MetaPost, +alsolanguage=TeX, +numbers=none, +basicstyle=\ttfamily\scriptsize +} + +\def\mpPre{textLabels := true;} + +\author{Sergey Slyusarev} +\title{``Byrne'' v. 0.2.2 package for \METAPOST\ and \LaTeX} + +\begin{document} +\maketitle + +\begin{abstract} +This document describes ``Byrne'' package for \METAPOST\ and \LaTeX. +\end{abstract} + +\begin{centering} + +This document is distributed under CC-BY-SA 4.0 license + +\ccbysa + +\href{https://github.com/jemmybutton/byrne-latex}{https://github.com/jemmybutton/byrne-latex} + +\end{centering} + +\section{Introduction} +Oliver Byrne's \href{https://archive.org/details/firstsixbooksofe00byrn/}{1847 quirky edition on the first six books of Euclid's ``Elements''}\cite{Byrne1847} is an interesting example of tight interplay between text and graphics in information visualization. The main feature of this book is that instead of relying on letter designations to describe lines, angles, etc., all the diagrams in it are colored and parts of these diagrams are directly incorporated into text. + +Recently this work met somewhat of a renaissance and in the span of a couple of years at least three independent attempts were made at reviving Byrne's work. In early 2017 the first six books \href{https://github.com/jemmybutton/byrne-euclid/}{remade in \ConTeXt} were published on GitHub\cite{Slyusarev2017}, in late 2018 an \href{https://www.c82.net/euclid/}{interactive web-version} was published by Nicholas Rougeux\cite{Rougeux2018} and in late 2019 a \href{https://www.kroneckerwallis.com/product/euclids-elements-completing-oliver-byrnes-work/}{printed version} of all Euclid's 15 books was published by Kronecker Wallis. All three are very different in terms of approach and goals. + +The \ConTeXt\ version's main objective was to develop a toolkit for applying Byrne's approach to a variety of geometry problems in the least time-consuming manner. This package is a \LaTeX\ (specifically \LuaLaTeX) version of the toolkit. It consists of two parts: \METAPOST\ code to describe and render geometric constructions and \LaTeX\ code to incorporate these constructions in text. + +\section{Requirements} + +This package works with \LuaLaTeX\ and requires \texttt{luamplib v2.23.0} or higher. +\section{Example} + +\begin{minipage}{\textwidth} +\vskip 0.5\baselineskip +\defineNewPicture{ +textLabels := true; +angleSize := 1cm; +pair A, B, C; +A := (0, 0); +B := (0, 2cm); +C := (3cm, 0); +byAngleDefine(C, B, A, byyellow, 0); +byAngleDefine(B, A, C, byblue, 0); +byAngleDefine(A, C, B, byred, 0); +draw byNamedAngleResized(); +byLineDefine(A, B, byred, 0, 0); +byLineDefine(B, C, byblue, 0, 0); +byLineDefine(C, A, byyellow, 0, 0); +draw byNamedLineSeq(0)(AB,BC,CA); +draw byLabelsOnPolygon(A, B, C)(0, 0); +} +~\hfill\drawCurrentPicture\hfill~ +\vskip \baselineskip + +In +\drawFromCurrentPicture[middle][triangleABC]{ +startAutoLabeling; +draw byNamedLineSeq(0)(AB,BC,CA); +stopAutoLabeling; +} +the angle \drawAngle{A} +is a right angle, therefore \triangleABC +is a right-angled triangle. +In it $\drawUnitLine{CB} > \drawUnitLine{AB}$ +and $\drawUnitLine{BC} > \drawUnitLine{AC}$. +\end{minipage} + +\vskip 2\baselineskip +The construction above is described like this: +\vskip 1.5\baselineskip + +\begin{lstlisting} +\usepackage{byrne} +... +\defineNewPicture{ % MetaPost code to describe the main diagram is placed inside + textLabels := true; % This turns text labels on + pair A, B, C; + A := (0, 0); + B := (0, 2cm); + C := (3cm, 0); + byAngleDefine(C, B, A, byyellow, 0); % This defines a yellow angle CBA + byAngleDefine(B, A, C, byblue, 0); + byAngleDefine(A, C, B, byred, 0); + draw byNamedAngleResized(); % Draws angles. + byLineDefine(A, B, byred, 0, 0); % This defines a red line AB. + byLineDefine(B, C, byblue, 0, 0); + byLineDefine(C, A, byyellow, 0, 0); + draw byNamedLineSeq(1)(AB,BC,CA); % Draws joint lines in sequence + draw byLabelsOnPolygon(A, B, C)(0, 0); % Draws text labels +} +\drawCurrentPicture % Draws the diagram + +In +\drawFromCurrentPicture[middle][triangleABC]{ + startAutoLabeling; + draw byNamedLineSeq(0)(AB,BC,CA); + stopAutoLabeling; +} +the angle \drawAngle{A} is a right angle, therefore \triangleABC\ is +a right-angled triangle. In it $\drawUnitLine{CB} > \drawUnitLine{AB}$ +and $\drawUnitLine{BC} > \drawUnitLine{AC}$. +\end{lstlisting} + +\vskip 1.5\baselineskip + +Several things to note here. + +In the \METAPOST\ part: +All points are ordinary \METAPOST\ pairs and can be defined and manipulated as such. Normally every part of the diagram is first defined (e.g. \texttt{byAngleDefine} or \texttt{byLineDefine}) and then is drawn (e.g. \texttt{draw byNamedAngleResized...} or \texttt{draw byNamedLineSeq...}). Also, although Byrne didn't use text labels in his book, they are implemented in this package \ref{textlabels}. Normally they are placed semi-manually in the main diagram (using functions like \texttt{byLabelsOnPolygon}) and mostly automatically in offspring pictures. By default they are turned off, and to turn them on \texttt{textLabels := true;} should be set. + +In the \LaTeX\ part: +The most general way to reference main diagram is to use \cs{drawFromCurrentPicture}. It will process arbitrary \METAPOST\ code based on the main diagram's code and output an image. However, it would be too cumbersome to use this route every time, so a few shortcuts are in place. If you do need to process arbitrary code, you can give picture a name (in the example it's \texttt{triangleABC}). It will define a macro of the same name \cs{triangleABC} which will output the same image again. For most common cases there are special macros which don't require you to write any \METAPOST\ code. For instance \cs{drawAngle} and \cs{drawUnitLine} allow you to call angles and lines simply by name. The names don't have to match the initial definition exactly. For instance, the angle in the example can be called \texttt{A}, \texttt{BAC} or \texttt{ABC} interchangeably, or the same line segment can be called either \texttt{BC} or \texttt{CB}. + +For more examples you can look at the source code of the \href{https://github.com/jemmybutton/byrne-euclid/}{\ConTeXt\ version of Byrne's book.} This package shares syntax with it and its code can be transferred to \LaTeX\ with minimal changes. + +The following reference guide is not complete and will be updated in the future. + +\section{\METAPOST\ part reference} + +\subsection{Global variables and settings} + +\subsubsection{Global variables}\label{Variables} + + There are several variables which control the looks and behaviour. + + \texttt{lineWidth}\label{lineWidth}~— the width for regular lines, set to \texttt{2pt} by default. Normally referenced as \texttt{0}. + + \texttt{lineWidthThin}~— the width for thin lines, set to \texttt{1pt} by default. Normally referenced as \texttt{1}. + + \texttt{lineWidthHair}~— the width for superthin lines, set to \texttt{1/2pt} by default. Normally referenced as \texttt{2}. + + \texttt{pointMarkSize}\label{pointMarkSize}~— size of point marks, set to \texttt{4pt} by default. + + \texttt{pointLinesSize}\label{pointLinesSize}~— size for line segments used to depict points, set to \texttt{1/2cm} by default. + + \texttt{defaultScaleFactor}~— global default scale factor, set to \texttt{1} by default. + + \texttt{angleSize}\label{angleSize}~— angle arc radius, set to \texttt{1cm} by default. + + \texttt{angleScale}\label{angleScale}~— scale factor for angle arcs, set to \texttt{1} by default. + + \texttt{globalRotation}~— angle to rotate the whole picture. + + \texttt{markLength}~— size of marks on lines, set to \texttt{3lineWidth} by default. + + \texttt{rayExtension}~— length for a ray depiction, set to \texttt{1/3cm} by default. + +% magnitudeSize := 5/18cm; +% magnitudeScale := 1; +% magnitudeGap := 3/2lineWidth; + + \texttt{textLabels}~— whether to render text labels, set to \texttt{false} by default. + +%ghostLines := true; + + \texttt{autoRightAngles}\label{autoRightAngles}~— whether to depict right angles using angles instead of arcs, set to \texttt{false} by default. + +%omitDuplicateTextLabels := false; + + \texttt{textLabelShift}~— how far away to put text labels from points, set to \texttt{lineWidth} by default. + +%autoLabelingMode := false; +%mainPictureMode := false; + +%pathToString + +\subsubsection{defineColor.ColorName(color)}\label{defineColor} + + \defineNewPicture{ + textLabels := false; + pair A, B, C; + A := dir(60); + B := (0, 0); + C := dir(0); + forsuffixes s=byblack,byred,byblue,byyellow,oiBlack,oiOrange,oiSkyBlue,oiGreen,oiYellow,oiBlue,oiVermillion,oiPurple: + byAngleDefine.s(A, B, C,s, 0); + endfor; + } + Defines a color. Colors can be defined as regular \METAPOST\ colors as well, this is just a shorthand version. + + \texttt{ColorName}~— mandatory color name of \texttt{suffix type}. + + \texttt{color}~— an actual calor, can be either of \texttt{color} type or of \texttt{cmykcolor} type. + + Some colors are predefined. Colors similar to the ones Byrne used for his book are: \texttt{byblack}\drawAngle{byblack}, \texttt{byred}\drawAngle{byred}, \texttt{byblue}\drawAngle{byblue}, \texttt{byyellow}\drawAngle{byyellow}. One obvious issue of any color coding is whether the colors are discernible by colorblind people. Byrne's colors should be mostly fine, but there are only foru of them. In case more colors are necessary, eight colors from \href{https://jfly.uni-koeln.de/color/}{colorblind-friendly palette by Okabe and Ito} \cite{Okabe2008} are defined: \texttt{oiBlack}\drawAngle{oiBlack}, \texttt{oiOrange}\drawAngle{oiOrange}, \texttt{oiSkyBlue}\drawAngle{oiSkyBlue}, \texttt{oiGreen}\drawAngle{oiGreen}, \texttt{oiYellow}\drawAngle{oiYellow}, \texttt{oiBlue}\drawAngle{oiBlue}, \texttt{oiVermillion}\drawAngle{oiVermillion}, \texttt{oiPurple}\drawAngle{oiPurple}. Also text labels \ref{textlabels} are helpful as a secondary means of identification. + +%\subsubsection{selectOutlineColor.ColorName}\label{selectOutlineColor} + +% Selects an outline color for a color. + +% \texttt{ColorName}~— mandatory color name of \texttt{suffix type}. + +% ... + +\subsubsection{startTempScale(tmpScale); ... \METAPOST\ code ... stopTempScale;}\label{startTempScale} + + Used to temporarily set scale factor for a section of code; + + \texttt{tmpScale}~— scale factor for a section of code. + +\subsubsection{startTempAngleScale(tmpAngleScale); ... \METAPOST\ code ... stopTempAngleScale;}\label{startTempAngleScale} + + Used to temporarily set angle scale factor for a section of code; + + \texttt{tmpAngleScale}~— angle scale factor for a section of code. + +\subsubsection{startGlobalRotation(rotationAngle); ... \METAPOST\ code ... stopGlobalRotation;}\label{startGlobalRotation} + + Used to temporarily set global rotation for a section of code; + + \texttt{rotationAngle}~— global rotation for a section of code. + +\subsubsection{startAutoLabeling; ... \METAPOST\ code ... stopAutoLabeling}\label{startAutoLabeling} + + Used to turn auto labeling for a section of code. Most of the drawing functions have auto labeling which is turned off by default. Sometimes it's handy to use it instead of labeling items by hand. + +%startOffspringPictureMode +%stopOffspringPictureMode + +%startMainPictureMode +%stopMainPictureMode + +\subsection{Lines} + +% byLineRender +% byReturnLineLength + +\subsubsection{byLineDefine.LineName(A, B, color, dashed, thick)}\label{byLineDefine} + + Defines a straight line. Does not return anything. + + \texttt{LineName}~— optional line name of \texttt{suffix type}. If no name is provided, it is being constructed from point names, e.g. \texttt{AB}. + + \texttt{A, B}~— point names, of \texttt{suffix} type (i.e., variable names should be provided). + + \texttt{color}~— line color, of \texttt{color} type. + + \texttt{dashed}~— is line dashed. \texttt{0} for solid line and \texttt{1} for dashed line. + + \texttt{thick}~— is line thick. \texttt{0} for regular line and \texttt{1} for thin line \ref{lineWidth}. + + \defineNewPicture{ + pair A, B, C, D; + A := (0, 0); + B := (2cm, 0); + C := (3cm, 0); + D := (5cm, 0); + byLineDefine(A, B, byred, 0, 0); + byLineDefine(C, D, byblue, 1, 1); + } + For example, this line \drawUnitLine{AB} is defined as \texttt{byLineDefine(A, B, byred, 0, 0);} and this line \drawUnitLine{CD} is defined as \texttt{byLineDefine(C, D, byblue, 1, 1);} + +% byLineStylize + +\subsubsection{byLine.LineName(A, B, color, dashed, thick)}\label{byLine} + + Defines and draws a straight line. Returns a \texttt{picture} of the line. + + Arguments are the same as in \texttt{byLineDefine} \ref{byLineDefine}. + +% byLineFull +% byLineWithName +% byNamedLineFull + +\subsubsection{byNamedLine(LINES\_LIST)}\label{byNamedLine} + + Draws lines. Returns a picture of the lines. + + \texttt{LINES\_LIST}~— a comma-separated list of line names. + +\subsubsection{byNamedLineSeq(lineShift)(LINES\_LIST)}\label{byNamedLineSeq} + + \defineNewPicture[1]{ + pair A, B, C; + A := dir(45) scaled 1/4cm; + B := (0, 0); + C := dir(0) scaled 1/4cm; + byLineDefine(A, B, byred, 0, 0); + byLineDefine(B,C, byyellow, 0, 0); + draw byNamedLineSeq(0)(BC,AB) scaled 2; + } + + Draws lines joined in sequence. Line joints look like this:\drawCurrentPicture. Returns a picture of the lines. + + \texttt{lineShift}~— how much the lines are shifted perpendicular lines' axis. \texttt{1} is for one regular line thickness to one side% + \drawFromCurrentPicture[bottom]{ + draw byNamedLineSeq(1)(BC,AB) scaled 2; + draw (A--B--C) scaled 2 withpen pencircle scaled 1/2pt; + }, + \texttt{-1} is for one regular line thickness to the other side% + \drawFromCurrentPicture[bottom]{ + draw byNamedLineSeq(-1)(BC,AB) scaled 2; + draw (A--B--C) scaled 2 withpen pencircle scaled 1/2pt; + }. + + \texttt{LINES\_LIST}~— a comma-separated list of line names. + + +\subsubsection{byMarkLine(position, color)(LineName)}\label{byMarkLine} + + \defineNewPicture[1]{ + pair A, B; + A := (0, 0); + B := (1cm, 0); + byLineDefine(A, B, byblue, 0, 0); + } + + Defines and draws a mark on a line. + + \texttt{position}~— a position of the mark. \texttt{0} for one end of the line, \texttt{1} for the other end of the line. + + \texttt{color}~— mark color, of \texttt{color} type. + + \texttt{LineName}~— line name, of \texttt{suffix} type. + + For example this line mark \drawFromCurrentPicture{ + startAutoLabeling; + draw byNamedLine(AB); + draw byLabelLine(0)(AB); + draw byMarkLine(1/4, byred, AB); + stopAutoLabeling; + } is drawn with \texttt{draw byMarkLine(1/4, byred, AB);}. + +\subsubsection{byNamedMarkLine(LineName)} + + Draws a defined mark on the given line. + + \texttt{LineName}~— line name, of \texttt{suffix} type. + +%byFindLinePointedThere +%byConstructCompoundLine +%byExpandLines +%byNamedCompoundLine +%byNamedCompoundRay +%byNamedCompoundIndLine +%byNamedCompoundLineRender +%generateLineSynonyms + +\subsection{Points} + +\subsubsection{byPointLabelDefine(A, pointLabel)}\label{byPointLabelDefine} + + Defines a point text label, returns nothing. By default point names are the same as the names of respective variables. + + \texttt{A}~— a name of a point of \texttt{suffix} type (i.e., variable name should be provided). + + \texttt{pointLabel}~— point label of \texttt{string} type. + +\subsubsection{byPointLabelRemove(POINTS\_LIST)}\label{byPointLabelRemove} + + Sets text labels of multiple points to blank. Useful if text labels are turned on, but you don't want labels on certain points. + + \texttt{POINTS\_LIST}~— a comma-separated list of point names. + +\subsubsection{byPointMarkDefine(PointName)(color, style)}\label{byPointMarkDefine} + + \defineNewPicture{ + textLabels := false; + pair A, B; + A := (0, 0); + B := (1cm, 0); + draw byPointMark(A, byblue, 0); + draw byPointMark(B, byred, 1); + } + + Defines a point mark. + + \texttt{color}~— mark color, of \texttt{color} type. + + \texttt{style}~— \texttt{0} for solid circle \drawPointM{A}, \texttt{1} for white circle with outline \drawPointM{B}. Mark size is controlled by \texttt{pointMarkSize} \ref{pointMarkSize}. + +%byPointXYZDefine +%byPointXYZEmpty +%byIsPointInSpace +%byPair +%byRotatePoints +%byPointMarkRender +%byNamedPointMark +%byPointMark +%byNamedPointLines + +\subsection{Arcs and circles} + +%byCirclePathGenerate +%byArcRender + +\subsubsection{byArcDefineBE.ArcName(O, begin, end, radius, color, dashed, thick, shift, endType)}\label{byArcDefineBE} + + Defines an arc based on its center, radius and two angles. Returns nothing. + + \defineNewPicture[1]{ + textLabels := false; + pair A, B; + A := (0, 0); + } + + \texttt{ArcName}~— optional arc name of \texttt{suffix type}. If no name is provided, it is being constructed from center point name, e.g. \texttt{O}. + + \texttt{O}~— the center of the circle an arc is from, of \texttt{suffix} type (i.e., variable name should be provided). + + \texttt{begin, end}~— beginning and ending of the arc in eighths (in essence, \texttt{arctime} of \texttt{fullcircle}). \texttt{0} for 3 o'clock, \texttt{2} for 12 o'clock etc. For instance, this + \drawFromCurrentPicture{ + draw byArcBE(A, 0, 3, 2/6cm, byblue, 0, 0, 1, 0); + } + is \texttt{0, 3} and this + \drawFromCurrentPicture{ + draw byArcBE(A, 5, 7, 2/6cm, byblue, 0, 0, 1, 0); + } + is \texttt{5, 7}. + + \texttt{radius}~— arc radius. + + \texttt{color}~— arc color, of \texttt{color} type. + + \texttt{dashed}~— is arc line dashed. \texttt{0} for solid line and \texttt{1} for dashed line. + + \texttt{thick}~— is arc line thick. \texttt{0} for regular line and \texttt{1} for thin line \ref{lineWidth}. + + \texttt{shift}~— how much arc line is shifted. \texttt{1} is for one regular line thickness outwards + \drawFromCurrentPicture{ + draw byArcBE(A, 1/2, 7/2, 1/6cm, byyellow, 0, 0, 1, 0) scaled 2; + draw (subpath (1/2, 7/2) of (fullcircle scaled 4/6cm)) withpen pencircle scaled 1/2pt; + }, \texttt{-1} is for one regular line thickness inwards + \drawFromCurrentPicture{ + draw byArcBE(A, 1/2, 7/2, 1/6cm, byyellow, 0, 0, -1, 0) scaled 2; + draw (subpath (1/2, 7/2) of (fullcircle scaled 4/6cm)) withpen pencircle scaled 1/2pt; + }. + + \texttt{endType}~— type of arc line ends. \texttt{0} for ends cut by the radii + \drawFromCurrentPicture{ + draw byArcBE(A, 1/2, 7/2, 1/6cm, byred, 0, 0, 0, 0) scaled 2; + } + and \texttt{1} for ends cut by the chord + \drawFromCurrentPicture{ + draw byArcBE(A, 1/2, 7/2, 1/6cm, byred, 0, 0, 0, 1) scaled 2; + }. + +\subsubsection{byArcBE.arcName(O, begin, end, radius, color, dashed, thick, shift, endType)}\label{byArcBE} + + Defines and draws an arc based on its center, radius and two angles. Returns the picture of the arc. + + Arguments are the same as in \texttt{byArcDefineBE} \ref{byArcDefineBE}. + +\subsubsection{byArcDefine.ArcName(O, B, E, radius, color, dashed, thick, shift, endType)}\label{byArcDefine} + + Defines an arc based on its center, radius and two points. Returns nothing. + + Arguments are the same as in \texttt{byArcDefineBE} \ref{byArcDefineBE}, except for instead of angles in eights points are used. + + \texttt{ArcName}~— optional arc name of \texttt{suffix type}. If no name is provided, it is being constructed from point names, e.g. \texttt{BOE}. + + \texttt{B, E}~— points in the direction of the beginning and the ending of the arc respectively. Of \texttt{suffix} type (i.e., variable names should be provided). + +\subsubsection{byArc.ArcName(O, B, E, radius, color, dashed, thick, shift, endType))}\label{byArc} + + Arguments are the same as in \texttt{byArcDefine} \ref{byArcDefine}. + +\subsubsection{byNamedArcExact(ARCS\_LIST)}\label{byNamedArcExact} + Draws a picture of arcs just as they were defined. Returns a picture of the arcs. + + \texttt{ARCS\_LIST}~— a comma-separated list of arc names. + +\subsubsection{byNamedArc(ARCS\_LIST)}\label{byNamedArc} + Draws a picture of arcs, except of it ignores \texttt{shift} and \texttt{endType}. Returns a picture of the arcs. + + \texttt{ARCS\_LIST}~— a comma-separated list of arc names. + +%byNamedArcLabel + +\subsubsection{byCircleDefineFree.CircleName(o, radius, color, dashed, thick, shift)}\label{byCircleDefineFree} + + Defines a circle based on its center and radius. Returns nothing. + + \defineNewPicture[1]{ + } + + \texttt{CircleName}~— optional circle name of \texttt{suffix type}. If no name is provided, \texttt{aCircle} is used for a name. + + \texttt{o}~— the center of the circle, of \texttt{pair} type. + + \texttt{radius}~— circle radius. + + \texttt{color}~— circle color, of \texttt{color} type. + + \texttt{dashed}~— is circle line dashed. \texttt{0} for solid line and \texttt{1} for dashed line. + + \texttt{thick}~— is circle line thick. \texttt{0} for regular line and \texttt{1} for thin line \ref{lineWidth}. + + \texttt{shift}~— how much circle line is shifted. \texttt{1} is for one regular line thickness outwards + \drawFromCurrentPicture{ + byCircleDefineFree((0,0), 1/8cm, byyellow, 0, 0, 1); + draw byNamedCircle(aCircle) scaled 2; + draw fullcircle scaled 1/2cm; + }, \texttt{-1} is for one regular line thickness inwards + \drawFromCurrentPicture{ + byCircleDefineFree((0,0), 1/8cm, byyellow, 0, 0, -1); + draw byNamedCircle(aCircle) scaled 2; + draw fullcircle scaled 1/2cm; + }. + +\subsubsection{byCircleDefineR.CircleName(O, radius, color, dashed, thick, shift)}\label{byCircleDefineR} + + Defines a circle based on its center and radius. Returns nothing. + + Arguments are the same as in \texttt{byCircleDefineFree} \ref{byCircleDefineFree}, except for the center is not defined directly by a \texttt{pair}. + + \texttt{CircleName}~— optional circle name of \texttt{suffix type}. If no name is provided, it is being constructed from center name, e.g. \texttt{O}. + + \texttt{O}~— the center of the circle. Of \texttt{suffix} type (i.e., variable name should be provided). + +\subsubsection{byCircleR.CircleName(O, radius, color, dashed, thick, shift)}\label{byCircleR} + + Defines and draws circle based on its center and radius. Returns a picture of that circle. + + Arguments are the same as in \texttt{byCircleDefineR} \ref{byCircleDefineR}. + +\subsubsection{byCircleDefine.CircleName(O, A, color, dashed, thick, shift)}\label{byCircleDefine} + + Defines circle based on its center and a point. Returns nothing. + + Arguments are the same as in \texttt{byCircleDefineR} \ref{byCircleDefineR}, except for instead of the radius, a point through which the circle is drawn is used. + + \texttt{CircleName}~— optional circle name of \texttt{suffix type}. If no name is provided, it is being constructed from point names, e.g. \texttt{OA}. + + \texttt{A}~— a point through which the circle is drawn. Of \texttt{suffix} type (i.e., variable name should be provided). + +\subsubsection{byCircle.CircleName(O, A, color, dashed, thick, shift)}\label{byCircle} + + Defines and draws circle based on its center and a point. Returns a picture of that circle. + + Arguments are the same as in \texttt{byCircleDefine} \ref{byCircleDefine}. + +\subsubsection{byCircleABC.CircleName(A, B, C, color, dashed, thick, shift)}\label{byCircleABC} + + Arguments are the same as in \texttt{byCircleDefineR} \ref{byCircleDefineR}, except for instead of the center and the radius, three points through which the circle is drawn are used. + + \texttt{CircleName}~— optional circle name of \texttt{suffix type}. If no name is provided, it is being constructed from point names, e.g. \texttt{ABC}. + + \texttt{A, B, C}~— points through which the circle is drawn. Of \texttt{suffix} type (i.e., variable names should be provided). + +%byFindCircleCenterABC +%byFindCircleCenterABCinSpace +%byReturnCircleCenter + +\subsubsection{byNamedCircle(CIRCLES\_LIST)}\label{byNamedCircle} + + Draws circles. Returns a picture of the circles. + + \texttt{CIRCLES\_LIST}~— a comma-separated list of circle names. + +\subsection{Arbitraty figures} + +\subsubsection{byArbitraryFigureDefine.ArbitraryFigureName(figurePath, col\-or, dashed, thick)}\label{byArbitraryFigureDefine} + + Defines an arbitrary figure (basically, any path). Returns nothing. + + \texttt{ArbitraryFigureName}~— optional figure name of \texttt{suffix type}. If no name is provided, \texttt{anArbitraryFigure} is used for a name. + + \texttt{figurePath}~— figure path, of \texttt{path} type. + + \texttt{color}~— figure color, of \texttt{color} type. + + \texttt{dashed}~— is figure line dashed. \texttt{0} for solid line and \texttt{1} for dashed line. + + \texttt{thick}~— is figure line thick. \texttt{0} for regular line and \texttt{1} for thin line \ref{lineWidth}. + +\subsubsection{byArbitraryFigure.ArbitraryFigureName(figurePath, color, dash\-ed, thick)}\label{byArbitraryFigure} + + Defines an arbitrary figure. Returns nothing. + + Arguments are the same as in \texttt{byArbitraryFigure} \ref{byArbitraryFigure}. + +\subsubsection{byNamedArbitraryFigure(ARBITRARY\_FIGURES\_LIST)}\label{byNamedArbitraryFigure} + + Draws arbitrary figures. Returns a picture of the figures. + + \texttt{ARBITRARY\_FIGURES\_LIST}~— a comma-separated list of figure names. + +%byArbitraryFigureRender +%byArbitraryCurveDefine +%byNamedArbitraryCurve +%byListToPath +%byArbitraryCurve + +\subsection{Filled figures} + +%byFilledCircleSegment +%byNamedFilledCircleSegment +%byFilledCircleSector +%byNamedFilledCircleSector + +\subsubsection{byPolygonDefine.PolygonName(POINTS\_LIST)(Color)}\label{byPolygonDefine} + + Defines a polygon based on its vertex points. Returns nothing. + + \texttt{PolygonName}~— optional polygon name \texttt{suffix}. + + \texttt{POINTS\_LIST}~— a comma-separated list of point names. + + \texttt{Color}~— line color, of \texttt{suffix} type (i.e. a variable name should be provided). + +%byPolygonPathFromList + +\subsubsection{byPolygon.polygonName(POINTS\_LIST)(Color)}\label{byPolygon} + + Defines and draws a polygon based on its vertex points. Returns a picture of the polygon. + + Arguments are the same as in \texttt{byPolygonDefine} \ref{byPolygonDefine}. + +%byPolygonName + +\subsubsection{byNamedPolygon(POLYGONS\_LIST)}\label{byNamedPolygon} + + Draws polygons. Returns a picture of the polygons. + + \texttt{POLYGONS\_LIST}~— a comma-separated list of polygon names. + +%byMergePolygons +%polygonCleanup + +\subsection{Angles} + +\subsubsection{byAngleDefine.AngleName(A, B, C, color, style)}\label{byAngleDefine} + + \defineNewPicture{ + textLabels := false; + pair A, B, C, A', B', C', A'', B'', C''; + A := dir(60); + B := (0, 0); + C := dir(0); + A' := A shifted (1cm, 0); + B' := B shifted (1cm, 0); + C' := C shifted (1cm, 0); + A'' := A shifted (2cm, 0); + B'' := B shifted (2cm, 0); + C'' := C shifted (2cm, 0); + byAngleDefine(A, B, C, byred, 0); + byAngleDefine(A', B', C', byred, 1); + byAngleDefine(A'', B'', C'', byred, 2); + } + + Defines an angle based on three points. Returns nothing. + + \texttt{AngleName}~— optional angle name \texttt{suffix}. + + \texttt{A, B, C}~— angle points. Of \texttt{suffix} type (i.e., variable names should be provided). + + \texttt{color}~— angle color, of \texttt{color} type. + + \texttt{style}~— angle arc style. \texttt{0} for solid filled sector \drawAngle{B}, \texttt{1} for arc \drawAngle{B'}, \texttt{2} for dashed arc \drawAngle{B''}. Default arc radius is determined by \texttt{angleSize} \ref{angleSize}. It can be temporarily changed using \texttt{angleScale} \ref{angleScale}. You can also define custom angle styles \ref{AngleStyles}. + +\subsubsection{byConsiderAngleRight(AngleName)}\label{byConsiderAngleRight} + + \defineNewPicture{ + textLabels := false; + pair A, B, C, A', B', C'; + A := dir(60)*cm; + B := (0, 0); + C := dir(0)*cm; + A' := A shifted (2cm, 0); + B' := B shifted (2cm, 0); + C' := C shifted (2cm, 0); + byAngleDefine(A, B, C, byyellow, 0); + byAngleDefine(A', B', C', byyellow, 0); + byConsiderAngleRight(A'B'C'); + } + Makes a defined angle look like an angle \drawAngle{A'B'C'} instead of a circle arc \drawAngle{ABC}, as right angles are often depicted. Useful if an angle is not actually right in the diagram, but is meant to be right or assumed to be right. To make all the right angles look like this automatically, you can set \texttt{autoRightAngles := true;}\ref{autoRightAngles}. + + \texttt{AngleName}~— name of the angle. + +%byConstructAngleArc +%byIsArcClockwise +%byFullCircleToPlane + +\subsubsection{byAngle.AngleName(A, B, C, color, style)}\label{byAngle} + + Defines and draws an angle based on three points. Returns a picture of the angle. + + Arguments are the same as in \texttt{byAngleDefine} \ref{byAngleDefine}. + +\subsubsection{byAngleDefineExtended.AngleName(A, B, C, color, style)(OPTIONAL\_COLORS\_LIST)}\label{byAngleDefineExtended} + + Defines an angle based on three points. Returns nothing. + + Arguments are the same as in \texttt{byAngleDefine} \ref{byAngleDefine}, except for using this function it's possible to define optional colors for an angle. + + \texttt{OPTIONAL\_COLORS\_LIST}~— a comma-separated list of optional color names. See \ref{AngleStyles} for details. + +\subsubsection{byAngleExtended.AngleName(A, B, C, color, style)(OPTION\-AL\_COLORS\_LIST)}\label{byAngleExtended} + + Defines and draws an angle based on three points. Returns a picture of the angle. + + Arguments are the same as in \texttt{byAngleDefineExtended} \ref{byAngleDefineExtended}. + +\subsubsection{byNamedAngle(ANGLES\_LIST)}\label{byNamedAngle} + + Draws angles. Returns a picture of the angles. + + \texttt{ANGLES\_LIST}~— a comma-separated list of polygon names. + +%byNamedSolidAngle +%byProcessAnglesList +%byAngleMWhite +%byAngleMSolid +%byAngleMLine +%byAngleMThinLine +%byAngleMDashedLine +%byNamedAngleSidesFull +%byNamedAngleDummySides +%byNamedAngleSides +%byNamedAngleWithDummySides +%byNamedAngleResized +%generateAngleSynonyms +%sortPointAnglesList +%bySplitStringIntoAngles +%byConstructCompoundAngle +%twoRightAngles +%rightAngle +%byDefineGenericRightAngles + +\subsubsection{Angle styles}\label{AngleStyles} + + It's possible to add custom angle styles. To do this, you first need to define a function, with a particular set of arguments and then include this function's name into a special list. As an example here's the function which describes a regular solid angle sector: + +\begin{lstlisting} +vardef byAngleMSolid (expr angleArc, angleColor)(suffix AngleOptionalColors) = + save p; + path p; + p := angleArc scaled (angleScale*angleSize); + image( + fill ((0, 0)--p--cycle) withcolor angleColor; + ) +enddef; +\end{lstlisting} + + Note the argument \texttt{AngleOptionalColors}. It is not used for solid angles and is not defined with \texttt{byAngleDefine}, but several optional colors can be defined using \texttt{byAngleDefineExtended} \ref{byAngleDefineExtended} and can be accessed using \texttt{AngleOptionalColors[0]}, \texttt{AngleOptionalColors[1]} etc. + + And here's how this angle style is included in the list: + +\begin{lstlisting} +byAngleMacroName[0] := "byAngleMSolid"; +\end{lstlisting} + + Here \texttt{0} is the value of \texttt{sty} argument used in \texttt{byAngleDefine} \ref{byAngleDefine} and such to call this particular angle style. + + As a more practical example, in Byrne's book, in a couple of places for some reason he chooses not to use ordinary arcs and sectors, but instead goes with unique angle designs. In order not to include these peculiarities into the main lib in the \ConTeXt\ version they were defined \textit{in situ}. Here's the definition from IV.III: + + \defineNewPicture{ + vardef byAngleMSectors (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p; + path p[]; + p1 := (subpath(0, arctime (1/3arclength(angleArc)) of angleArc) + of angleArc) + scaled (angleScale*angleSize); + p2 := (subpath(arctime (1/3arclength(angleArc)) + of angleArc, arctime(2/3arclength(angleArc)) of angleArc) of angleArc) + scaled (angleScale*angleSize); + p3 := (subpath(arctime (2/3arclength(angleArc)) + of angleArc, length(angleArc)) of angleArc) + scaled (angleScale*angleSize); + image( + fill ((0, 0)--p1--cycle) withcolor angleOptionalColors[0]; + fill ((0, 0)--p2--cycle) withcolor angleColor; + fill ((0, 0)--p3--cycle) withcolor angleOptionalColors[1]; + ) + enddef; + byAngleMacroName[3] := "byAngleMSectors"; + textLabels := false; + pair A, B, C; + A := dir(45) scaled 1/4cm; + B := (0, 0); + C := dir(0) scaled 1/4cm; + byAngleDefineExtended(A, B, C, byred, 3)(byblue, byyellow); + } + +\begin{lstlisting} +vardef byAngleMSectors (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p; + path p[]; + p1 := (subpath(0, arctime (1/3arclength(angleArc)) of angleArc) + of angleArc) + scaled (angleScale*angleSize); + p2 := (subpath(arctime (1/3arclength(angleArc)) + of angleArc, arctime(2/3arclength(angleArc)) of angleArc) of angleArc) + scaled (angleScale*angleSize); + p3 := (subpath(arctime (2/3arclength(angleArc)) + of angleArc, length(angleArc)) of angleArc) + scaled (angleScale*angleSize); + image( + fill ((0, 0)--p1--cycle) withcolor angleOptionalColors[0]; + fill ((0, 0)--p2--cycle) withcolor angleColor; + fill ((0, 0)--p3--cycle) withcolor angleOptionalColors[1]; + ) +enddef; +byAngleMacroName[3] := "byAngleMSectors"; +\end{lstlisting} + +This angle style employs two optional colors and can be called by setting \texttt{style} argument \ref{byAngleDefine} to \texttt{3}. This is what the result of \texttt{byAngleDefineExtended(A, B, C, byred, 3)(byblue, byyellow);} (\ref{byAngleDefineExtended}) looks like:\drawAngle{B}. +%https://archive.org/details/firstsixbooksofe00byrn/page/126/mode/2up + +%\subsection{Arrows} + +%commonArrowSettings +%byRotationArrowDefine +%byGenerateRotationArrowPath +%byNamedRotationArrow +%byFlatArrowDefine +%byNamedFlatArrow + +%\subsection{Spheres} + +%\subsection{Magnitudes} + +%byMagnitudeSymbolDefine +%byNamedMagnitudeSymbol +%byMagnitudeDefine +%byNamedMagnitude + +\subsection{Text labels}\label{textlabels} + +There are no text labels in Byrne's book, but, as Edward Tufte points out in his 1990 book, adding small text labels can actually help speed recognition of geometric elements \cite{Tufte90}. It's not clear whether it's indeed the case for the reader, but at the very least text labels provide the author with the means of linking source code with the rendered pages. Several functions listed below allow for adding text labels very similar to the ones suggested by Tufte. + +Text labels are turned off by default. As a result, simply adding code for text labels won't make them visible. They only appear when \texttt{textLabels := true;} is set in the beginning of a main picture or globally by defining \texttt{\textbackslash def\textbackslash\{...\} mpPre} \ref{defineNewPicture}. Text labels don't always have to be added manually, most elements can do auto labeling \ref{startAutoLabeling} which is intended to work with standalone angles, polygons, etc. + +%\subsubsection{byTextLabel(labelType, text, point, angle, distance)}\label{byTextLabel} +%Draws a text label. Returns a picture of the label. + +\subsubsection{byLabelPoint(A, labelAngle, distance)}\label{byLabelPoint} +Draws a text label for a point. Returns a picture of the label. + + \texttt{A}~— the point to label, of \texttt{suffix} type (i.e., point name should be provided). + + \texttt{labelAngle}~— angle at which the label should be drawn in degrees. + + \texttt{distance}~— distance at which the label should be drawn in \texttt{textLabelShift} units. + +\subsubsection{byLabelLine(distance)(LINES\_LIST)}\label{byLabelLine} + + Draws text labels for lines. Returns a picture of the labels. + + \texttt{distance}~— distance at which the labels should be drawn in \texttt{textLabelShift} units. + + \texttt{LINES\_LIST}~— a comma-separated list of line names. + +\subsubsection{byLabelPolygon(distance)(POLYGONS\_LIST)}\label{byLabelPolygon} + + Draws text labels for polygons. Returns a picture of the labels. + + \texttt{distance}~— distance at which the labels should be drawn in \texttt{textLabelShift} units. + + \texttt{POLYGONS\_LIST}~— a comma-separated list of polygon names. + +%byLabelCircle + +\subsubsection{byLabelsOnCircle(POINTS\_LIST)(CircleName)}\label{byLabelsOnCircle} + + Draws text labels for points on a circle. Returns a picture of the labels. + + \texttt{POINTS\_LIST}~— a comma-separated list of point names. + + \texttt{CircleName}~— circle name of \texttt{suffix type}. + +\subsubsection{byLabelsOnPolygon(POINTS\_LIST)(style, shift)}\label{byLabelsOnPolygon} + + Draws text labels for points on a polygon. Returns a picture of the labels. + + \defineNewPicture{ + pair A, B, C, D; + D := (0, 0); + C := (1cm, 0); + B := (2cm, 0); + A := (1/2cm, 1cm); + byPolygonDefine(D, C, A)(byred); + byPolygonDefine(C, B, A)(byyellow); + } + + \texttt{POINTS\_LIST}~— a comma-separated list of point names. + + \texttt{style}~— specifies which points to omit. \texttt{0} to draw labels for all listed points + \drawFromCurrentPicture{ + draw byNamedPolygon(DCA,CBA); + draw byLabelsOnPolygon(A, B, C, D)(0, 0); + }, \texttt{1} to draw labels for all listed points, except for the ones at straight angles + \drawFromCurrentPicture{ + draw byNamedPolygon(DCA,CBA); + draw byLabelsOnPolygon(A, B, C, D)(1, 0); + }, \texttt{2} omits first and last points + \drawFromCurrentPicture{ + draw byNamedPolygon(DCA,CBA); + draw byLabelsOnPolygon(A, B, C, D)(2, 0); + }, \texttt{3} omits the first point + \drawFromCurrentPicture{ + draw byNamedPolygon(DCA,CBA); + draw byLabelsOnPolygon(A, B, C, D)(3, 0); + }, \texttt{4} omits the last point + \drawFromCurrentPicture{ + draw byNamedPolygon(DCA,CBA); + draw byLabelsOnPolygon(A, B, C, D)(4, 0); + }. + + \texttt{shift}~— negative to move labels closer, positive to move labels farther. + +\subsubsection{byLabelLineEnd(A, B)(distance)}\label{byLabelLineEnd} + + Draws a text label for a line end. Returns a picture of the label. + + \texttt{A, B}~— line ends, of \texttt{suffix} type (i.e., point name should be provided). + + \texttt{distance}~— negative to move labels closer, positive to move labels farther. + +%byLabelAngleCompensate + +\section{\LaTeX\ part reference} + +\subsection{General purpose} + +\sloppy + +\subsubsection{\textbackslash defineNewPicture [offspringPicture\-Sca\-leFactor] [mainPicture\-Sca\-le\-Factor] \{\METAPOST\ code\}}\label{defineNewPicture} + +\fussy + + This macro is used to describe a main diagram. + + \texttt{offspringPictureScaleFactor}~— how much smaller offspring pictures should be. \texttt{1/3} by default. + + \texttt{mainPictureScaleFactor}~— scale factor for the main picture. \texttt{1} by default. + + \texttt{\METAPOST\ code}~— actual \METAPOST\ code to describe the main picture. To have some snippets of code always executed before and after the code for the main picture (e.g. to turn on text labels by default with \texttt{textLabels := true;}), define \cs{mpPre} and \cs{mpPost} macros respectively in the preamble of your document, like this: + +\begin{lstlisting} +\def\mpPre{...some \METAPOST\ code...} +\def\mpPost{...some \METAPOST\ code...} +\end{lstlisting} + + +\subsubsection{\textbackslash drawCurrentPicture} + + Draws the main diagram. + +% \drawImageFromCurrentInstance + +% \unmarkPictAsReady + +% \addToUndefineList + +\sloppy + +\subsubsection{\textbackslash defineFromCurrentPicture \{verticalAlignment\} \{PictureName\} \{\METAPOST\ code\}} + +\fussy + + This macro is used to describe a diagram, based on the main diagram. + + \texttt{verticalAlignment}~— vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{PictureName}~— picture name. + + \texttt{MP code}~— actual \METAPOST\ code to describe the picture. + + Can be drawn using \texttt{\textbackslash PictureName}. + +\sloppy + +\subsubsection{\textbackslash drawFromCurrentPicture [verticalAlignment] [PictureName] \{\METAPOST\nolinebreak\ code\}} + + \fussy + + This macro is used to describe and draw a diagram, based on the main diagram. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{PictureName}~— optional picture name. + + \texttt{\METAPOST\ code}~— actual \METAPOST\ code to describe the picture. + + Can be drawn again later using \texttt{\textbackslash PictureName}. + +% \drawDefinedPicture + +\subsection{Specialized} + +\subsubsection{\textbackslash drawUnitLine[LineSecondName]\{LineName\}} + + Draws a line of unit length with colors and labels based on the line of the same name in the main diagram. + + \texttt{LineSecondName}~— optional line name. + + \texttt{LineName}~— name of the line to reference in the main diagram. + + If a name is given, can be called later using \texttt{\textbackslash ulineLineNameLineSecondName}, otherwise~— \texttt{\textbackslash ulineLineName}. + +\subsubsection{\textbackslash drawProportionalLine\{LineName\}} + + Draws a line with colors and labels based on and length linearly proportional to the line of the same name in the main diagram. + + \texttt{LineName}~— name of the line to reference in the main diagram. + + Can be called later using \texttt{\textbackslash plineLineName}. + +%\drawProportionalLine + +\subsubsection{\textbackslash drawSizedLine[LineSecondName]\{LineName\}} + + Draws a line with colors and labels based on and length proportional to the line of the same name in the main diagram. + + \texttt{LineSecondName}~— optional line name. + + \texttt{LineName}~— name of the line to reference in the main diagram. + + If a name is given, can be called later using \texttt{\textbackslash slineLineNameLineSecondName}, otherwise~— \texttt{\textbackslash slineLineName}. + +\subsubsection{\textbackslash drawUnitRay[RayName]\{LineName\}} + + Draws a ray of unit length with colors and labels based on the line of the same name in the main diagram. + + \texttt{RayName}~— optional ray name. + + \texttt{LineName}~— name of the line to reference in the main diagram. + + If a name is given, can be called later using \texttt{\textbackslash urayLineNameRayName}, otherwise~— \texttt{\textbackslash urayLineName}. + +%\subsubsection{\textbackslash drawProportionalRay\{LineName\}} + +% Draws a ray. + +% \texttt{LineName}~— name of the line to reference in the main diagram. + +% Can be called later using \texttt{\textbackslash prayAngleName}. + +%\drawSizedRay + +%\drawUnitIndLine + +%\drawProportionalIndLine + +\subsubsection{\textbackslash drawRightAngle, \textbackslash drawTwoRightAngles} + \defineNewPicture{textLabels := false;} + Draws a right angle \drawRightAngle\ and two right angles \drawTwoRightAngles\ respectively. + + More broadly, following generic right angles are defined: \texttt{rightAngleNE} \drawAngle{rightAngleNE}, \texttt{rightAngleES} \drawAngle{rightAngleES}, \texttt{rightAngleSW} \drawAngle{rightAngleSW}, \texttt{rightAngleWN} \drawAngle{rightAngleWN}. + + If you choose to go with angles instead of arcs for right angles (\ref{autoRightAngles}, \ref{byConsiderAngleRight}), adding \texttt{autoRightAngles := true;} to \texttt{\textbackslash mpPre} \ref{defineNewPicture} won't change the appearance of these angles, since they are defined before \texttt{\textbackslash mpPre} commands are executed. Instead, these angles should be redefined. There's a special command for this purpose: \texttt{byDefineGenericRightAngles(true);} where \texttt{true} tells this command to run \texttt{byConsiderAngleRight} \ref{byConsiderAngleRight} for each of the generic right angles. + +\subsubsection{\textbackslash drawAngle\{AngleName\}}\label{drawAngle} + + Draws an angle. + + \defineNewPicture{ + pair A, B, C, D, E, O; + O := (0, 0); + A := (2cm, 0); + B := A rotated (360/5); + C := B rotated (360/5); + D := C rotated (360/5); + E := D rotated (360/5); + byAngleDefine(A, O, B, byred, 0); + byAngleDefine(B, O, C, byyellow, 0); + byAngleDefine(C, O, D, byblue, 0); + byAngleDefine(D, O, E, byblack, 0); + byAngleDefine(E, O, A, byblack, 1); + byLineDefine(A, O, byblack, 0, 0); + } + + \texttt{AngleName}~— name of the angle to reference in the main diagram. + When just one letter is used, all the angles defined at this point will be drawn, e.g. \drawAngle{O}. When used to reference one single defined angle, the direction doesn't matter. + E.g. \texttt{AOB} and \texttt{BOA} will return the same picture \drawAngle{AOB}. + When used to reference an angle composed of several defined angles, the name sould be given counterclockwise. + E.g. \texttt{\textbackslash drawAngle\{AOD\}} is \drawAngle{AOD} and \texttt{\textbackslash drawAngle\{DOA\}} is \drawAngle{DOA}. + + Can be called later using \texttt{\textbackslash angleAngleName}. + +\subsubsection{\textbackslash drawAngleWithSides \{AngleName\}} + + \defineNewPicture{ + pair A, B, C, D; + A := (1/2cm, 2cm); + B := (0, 0); + C := (3cm, 0); + D := 1/2[A, C]; + byAngleDefine(A, B, D, byyellow, 1); + byAngleDefine(D, B, C, byyellow, 0); + byLineDefine(A, B, byred, 1, 0); + byLineDefine(B, C, byblue, 0, 0); + draw byNamedAngleResized(); + draw byNamedLineSeq(0)(AB, BC); + } + + Draws an angle and sections of its adjacent sides placed if angle portion style is not \texttt{0}. E.g., this angle \drawAngle{B} is drawn with \texttt{\textbackslash drawAngle\{B\}}, and this angle \drawAngleWithSides{B} is drawn with \texttt{\textbackslash drawAngleWithSides\{B\}}. + + \texttt{AngleName}~— the same as in \ref{drawAngle}. + + Can be called later using \texttt{\textbackslash anglewithsidesAngleName}. + +\subsubsection{\textbackslash drawPolygon[verticalAlignment][PolygonNewName] \{Poly\-gon\-Na\-me\}} + + Draws a polygon. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{PolygonNewName}~— optional polygon name. + + \texttt{PolygonName}~— name of the polygon to reference in the main diagram. + + If a name is given, can be called later using \texttt{\textbackslash PolygonNewName}, otherwise~— \texttt{\textbackslash polygonPolygonName}. + +\subsubsection{\textbackslash drawCircle[verticalAlignment][scaleFactor]\{CircleName\}} + + Draws a circle. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{scaleFactor}~— optional scale factor for the circle to replace the default offspring picture scale factor. + + \texttt{CircleName}~— name of the circle to reference in the main diagram. + + Can be called later using \texttt{\textbackslash circleCircleName}. + +\subsubsection{\textbackslash drawArc[verticalAlignment][scaleFactor]\{ArcName\}} + + Draws an arc. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{scaleFactor}~— optional scale factor for the arc to replace the default offspring picture scale factor. + + \texttt{ArcName}~— name of the arc to reference in the main diagram. + + Can be called later using \texttt{\textbackslash arcCircleName}. + +\subsubsection{\textbackslash drawLine[verticalAlignment][LineNewName]\{LineName\}} + + Draws a line. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{LineNewName}~— optional line name. + + \texttt{LineName}~— name of the line to reference in the main diagram. + + If a name is given, can be called later using \texttt{\textbackslash LineNewName}, otherwise~— \texttt{\textbackslash lineLineName}. + +\subsubsection{\textbackslash drawPointM\{PointName\}} + + \defineNewPicture{ + pair A, B, C, D; + A := (0, 0); + B := dir(15)*cm; + C := B rotated 120; + D := C rotated 120; + byLineDefine(A, B, byred, 0, 0); + byLineDefine(A, C, byyellow, 0, 0); + byLineDefine(A, D, byblue, 0, 0); + draw byNamedLine(AD); + draw byNamedLineSeq(0)(AB,AC); + draw byPointMark(A, byblack, 1); + } + + Draws a point, depicted as a small circle \drawPointM{A}. + + \texttt{PointName}~— name of the point to reference in the main diagram. + + Can be called later using \texttt{\textbackslash pointmPointName}. + +\subsubsection{\textbackslash drawPointL[verticalAlignment][LinesToOmit]\{PointName\}}\label{drawPointL} + + Draws a point, depicted as line segments starting/ending in it \drawPointL{A}. Only the lines which were actually drawn, not just defined, are used. Line segments' length is controlled by \texttt{pointLinesSize} \ref{pointLinesSize}. + + \texttt{verticalAlignment}~— optional vertical alignment declaration. \texttt{middle} to align the picture with the middle of the line, anything else for bottom alignment. + + \texttt{LinesToOmit}~— a comma-separated list lines, segments of which not to draw. + + \texttt{PointName}~— name of the point to reference in the main diagram. + + If a list of lines to omit is given, can be called later using \texttt{\textbackslash pointl\-Point\-Name\-Minus\-Lines\-To\-Omit}, otherwise~— \texttt{\textbackslash pointlPointName}. + +\subsubsection{\textbackslash drawPoint[verticalAlignment][LinesToOmit]\{PointName\}} + + Draws a point, depicted as line segments starting/ending in it with a point mark if defined \drawPoint{A}. Only the lines which were actually drawn, not just defined, are used. + + Arguments are the same as in \texttt{drawPointL} \ref{drawPointL}. + + If a list of lines to omit is given, can be called later using \texttt{\textbackslash point\-Point\-Name\-Minus\-Lines\-To\-Omit}, otherwise~— \texttt{\textbackslash pointPointName}. + +%\drawMagnitude + +\bibliographystyle{plain} +\bibliography{byrne-latex} + +\end{document}
\ No newline at end of file diff --git a/Master/texmf-dist/metapost/byrne/byrne.mp b/Master/texmf-dist/metapost/byrne/byrne.mp new file mode 100644 index 00000000000..115998e257a --- /dev/null +++ b/Master/texmf-dist/metapost/byrne/byrne.mp @@ -0,0 +1,3852 @@ +% byrne-euclid 0.2.2 +% MetaPost library that implements most of the graphical +% features present in Oliver Byrne's version of Euclid's "Elements" +% Copyright 2022 Sergey Slyusarev +% +% This program is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program. If not, see <http://www.gnu.org/licenses/>. + +% +% Here we define some things of general interest +% + +pi := 3.1415926; +radian := 180/pi; +lineWidth := 2pt; +lineWidthThin := 1pt; +lineWidthHair := 1/2pt; +pointMarkSize := 4pt; +pointLinesSize := 1/2cm; +defaultScaleFactor := 1; +angleSize := 1cm; +angleScale := 1; +globalRotation := 0; +magnitudeSize := 5/18cm; +magnitudeScale := 1; +magnitudeGap := 3/2lineWidth; +markLength := 3lineWidth; +rayExtension := 1/3cm; + +boolean textLabels, ghostLines, autoRightAngles, omitDuplicateTextLabels, autoLabelingMode, mainPictureMode; + +textLabels := false; +ghostLines := true; +autoRightAngles := false; +omitDuplicateTextLabels := false; +textLabelShift := lineWidth; +autoLabelingMode := false; +mainPictureMode := false; + +boolean solidAngleMode; + +solidAngleMode := false; + +numeric dpLength[]; +dpLength0 := 2lineWidth; +dpLength1 := 2lineWidth; + +vardef sin primary x = (sind(x*radian)) enddef; + +vardef cos primary x = (cosd(x*radian)) enddef; + +vardef arcsind primary x = angle((1+-+x,x)) enddef; + +vardef arccosd primary x = angle((x,1+-+x)) enddef; + +vardef pathToString (expr p) = +save outputString, endpoint, i; + string outputString; + if (length(p) > 0): + outputString := + "(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")" + & ".. controls (" & decimal(xpart(postcontrol 0 of p)) & ", " & decimal(ypart(postcontrol 0 of p)) & ") and " + & "(" & decimal(xpart(precontrol 1 of p)) & ", " & decimal(ypart(precontrol 1 of p)) & ")"; + if (cycle p): + endpoint := length(p) - 1; + else: + endpoint := length(p); + fi; + for i := 1 step 1 until endpoint: + outputString := outputString + & ".. (" & decimal(xpart(point i of p)) & ", " & decimal(ypart(point i of p)) & ")"; + if ((i < length(p)) or (cycle p)): + outputString := outputString + & ".. controls (" & decimal(xpart(postcontrol i of p)) & ", " & decimal(ypart(postcontrol i of p)) & ") and " + & "(" & decimal(xpart(precontrol i + 1 of p)) & ", " & decimal(ypart(precontrol i + 1 of p)) & ")"; + fi; + endfor; + if (cycle p): + outputString := outputString & " .. cycle"; + fi; + else: + outputString := + "(" & decimal(xpart(point 0 of p)) & ", " & decimal(ypart(point 0 of p)) & ")"; + fi; + outputString +enddef; + +color byred, byblue, byyellow, byblack, bygrey, + byredN, byblueN, byyellowN, byblackN, bygreyN, + outlineColor.byredN, outlineColor.byblueN, outlineColor.byyellowN, outlineColor.byblackN, outlineColor.bygreyN, + bytransparent, byDefaultAngleOptionalColor; +byred := (.85, .3, .1); +byblue := (.15, .35, .6); +byyellow := (.95, .7, 0.1); +byblack := black; +bygrey := (.8, .8, .8); +byredN := white; +byblueN := white; +byyellowN := white; +byblackN := white; +bygreyN := white; +outlineColor.byredN := byred; +outlineColor.byblueN := byblue; +outlineColor.byyellowN := byyellow; +outlineColor.byblackN := byblack; +outlineColor.bygreyN := bygrey; +bytransparent := 0.99white; + +byDefaultAngleOptionalColor := white; + +linecap := butt; + +vardef typeOf (expr v) = + save determinedType; + string determinedType; + determinedType := "undefined"; + if (boolean v): + determinedType := "boolean"; + elseif (cmykcolor v): + determinedType := "cmykcolor"; + elseif (color v): + determinedType := "color"; + elseif (rgbcolor v): + determinedType := "rgbcolor"; + elseif (numeric v): + determinedType := "numeric"; + elseif (pair v): + determinedType := "pair"; + elseif (path v): + determinedType := "path"; + elseif (pen v): + determinedType := "pen"; + elseif (picture v): + determinedType := "picture"; + elseif (string v): + determinedType := "string"; + elseif (transform v): + determinedType := "transform"; + fi; + determinedType +enddef; + +vardef sign (expr x) = + if x > 0: 1 fi + if x < 0: -1 fi + if x = 0: 1 fi +enddef; + +vardef colorLightness (expr col) = +save r, g, b; + if color col: + r := redpart(col); + g := greenpart(col); + b := bluepart(col); + elseif cmykcolor col: + r := (1 - cyanpart(col)) * (1 - blackpart(col)); + g := (1 - magentapart(col)) * (1 - blackpart(col)); + b := (1 - yellowpart(col)) * (1 - blackpart(col)); + fi; + (0.2126*r) + (0.7152*g) + (0.0722*b) % from here https://en.wikipedia.org/wiki/Relative_luminance but it doesn't work this way; to be corrected +enddef; + +vardef isWhite (expr col) = + if (colorLightness(col) = 1): % arbitrary value + true + else: + false + fi +enddef; + +vardef isLight (expr col) = + if (colorLightness(col) > 0.95): % arbitrary value + true + else: + false + fi +enddef; + +vardef defineColor@#(expr col) = + if (typeOf(col) = "color"): + color @#; + else: + cmykcolor @#; + fi; + @# := col; +enddef; + +vardef selectOutlineColor@# = + if (typeOf(outlineColor.@#) = "color") or (typeOf(outlineColor.@#) = "cmykcolor"): + outlineColor.@# + else: + black + fi +enddef; + +defineColor.oiBlack((0, 0, 0)); +defineColor.oiOrange((0.9,0.6,0)); +defineColor.oiSkyBlue((0.35,0.7,0.9)); +defineColor.oiGreen((0,0.6,0.5)); +defineColor.oiYellow((0.95,0.9,0.25)); +defineColor.oiBlue((0,0.45,0.7)); +defineColor.oiVermillion((0.8,0.4,0)); +defineColor.oiPurple((0.8,0.6,0.7)); + +% CMYK variants +%defineColor.oiBlack((0,0,0,1)); +%defineColor.oiOrange((0,0.5,1,0)); +%defineColor.oiSkyBlue((0.8,0,0,0)); +%defineColor.oiGreen((0.97,0,0.75,0)); +%defineColor.oiYellow((0.1,0.05,0.9,0)); +%defineColor.oiBlue((1,0.5,0,0)); +%defineColor.oiVermillion((0,0.8,1,0)); +%defineColor.oiPurple((0.1,0.7,0,0)); + +vardef cycleval (expr v, l) = + save rv; + numeric rv; + if (l > 0): + if ((v mod l) < 0): + rv := l - (v mod l); + else: + rv := (v mod l); + fi; + else: + rv := 0; + fi; + rv +enddef; + +vardef angleOpticalScale (expr v) = + save va; + numeric va; + if (v > 60): + va := 60; + else: + va := v; + fi; + ((va/60)**(-1/2)) +enddef; + +vardef distanceToLine (expr givenPoint, givenLine) = + save p; + pair p[]; + p0 := point 0 of givenLine; + p1 := point 0 of reverse(givenLine); + p2 - givenPoint = whatever * ((p1-p0) rotated 90); + p2 = whatever[p0, p1]; + p3 := p2; + arclength(givenPoint -- p3) +enddef; + +vardef angleValue (expr a, b, c) = + save v; + numeric v; + v := angle(a-b) - angle(c-b); + if (v > 180): v := v - 360; fi; + if (v < -180): v := v + 360; fi; + v +enddef; + +vardef lineThickness (expr th) = + if (th = 0): + lineWidth + elseif (th = 1): + lineWidthThin + elseif (th = 2): + lineWidthHair + else: + lineWidth + fi +enddef; + +vardef byDashPattern (expr l, dp) = + save d, n; + numeric d[], n; + n := ceiling(l/(1/3dpLength[dp])); + if (n mod 3) <> 2: + n := n - (n mod 3) + 2; + fi; + d1 := 2(l/n); + d2 := l/n; + dashpattern (on d1 off d2) +enddef; + +def startTempScale (expr tmpScale) = + begingroup + save scaleFactor; + scaleFactor := tmpScale; +enddef; + +def stopTempScale = + endgroup +enddef; + +def startTempAngleScale (expr tmpAngleScale) = + begingroup + save angleScale; + angleScale := tmpAngleScale; +enddef; + +def stopTempAngleScale = + endgroup +enddef; + +def startGlobalRotation (expr ang) = + begingroup + save globalRotation; + globalRotation := ang; +enddef; + +def stopGlobalRotation = + endgroup +enddef; + +def startAutoLabeling = + begingroup + save autoLabelingMode; + boolean autoLabelingMode; + autoLabelingMode := true; +enddef; + +def stopAutoLabeling = + endgroup +enddef; + +def startOffspringPictureMode = + begingroup + save uniqueTextLabels, omitDuplicateTextLabels; + string uniqueTextLabels; + boolean omitDuplicateTextLabels; + omitDuplicateTextLabels := true; + uniqueTextLabels := ""; +enddef; + +def stopOffspringPictureMode = + endgroup +enddef; + +def startMainPictureMode = + begingroup + save mainPictureMode, ghostPicture; + boolean mainPictureMode; + picture ghostPicture; + ghostPicture := image(); + mainPictureMode := true; +enddef; + +def stopMainPictureMode = + endgroup +enddef; + +picture ghostPicture; +ghostPicture := image(); + +% +% This section is devoted to lists +% + +vardef appendList (suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates) = + if (not string listName) or (unknown listName): + string listName; + listName := ""; + fi; + if length(listName) = 0: + listName := valueToAdd; + else: + save valueExists; + boolean valueExists; + valueExists := false; + if omitDuplicates: + valueExists := isInList (valueToAdd, listName); + fi; + if not valueExists: + if (whereToAdd = 1): + listName := listName & ", " & valueToAdd; + else: + listName := valueToAdd & ", " & listName; + fi; + fi; + fi; +enddef; + +vardef isInList (expr valueToLookFor)(suffix listName) = + save rv, i; + boolean rv; + rv := false; + if (not string listName) or (unknown listName): + string listName; + listName := ""; + fi; + forsuffixes i=scantokens(listName): + if (str i = valueToLookFor): + rv := true; + fi; + endfor; + rv +enddef; + +vardef setAttribute@#(expr attrGenus, attrSpecies, attrName, attrValue) = + save finName; + string finName; + if not string scantokens(attrGenus & "Synonym").scantokens(attrName): + finName := attrName; + else: + finName := scantokens(attrGenus & "Synonym").scantokens(attrName); + if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)): + if scantokens(attrGenus & "SynonymPartial").scantokens(attrName): + finName := attrName; + fi; + fi; + fi; + if str @# = "": + scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName); + elseif (typeOf(scantokens(attrGenus & attrSpecies).scantokens(finName)0) <> typeOf(attrValue)): + scantokens(typeOf(attrValue)) scantokens(attrGenus & attrSpecies).scantokens(finName)[]; + fi; + scantokens(attrGenus & attrSpecies).scantokens(finName)@# := attrValue; +enddef; + +vardef getAttribute@#(expr attrGenus, attrSpecies, attrName) = + save finName; + string finName; + if not string scantokens(attrGenus & "Synonym").scantokens(attrName): + finName := attrName; + else: + finName := scantokens(attrGenus & "Synonym").scantokens(attrName); + if (boolean scantokens(attrGenus & "SynonymPartial").scantokens(attrName)): + if scantokens(attrGenus & "SynonymPartial").scantokens(attrName): + if known scantokens(attrGenus & attrSpecies).scantokens(attrName)@#: + finName := attrName; + fi; + fi; + fi; + fi; + scantokens(attrGenus & attrSpecies).scantokens(finName)@# +enddef; + +vardef attributeExists (expr attrGenus, attrSpecies, attrName) = + save rv; + boolean rv; + if known scantokens(attrGenus & attrSpecies).scantokens(attrName): + rv := true; + else: + rv := false; + fi; + rv +enddef; + +setAttribute("line", "Color", "noLine", white); +setAttribute("line", "Dashed", "noLine", 0); +setAttribute("line", "EndAName", "noLine", "noLine"); +setAttribute("line", "EndBName", "noLine", "noLine"); +setAttribute("line", "EndA", "noLine", inf); +setAttribute("line", "EndB", "noLine", inf); +setAttribute("line", "Synonym", "noLine", "noLine"); + +setAttribute("line", "Color", "dummyLine", bygrey); +setAttribute("line", "Dashed", "dummyLine", 0); +setAttribute("line", "Thin", "dummyLine", 1); + +% +% Some general 3D stuff +% + +color projectionAngle; +projectionAngle := (0, 0, 0); + +color rotationAxisB.X, rotationAxisE.X, rotationAxisB.Y, rotationAxisE.Y, rotationAxisB.Z, rotationAxisE.Z; +string rotationAxisPart.X, rotationAxisPart.Y, rotationAxisPart.Z; +rotationAxisB.X := (0, 0, 0); +rotationAxisE.X := (1, 0, 0); +rotationAxisPart.X := "redpart"; +rotationAxisB.Y := (0, 0, 0); +rotationAxisE.Y := (0, 1, 0); +rotationAxisPart.Y := "greenpart"; +rotationAxisB.Z := (0, 0, 0); +rotationAxisE.Z := (0, 0, 1); +rotationAxisPart.Z := "bluepart"; + +primarydef somecolor colorrotated someangle = + rotateColor(somecolor, someangle) +enddef; + +% Rotation order can be selected like this rotateColor.XYZ, +% or by setting defaultRotationOrder + +string defaultRotationOrder; +defaultRotationOrder := "ZYX"; + +vardef rotateColor@#(expr somecolor, someangle) = + save rotationOrder, argumentString, rv; + string rotationOrder[], argumentString; + color rv; + argumentString := str @#; + if argumentString = "": + argumentString := defaultRotationOrder; + fi; + for i := 0 step 1 until length(argumentString) - 1: + rotationOrder[i] = substring (i, i + 1) of argumentString; + endfor; + rv := somecolor; + rv := byRotateAroundAxis( + rotationAxisB.scantokens(rotationOrder0), + rotationAxisE.scantokens(rotationOrder0), + scantokens(rotationAxisPart.scantokens(rotationOrder0))(someangle), rv); + rv := byRotateAroundAxis( + rotationAxisB.scantokens(rotationOrder1), + rotationAxisE.scantokens(rotationOrder1), + scantokens(rotationAxisPart.scantokens(rotationOrder1))(someangle), rv); + rv := byRotateAroundAxis( + rotationAxisB.scantokens(rotationOrder2), + rotationAxisE.scantokens(rotationOrder2), + scantokens(rotationAxisPart.scantokens(rotationOrder2))(someangle), rv); + rv +enddef; + +primarydef colorone crossproduct colortwo = +begingroup + save xp, yp, zp; + numeric xp[], yp[], zp[]; + xp1 := (redpart colorone)/cm; + yp1 := (greenpart colorone)/cm; + zp1 := (bluepart colorone)/cm; + xp2 := (redpart colortwo)/cm; + yp2 := (greenpart colortwo)/cm; + zp2 := (bluepart colortwo)/cm; + xp3 := ((yp1*zp2) - (zp1*yp2))*cm; + yp3 := ((zp1*xp2) - (xp1*zp2))*cm; + zp3 := ((xp1*yp2) - (yp1*xp2))*cm; + (xp3, yp3, zp3) +endgroup +enddef; + +primarydef colorone dotprodXYZ colortwo = +begingroup + save xp, yp, zp; + numeric xp[], yp[], zp[]; + xp1 := (redpart colorone); + yp1 := (greenpart colorone); + zp1 := (bluepart colorone); + xp2 := (redpart colortwo); + yp2 := (greenpart colortwo); + zp2 := (bluepart colortwo); + xp1*xp2 + yp1*yp2 + zp1*zp2 +endgroup +enddef; + +vardef isInPlane (expr colPoint, colPlaneA, colPlaneB, colPlaneC) = + save vecsToCompare, rv; + color vecsToCompare[]; + boolean rv; + rv := false; + if (colPoint = colPlaneA) + or (colPoint = colPlaneB) + or (colPoint = colPlaneC): + rv := true; + else: + vecsToCompare1 := colPlaneA-colPlaneB; + vecsToCompare2 := colPlaneC-colPlaneB; + vecsToCompare3 := colPoint-colPlaneB; + fi; + if not rv: + if ((absXYZ(unitvectorXYZ(vecsToCompare1) - unitvectorXYZ(vecsToCompare3)) < 1/100) + or (absXYZ(unitvectorXYZ(vecsToCompare1) + unitvectorXYZ(vecsToCompare3)) < 1/100) + or (absXYZ(unitvectorXYZ(vecsToCompare2) - unitvectorXYZ(vecsToCompare3)) < 1/100) + or (absXYZ(unitvectorXYZ(vecsToCompare2) + unitvectorXYZ(vecsToCompare3)) < 1/100)): + rv := true; + else: + vecsToCompare4 := unitvectorXYZ(vecsToCompare1 crossproduct vecsToCompare2); + vecsToCompare5 := unitvectorXYZ(vecsToCompare3 crossproduct vecsToCompare2); + if ((absXYZ(vecsToCompare4 - vecsToCompare5) < 1/100) + or (absXYZ(vecsToCompare4 + vecsToCompare5) < 1/100)): + rv := true; + else: + rv := false; + fi; + fi; + fi; + rv +enddef; + +vardef unitvectorXYZ (expr somecolor) = + save vecLength; + numeric vecLength; + vecLength := absXYZ(somecolor); + if (vecLength > 0): + somecolor/vecLength + else: + errmessage("Can't construct a unit vector from a zero-length vector"); + fi +enddef; + +vardef absXYZ (expr somecolor) = + (sqrt( + ((redpart somecolor)/1cm)**2 + + ((greenpart somecolor)/1cm)**2 + + ((bluepart somecolor)/1cm)**2)*1cm) +enddef; + +vardef lineAndPlaneIntersection (expr lineA, lineB, planeA, planeB, planeC) = + save planeNormal, planeOrigin, lineD; + color planeNormal, planeOrigin; + numeric lineD; + planeNormal := unitvectorXYZ((planeA-planeB) crossproduct (planeC-planeB)); + planeOrigin := planeB; + lineD := ((planeNormal/cm) dotprodXYZ ((planeOrigin - lineA)/cm)) + /((planeNormal/cm) dotprodXYZ ((lineB - lineA)/cm)); + lineA + ((lineB-lineA)*lineD) +enddef; + +vardef spaceRotated (suffix somepath)(expr someangle) = + save rv, tv, spacepoint, op, rp, tp; + path rv, tv; + color spacepoint[]; + pair op[], rp[], tp[]; + if (typeOf(thirdDimension.somepath) <> "path"): + path thirdDimension.somepath; + thirdDimension.somepath := somepath scaled 0; + fi; + for i := 0 step 1 until length(somepath): + op0 := point i of somepath; + spacepoint0 := + (xpart(op0), ypart(op0), + xpart(point i of thirdDimension.somepath) + ) colorrotated someangle; + rp0 := (redpart(spacepoint0), greenpart(spacepoint0)); + tp0 := (bluepart(spacepoint0), 0); + if (i = 0): + rv := rp0; + tv := tp0; + else: + op1 := postcontrol (i-1) of somepath; + spacepoint1 := + (xpart(op1), ypart(op1), + xpart(postcontrol (i-1) of thirdDimension.somepath) + ) colorrotated someangle; + rp1 := (redpart(spacepoint1), greenpart(spacepoint1)); + tp1 := (bluepart(spacepoint1), 0); + op2 := precontrol i of somepath; + spacepoint2 := + (xpart(op2), ypart(op2), + xpart(precontrol i of thirdDimension.somepath) + ) colorrotated someangle; + rp2 := (redpart(spacepoint2), greenpart(spacepoint2)); + tp2 := (bluepart(spacepoint2), 0); + rv := rv .. controls rp1 and rp2 .. + if ((i < length(somepath)) or (not cycle somepath)): + rp0 + else: + cycle + fi; + tv := tv .. controls tp1 and tp2 .. + if ((i < length(somepath)) or (not cycle somepath)): + tp0 + else: + cycle + fi; + fi; + endfor; + thirdDimension.somepath := tv; + rv +enddef; + +vardef bySetProjection@#(expr xa, ya, za) = + projectionAngle := (xa, ya, za); + byRotatePoints@#(xa, ya, za, false)(); + if str @# <> "": + defaultRotationOrder := str @#; + fi; +enddef; + +vardef byPutPointsInSpace (text pL) = + byRotatePoints(0, 0, 0, true)(pL); +enddef; + +% Gets a either a 2d or a 3d point and returns a 3d point + +vardef byGetPointXYZ (suffix givenPoint) = + if (typeOf(givenPoint) = "color"): + givenPoint + elseif (typeOf(pointXYZ.givenPoint) = "color"): + pointXYZ.givenPoint + else: + errmessage("Point " & str givenPoint & " has no representation in space"); + fi +enddef; + +vardef byRotateAroundAxis (expr lineEndA, lineEndB, a, pointA) = + save vecA, vecB, rV, q; + color vecA, vecB, rV; + cmykcolor q[]; + vecA := pointA - lineEndA; + vecB := unitvectorXYZ(lineEndB - lineEndA); + q1 := (0, redpart(vecA), greenpart(vecA), bluepart(vecA)); + q2 := (cosd(a/2), sind(a/2)*redpart(vecB), sind(a/2)*greenpart(vecB), sind(a/2)*bluepart(vecB)); + q3 := (cyanpart(q2), -magentapart(q2), -yellowpart(q2), -blackpart(q2)); + q4 := (q2 hamiltonProduct q1) hamiltonProduct q3; + rV := (magentapart(q4), yellowpart(q4), blackpart(q4)) + lineEndA; + rV +enddef; + +vardef byRotatePointsAroundAxis (suffix lineEndA, lineEndB)(expr a)(text pointsList) = + save lineEndAxyz, lineEndBxyz, pN; + color lineEndAxyz, lineEndBxyz; + if (typeOf(lineEndA) = "color"): + lineEndAxyz := lineEndA; + else: + byPutPointsInSpace(lineEndA); + lineEndAxyz := pointXYZ.lineEndA; + fi; + if (typeOf(lineEndB) = "color"): + lineEndBxyz := lineEndB; + else: + byPutPointsInSpace(lineEndB); + lineEndBxyz := pointXYZ.lineEndB; + fi; + forsuffixes pN := pointsList: + if (typeOf(pN) = "color"): + pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pN); + elseif (typeOf(pN) = "pair"): + byPutPointsInSpace(pN); + pointXYZ.pN := byRotateAroundAxis(lineEndAxyz, lineEndBxyz, a, pointXYZ.pN); + fi; + endfor; +enddef; + +primarydef colorone hamiltonProduct colortwo = +begingroup + save a, b, c, d; + numeric a[], b[], c[], d[]; + a1 := cyanpart(colorone); + b1 := magentapart(colorone); + c1 := yellowpart(colorone); + d1 := blackpart(colorone); + a2 := cyanpart(colortwo); + b2 := magentapart(colortwo); + c2 := yellowpart(colortwo); + d2 := blackpart(colortwo); + ( + (a1*a2) - (b1*b2) - (c1*c2) - (d1*d2), + (a1*b2) + (b1*a2) + (c1*d2) - (d1*c2), + (a1*c2) - (b1*d2) + (c1*a2) + (d1*b2), + (a1*d2) + (b1*c2) - (c1*b2) + (d1*a2) + ) +endgroup +enddef; + +vardef isRightAngle (suffix a, b, c) = + save d, rv; + numeric d; + boolean rv; + if (attributeExists("point", "XYZ", str a) + and attributeExists("point", "XYZ", str b) + and attributeExists("point", "XYZ", str c)): + d := unitvectorXYZ(pointXYZ.a - pointXYZ.b) dotprodXYZ unitvectorXYZ(pointXYZ.c - pointXYZ.b); + else: + d := unitvector(a - b) dotprod unitvector(c - b); + fi; + if (abs(d) < 1/1000): + rv := true; + else: + rv := false; + fi; + rv +enddef; + +% +% This section is dedicated to lines +% + +vardef byLineRender (expr a, b, col, dp, th, c, d, ct, dt, s, sf) = + save p, la, lineItself, lineBleeding, clippingPath, cutAngle, linePerp, lineStyShift, currentLineWidth, clippingPathPoint, aS, bS; + picture p; + path lineItself, lineBleeding, clippingPath; + pair cutAngle[], linePerp, lineStyShift, clippingPathPoint[], aS, bS; + la := angle(b-a); + aS := a*sf; + bS := b*sf; + lineStyShift := (0, 0); + currentLineWidth := lineThickness(th); + cutAngle1 := unitvector(b-a) rotated -90; + if (abs(c-a) > 0): + if (abs(unitvector(c-a)-unitvector(a-b)) > 1/100): + if (ct = 0): + cutAngle1 := unitvector(1/2[unitvector(b-a), unitvector(c-a)]); + elseif (ct = 1): + cutAngle1 := unitvector(1[unitvector(b-a), unitvector(c-a)]); + fi; + fi; + fi; + cutAngle2 := unitvector(a-b) rotated 90; + if (abs(d-b) > 0): + if (abs(unitvector(d-b)-unitvector(b-a)) > 1/100): + if (dt = 0): + cutAngle2 := unitvector(1/2[unitvector(a-b), unitvector(d-b)]); + elseif (dt = 1): + cutAngle2 := unitvector(1[unitvector(a-b), unitvector(d-b)]); + fi; + fi; + fi; + if (sign(ypart(cutAngle2 rotated -la)) <> sign(ypart(cutAngle1 rotated -la))): + cutAngle2 := cutAngle2 rotated 180; + fi; + lineItself := (a--b) scaled sf; + linePerp := unitvector((point 0 of lineItself)-(point 1 of lineItself)) rotated 90; + lineBleeding := + ((point 0 of lineItself) shifted (unitvector((point 0 of lineItself) - (point 1 of lineItself)) scaled 2dpLength[dp])) -- + lineItself -- ((point 1 of lineItself) shifted (unitvector((point 1 of lineItself) - (point 0 of lineItself)) scaled 2dpLength[dp])); + lineStyShift := (linePerp scaled (1/2currentLineWidth*s)); + p := image( + pickup pencircle scaled currentLineWidth; + if (dp > 0): + draw (lineItself shifted lineStyShift) withcolor white; + draw (lineItself shifted lineStyShift) withcolor col + dashed byDashPattern(arclength(lineItself), dp); + draw (subpath(0, 1) of lineBleeding shifted lineStyShift) withcolor col; + draw (subpath(2, 3) of lineBleeding shifted lineStyShift) withcolor col; + else: + draw (lineBleeding shifted lineStyShift) withcolor col; + fi; + if (th < 0): + draw (lineBleeding shifted (lineStyShift + (linePerp scaled 1/4currentLineWidth))) withpen pencircle scaled 1/2currentLineWidth withcolor black; + fi; + ); + clippingPathPoint1 + = whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift, + bS shifted (linePerp*1/2lineWidth) shifted lineStyShift] + = whatever[aS, aS shifted cutAngle1]; + clippingPathPoint2 + = whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift, + bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift] + = whatever[aS, aS shifted cutAngle1]; + clippingPathPoint3 + = whatever[aS shifted (linePerp*-1/2lineWidth) shifted lineStyShift, + bS shifted (linePerp*-1/2lineWidth) shifted lineStyShift] + = whatever[bS, bS shifted cutAngle2]; + clippingPathPoint4 + = whatever[aS shifted (linePerp*1/2lineWidth) shifted lineStyShift, + bS shifted (linePerp*1/2lineWidth) shifted lineStyShift] + = whatever[bS, bS shifted cutAngle2]; + clippingPath := clippingPathPoint1 -- clippingPathPoint2 -- clippingPathPoint3 -- clippingPathPoint4 -- cycle; + clip p to clippingPath; + if mainPictureMode and ghostLines: + save gP; + picture gP; + gP := ghostPicture; + ghostPicture := image( + draw gP; + begingroup; + save ghostLines; + boolean ghostLines; + ghostLines := false; + draw byLineRender (a, b, white, dp, 2, c, d, ct, dt, s, sf); + % draw byLineRender (a, b, col, dp, 2, c, d, ct, dt, s, sf); + endgroup; + ); + fi; + p rotated globalRotation +enddef; + +vardef byReturnLineLength (suffix a, b) = + if byIsPointInSpace(a, b): + absXYZ(pointXYZ.a - pointXYZ.b) + else: + abs(a-b) + fi +enddef; + +vardef byLineDefine@#(suffix a, b)(expr col, dp, th) = +if str @# = "": + byLineDefine.scantokens(str a & str b)(a, b)(col, dp, th); +else: + save lA; + if mainPictureMode: + appendList(allLinesList, str @#, 1, true); + fi; + if byIsPointInSpace(a, b): + if (typeOf(a) = "pair") and (typeOf(b) = "pair"): + if (a <> b): + lA := angle(b-a); + else: + lA := 0; + fi; + else: + lA := 0; + fi; + else: + if (a <> b): + lA := angle(b-a); + else: + lA := 0; + fi; + fi; + setAttribute("line", "Angle", str @#, lA); + setAttribute("line", "Color", str @#, col); + setAttribute("line", "Dashed", str @#, dp); + setAttribute("line", "Thin", str @#, th); + setAttribute("line", "EndA", str @#, a); + setAttribute("line", "EndB", str @#, b); + setAttribute("line", "Shift", str @#, 0); + setAttribute("line", "DirA", str @#, a); + setAttribute("line", "DirB", str @#, b); + setAttribute("line", "EndAType", str @#, 0); + setAttribute("line", "EndBType", str @#, 0); + setAttribute("line", "EndAName", str @#, str a); + setAttribute("line", "EndBName", str @#, str b); + setAttribute("line", "DirAName", str @#, str a); + setAttribute("line", "DirBName", str @#, str b); + setAttribute("line", "UseLineLabel", str @#, false); + setAttribute("line", "Label", str @#, str @#); + setAttribute("line", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byLineStylize (suffix c, d)(expr ct, dt, s) (suffix lineName) = + setAttribute("line", "Shift", str lineName, s); + setAttribute("line", "DirA", str lineName, c); + setAttribute("line", "DirB", str lineName, d); + setAttribute("line", "DirAName", str lineName, str c); + setAttribute("line", "DirBName", str lineName, str d); + setAttribute("line", "EndAType", str lineName, ct); + setAttribute("line", "EndBType", str lineName, dt); +enddef; + +vardef byLine@#(suffix a, b)(expr col, dp, th) = +if str @# = "": + byLine.scantokens(str a & str b)(a, b)(col, dp, th) +else: + byLineDefine@#(a, b, col, dp, th); + byNamedLineFull(a, b, 0, 0, 0)(@#) +fi +enddef; + +vardef byLineFull@#(suffix a, b)(expr col, dp, th)(suffix c, d)(expr ct, dt, s) = +if str @# = "": + byLineFull.scantokens(str a & str b)(a, b)(col, dp, th)(c, d)(ct, dt, s) +else: + byLineDefine@#(a, b) (col, dp, th); + byLineStylize (c, d, ct, dt, s)(@#); + byNamedLine(@#) +fi +enddef; + +% This one's behaviour is slightly off, since it affects line label type + +vardef byLineWithName (suffix a, b)(expr col, dp, th)(suffix lineLabel) = + byLineDefine.lineLabel(a, b, col, dp, th); + setAttribute("line", "UseLineLabel", str lineLabel, true); + byNamedLineFull(a, b, 0, 0, 0)(lineLabel) +enddef; + +vardef byNamedLineFull (expr c, d, ct, dt, s) (suffix lineName) = + if (mainPictureMode): + appendList(pointLinesList.scantokens(getAttribute("line", "EndAName", str lineName)), str lineName, 1, true); + appendList(pointLinesList.scantokens(getAttribute("line", "EndBName", str lineName)), str lineName, 1, true); + fi; + byLineRender( + scantokens(getAttribute("line", "EndAName", str lineName)), + scantokens(getAttribute("line", "EndBName", str lineName)), + getAttribute("line", "Color", str lineName), + getAttribute("line", "Dashed", str lineName), + getAttribute("line", "Thin", str lineName), + c, d, ct, dt, s, scaleFactor) +enddef; + +vardef byNamedLine (text linesList) = + image( + forsuffixes lN=linesList: + draw byNamedLineFull( + scantokens(getAttribute("line", "DirAName", str lN)), + scantokens(getAttribute("line", "DirBName", str lN)), + getAttribute("line", "EndAType", str lN), + getAttribute("line", "EndBType", str lN), + getAttribute("line", "Shift", str lN))(lN); + endfor; + ) +enddef; + +vardef byNamedLineSeq (expr s)(text linesListRaw) = + save lAname, lBname, lShift, lName, i, j, k, l, m, n, c, d, prp, pointNames, linesList; + string lAname[], lBname[], lName[], c, d, pointNames, linesList; + numeric i, j, k, l, m, n, prp, lShift[]; + linesList := byExpandLines(linesListRaw); + pointNames := ""; + image( + i := -1; + forsuffixes lN=scantokens(linesList): + i := i + 1; + lAname[i] := getAttribute("line", "EndAName", str lN); + lBname[i] := getAttribute("line", "EndBName", str lN); + lShift[i] := getAttribute("line", "Shift", str lN); + lName[i] := str lN; + endfor; + if (i = 1): + i := i + 1; + lAname[i] := "noLine"; + lBname[i] := "noLine"; + lName[i] := "noLine"; + fi; + if (i > 0): + for j := 0 step 1 until i: + k := cycleval (j - 1, i + 1); + l := cycleval (j + 1, i + 1); + m := 0; + n := 0; + if (lAname[k] = "noLine"): + k := cycleval (k - 1, i + 1); + m := 1; + fi; + if (lAname[l] = "noLine"): + l := cycleval (l + 1, i + 1); + n := 1; + fi; + c := lAname[j]; + d := lBname[j]; + prp := 0; + if (((lAname[j] = lAname[k]) or (lAname[j] = lBname[k])) + and (not ((m <> 0) and (lName[k] = lName[l])))) or + (((lBname[j] = lAname[l]) or (lBname[j] = lBname[l])) + and (not ((n <> 0) and (lName[k] = lName[l])))): + if (lAname[j] = lAname[k]): + if (m = 0): c := lBname[k]; fi; + elseif (lAname[j] = lBname[k]): + if (m = 0): c := lAname[k]; fi; + else: + m := 2; + fi; + if (lBname[j] = lAname[l]): + if (n = 0): d := lBname[l]; fi; + elseif (lBname[j] = lBname[l]): + if (n = 0): d := lAname[l]; fi; + else: + n := 2; + fi; + prp := 1; + else: + if (lAname[j] = lAname[l]): + if (n = 0): c := lBname[l]; fi; + elseif (lAname[j] = lBname[l]): + if (n = 0): c := lAname[l]; fi; + else: + n := 2; + fi; + if (lBname[j] = lAname[k]): + if (m = 0): d := lBname[k]; fi; + elseif (lBname[j] = lBname[k]): + if (m = 0): d := lAname[k]; fi; + else: + m := 2; + fi; + prp := -1; + fi; + if (lName[j] <> "noLine"): + if (prp = 1): + pointNames := pointNames & lAname[j] & ", "; + if (n = 2): + pointNames := pointNames & lBname[j] & ", noPoint, "; + fi; + elseif (prp = -1): + pointNames := pointNames & lBname[j] & ", "; + if (n = 2): + pointNames := pointNames & lAname[j] & ", noPoint, "; + fi; + fi; + lShift[j] := s*prp; + draw byNamedLineFull(scantokens(c), scantokens(d), 0, 0, -s*sign(prp))(scantokens(lName[j])); + else: + prp := -2; + fi; + endfor; + if (prp = 1) and (n <> 2): + pointNames := pointNames & lBname[i]; + elseif (prp = -1) and (n <> 2): + pointNames := pointNames & lAname[i]; + else: + pointNames := substring (0, length(pointNames)-2) of pointNames; + fi; + if (textLabels and autoLabelingMode): + draw byLabelsOnPolygon(scantokens pointNames)(1, s); + fi; + else: + forsuffixes lN=scantokens(linesList): + setAttribute("line", "Shift", str lN, s); + draw byNamedLineFull( + scantokens(getAttribute("line", "EndAName", str lN)), + scantokens(getAttribute("line", "EndBName", str lN)), + 0, 0, -s)(lN); + if (textLabels and autoLabelingMode): + draw byLabelsOnPolygon(scantokens (getAttribute("line", "EndAName", str lN) & ", " & getAttribute("line", "EndBName", str lN)))(1, s); + fi; + endfor; + fi; + ) +enddef; + +vardef byMarkLine(expr pos, col)(suffix lineName) = + setAttribute("mark", "Position", str lineName, pos); + setAttribute("mark", "Color", str lineName, col); + byNamedMarkLine(lineName) +enddef; + +vardef byNamedMarkLine(suffix lineName) = + save p, q; + pair q[]; + picture p; + p := image( + q0 := getAttribute("mark", "Position", str lineName)[ + scantokens(getAttribute("line", "EndAName", str lineName)), + scantokens(getAttribute("line", "EndBName", str lineName)) + ]; + q1 := q0 shifted (dir(getAttribute("line", "Angle", str lineName) + 90) scaled 1/2markLength); + q2 := q0 shifted (dir(getAttribute("line", "Angle", str lineName) - 90) scaled 1/2markLength); + q3 := (dir(getAttribute("line", "Angle", str lineName) - 90) scaled (1/2lineWidth*getAttribute("line", "Shift", str lineName))); + draw (q1--q2) shifted q3 withpen pencircle scaled lineWidthThin withcolor getAttribute("mark", "Color", str lineName); + ); + p rotated globalRotation +enddef; + +vardef byFindLinePointedThere (expr a, b) = + save endA, endB, endAC, endBC, endAA, endBA, chosenLine, lL; + string chosenLine; + pair endA, endB, endAC, endBC, endAA, endBA; + numeric lL; + endA := scantokens(a); + endB := b; + if known pointLinesList.scantokens(a): + forsuffixes lL=scantokens(pointLinesList.scantokens(a)): + endAC := scantokens(getAttribute("line", "EndAName", str lL)); + endBC := scantokens(getAttribute("line", "EndBName", str lL)); + if (endAC = endA): + endAA := endAC; + endBA := endBC; + elseif (endBC = endA): + endAA := endBC; + endBA := endAC; + fi; + if ((abs(angle(endBA - endAA) - angle(endB - endA)) mod 360) < 1): + chosenLine := str lL; + fi; + endfor; + fi; + if unknown chosenLine: + chosenLine := ""; + fi; + chosenLine +enddef; + +vardef byConstructCompoundLine (expr lineName) = + save lineStart, lEnd, newLine, lEndA, lEndB, compoundLineString, i, j, cPointA, cPointB; + string lineStart, lEnd, newLine, lEndA, lEndB, compoundLineString, cPointA, cPointB; + numeric i, j; + j := 0; + if (length(lineName) = 2): + lineStart := substring (0, 1) of lineName; + lEnd := substring (1, 2) of lineName; + else: + for i := 1 step 1 until length(lineName): + cPointA := substring (0, i) of lineName; + cPointB := substring (i, length(lineName)) of lineName; + if known scantokens(cPointA): % for some reason 'and' doesn't work here + if known scantokens(cPointB): + j := j + 1; + lineStart := cPointA; + lEnd := cPointB; + fi; + fi; + endfor; + fi; + if unknown lineStart: + errmessage("Can't find points for line: " & lineName); + fi; + if j > 1: + errmessage("Ambiguous line name: " & lineName); + fi; + i := 0; + forever: + i := i + 1; + newLine := byFindLinePointedThere(lineStart, scantokens(lEnd)); + if (newLine = ""): + errmessage("There's no line from " & lineStart & " to " & lEnd); + fi; + appendList (compoundLineString, newLine, 1, false); + lEndA := getAttribute("line", "EndAName", newLine); + lEndB := getAttribute("line", "EndBName", newLine); + if (lEndA = lineStart): + lineStart := lEndB; + elseif (lEndB = lineStart): + lineStart := lEndA; + fi; + exitif (i > 10); + exitif (lEndA = lEnd); + exitif (lEndB = lEnd); + endfor; + compoundLineString +enddef; + +vardef byExpandLines (text linesList) = + save lineString, compoundLineString; + string lineString, compoundLineString; + forsuffixes lN=linesList: + if attributeExists("line", "Synonym", str lN): + appendList(lineString, str lN, 1, false); + else: + appendList(lineString, byConstructCompoundLine(str lN), 1, false); + fi; + endfor; + lineString +enddef; + +vardef byNamedCompoundLine (expr cu, pw)(text linesList) = + byNamedCompoundLineRender(cu, pw, 0)(scantokens(byExpandLines(linesList))) +enddef; + +vardef byNamedCompoundRay (expr cu, pw)(text linesList) = + byNamedCompoundLineRender(cu, pw, 1)(scantokens(byExpandLines(linesList))) +enddef; + +vardef byNamedCompoundIndLine (expr cu, pw)(text linesList) = + byNamedCompoundLineRender(cu, pw, 2)(scantokens(byExpandLines(linesList))) +enddef; + +vardef byNamedCompoundLineRender (expr cu, pw, sty)(text linesList) = + save correctedLength, currentPosition, a, b, sc, totalLength, pwL, lineNames, endNames, i, j, angleCheck, lineNotStraight, lineNotContinuous, globalRotation, extensionNames, textLabelImage; + numeric correctedLength, currentPosition[], sc, totalLength, pwL, i, j, angleCheck[]; + globalRotation := 0; + string lineNames[], endNames[], extensionNames[]; + pair a, b; + picture textLabelImage[]; + boolean lineNotStraight, lineNotContinuous; + i := 0; + totalLength := 0; + lineNotStraight := false; + lineNotContinuous := false; + forsuffixes lN=linesList: + lineNames[i] := getAttribute("line", "EndAName", str lN); + lineNames[i+1] := getAttribute("line", "EndBName", str lN); + if (i > 0): + if (lineNames[i] <> lineNames[i-2]) + and (lineNames[i] <> lineNames[i-1]) + and (lineNames[i+1] <> lineNames[i-2]) + and (lineNames[i+1] <> lineNames[i-1]): + lineNotContinuous := true; + fi; + fi; + i := i + 2; + angleCheck[i/2] := abs(cosd(getAttribute("line", "Angle", str lN))); + if (abs(angleCheck[i/2] - angleCheck[1]) > 1/100): + lineNotStraight := true; + fi; + totalLength := totalLength + + byReturnLineLength( + scantokens(getAttribute("line", "EndAName", str lN)), + scantokens(getAttribute("line", "EndBName", str lN))); + endfor; + if (lineNotStraight): + message("The line is not straight"); + fi; + if (lineNotContinuous): + errmessage("The line is not continuous"); + fi; + if (pw <> 0): + sc := cu**(1 - (1/pw)); + pwL := 1/pw; + else: + sc := cu/(totalLength*scaleFactor); + pwL := 1; + fi; + currentPosition0 := 0; + image( + j := 0; + forsuffixes lN=linesList: + correctedLength := (byReturnLineLength( + scantokens(getAttribute("line", "EndAName", str lN)), + scantokens(getAttribute("line", "EndBName", str lN)) + )**pwL)*sc; + currentPosition1 := currentPosition0 + correctedLength; + a := (currentPosition0, 0); + b := (currentPosition1, 0); + draw byLineRender( + a, b, + getAttribute("line", "Color", str lN), + getAttribute("line", "Dashed", str lN), + getAttribute("line", "Thin", str lN), + a, b, 0, 0, 0, scaleFactor); + currentPosition0 := currentPosition1; + if (i > 2): + if (j = 0): + extensionNames3 := str lN; + if (getAttribute("line", "EndAName", str lN) = lineNames[2]) + or (getAttribute("line", "EndAName", str lN) = lineNames[3]): + endNames[0] := getAttribute("line", "EndBName", str lN); + elseif (getAttribute("line", "EndBName", str lN) = lineNames[2]) + or (getAttribute("line", "EndBName", str lN) = lineNames[3]): + endNames[0] := getAttribute("line", "EndAName", str lN); + fi; + fi; + if (j = (i-2)/2): + extensionNames2 := str lN; + if (getAttribute("line", "EndAName", str lN) = lineNames[i-3]) + or (getAttribute("line", "EndAName", str lN) = lineNames[i-4]): + endNames[1] := getAttribute("line", "EndBName", str lN); + elseif (getAttribute("line", "EndBName", str lN) = lineNames[i-3]) + or (getAttribute("line", "EndBName", str lN) = lineNames[i-4]): + endNames[1] := getAttribute("line", "EndAName", str lN); + fi; + fi; + else: + endNames[0] := getAttribute("line", "EndAName", str lN); + endNames[1] := getAttribute("line", "EndBName", str lN); + if (getAttribute("line", "UseLineLabel", str lN)): + endNames[2] := str lN; + fi; + fi; + j := j + 1; + endfor; + extensionNames0 := byFindLinePointedThere(endNames1, 2[scantokens(endNames0), scantokens(endNames1)]); + extensionNames1 := byFindLinePointedThere(endNames0, 2[scantokens(endNames1), scantokens(endNames0)]); + if extensionNames0 = "": + extensionNames0 := "dummyLine"; + else: + extensionNames2 := extensionNames0; + fi; + if extensionNames1 = "": + extensionNames1 := "dummyLine"; + else: + extensionNames3 := extensionNames1; + fi; + if (sty > 0): + draw byLineRender(b, b shifted (rayExtension/scaleFactor, 0), + getAttribute("line", "Color", extensionNames0), + getAttribute("line", "Dashed", extensionNames0), + getAttribute("line", "Thin", extensionNames2), + b, b shifted (rayExtension/scaleFactor, 0), 0, 0, 0, scaleFactor); + fi; + if (sty > 1): + draw byLineRender((0, 0), (0, 0) shifted (-rayExtension/scaleFactor, 0), + getAttribute("line", "Color", extensionNames1), + getAttribute("line", "Dashed", extensionNames1), + getAttribute("line", "Thin", extensionNames3), + (0, 0), (0, 0) shifted (-rayExtension/scaleFactor, 0), 0, 0, 0, scaleFactor); + fi; + if (textLabels and autoLabelingMode): + if unknown endNames[2]: + textLabelImage1 := byTextLabel(pointLabel)(endNames[0], (0, 0), 90, textLabelShift); + textLabelImage2 := byTextLabel(pointLabel)(endNames[1], b, 90, textLabelShift); + draw textLabelImage1; + draw textLabelImage2; + % these are only needed to make the image symmetrical in order to have + % the line with and without labels at the same level in text. + draw (textLabelImage1 yscaled -1) withcolor white; + draw (textLabelImage2 yscaled -1) withcolor white; + else: + textLabelImage1 := byTextLabel(lineLabel)(endNames[2], 1/2[(0, 0), b], 90, textLabelShift); + draw textLabelImage1; + draw (textLabelImage1 yscaled -1) withcolor white; + fi; + fi; + ) +enddef; + +vardef generateLineSynonyms = + if string allLinesList: + save candidateName, originalName; + string candidateName[], originalName; + forsuffixes i=scantokens(allLinesList): + originalName := str i; + candidateName1 := getAttribute("line", "EndAName", originalName) + & getAttribute("line", "EndBName", originalName); + candidateName2 := getAttribute("line", "EndBName", originalName) + & getAttribute("line", "EndAName", originalName); + if not string getAttribute("line", "Synonym", candidateName1): + setAttribute("line", "Synonym", candidateName1, originalName); + fi; + if not string getAttribute("line", "Synonym", candidateName2): + setAttribute("line", "SynonymPartial", candidateName2, true); + setAttribute("line", "Synonym", candidateName2, originalName); + setAttribute("line", "EndA", candidateName2, getAttribute("line", "EndB", originalName)); + setAttribute("line", "EndB", candidateName2, getAttribute("line", "EndA", originalName)); + setAttribute("line", "EndAType", candidateName2, getAttribute("line", "EndBType", originalName)); + setAttribute("line", "EndBType", candidateName2, getAttribute("line", "EndAType", originalName)); + setAttribute("line", "EndAName", candidateName2, getAttribute("line", "EndBName", originalName)); + setAttribute("line", "EndBName", candidateName2, getAttribute("line", "EndAName", originalName)); + setAttribute("line", "DirA", candidateName2, getAttribute("line", "DirB", originalName)); + setAttribute("line", "DirB", candidateName2, getAttribute("line", "DirA", originalName)); + setAttribute("line", "DirAName", candidateName2, getAttribute("line", "DirBName", originalName)); + setAttribute("line", "DirBName", candidateName2, getAttribute("line", "DirAName", originalName)); + setAttribute("line", "Angle", candidateName2, 180 + getAttribute("line", "Angle", originalName)); + fi; + endfor; + fi; +enddef; + +% +% Points +% + +vardef byPointLabelDefine (suffix pointName)(expr t) = + setAttribute("point", "Label", str pointName, t); +enddef; + +vardef byPointLabelRemove (text pointsList) = + forsuffixes i=pointsList: + byPointLabelDefine(i, ""); + endfor; +enddef; + +vardef byPointMarkDefine (suffix pointName)(expr col, sty) = + setAttribute("point", "Color", str pointName, col); + setAttribute("point", "Style", str pointName, sty); +enddef; + +vardef byPointXYZDefine@#(expr ox, oy, oz) = +if str @# = "": + errmessage("byPointXYZDefine needs a name (byPointXYZDefine.somename...)"); +else: + if mainPictureMode: + appendList(allPointsList, str @#, 1, true); + fi; + setAttribute("point", "XYZ", str @#, (ox, oy, oz)); + pair @#; + color pointXYZ.@#; + @# := (ox, oy); + pointXYZ.@# := (ox, oy, oz); +fi; +enddef; + +vardef byPointXYZEmpty (text pointNames) = + forsuffixes pN = pointNames: + if mainPictureMode: + appendList(allPointsList, str pN, 1, true); + fi; + color pointXYZ.pN; + endfor; +enddef; + +vardef byIsPointInSpace (text pointNames) = + save pN; + (true forsuffixes pN = pointNames: + and (typeOf(pointXYZ.pN) = "color") + endfor) +enddef; + +% in order to automatically project points they need to be added +% to allPointsList list. +vardef byPair (text pointsList) = + save i; + pair pointsList; + forsuffixes i := pointsList: + appendList(allPointsList, str i, 1, true); + numeric pointXYZ.i; + endfor; +enddef; + +% @# here stands for rotation order, e. g. byRotatePoints.XYZ + +vardef byRotatePoints@#(expr xa, ya, za, toRotate)(text pointsList) = + save oxyz, pointsToProcess; + color oxyz; + string pointsToProcess; + forsuffixes i=pointsList: + appendList(pointsToProcess, str i, 1, true); + endfor; + if (length(pointsToProcess) = 0): + pointsToProcess := allPointsList; + fi; + forsuffixes i=scantokens(pointsToProcess): + if (known i) or (known pointXYZ.i): + if attributeExists("point", "XYZ", str i): + oxyz := getAttribute("point", "XYZ", str i); + elseif typeOf(i) = "pair": + byPointXYZDefine.i(xpart i, ypart i, 0); + oxyz := getAttribute("point", "XYZ", str i); + else: + errmessage("Point " & str i & " doesn't exist"); + fi; + oxyz := rotateColor@#(oxyz, (xa, ya, za)); + pair i; + i := (redpart(oxyz), greenpart(oxyz)); + if toRotate: + setAttribute("point", "XYZ", str i, oxyz); + fi; + fi; + endfor; +enddef; + +vardef byPointMarkRender (expr p, col, sty) = + image( + if (sty = 0): + fill ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor col; + elseif (sty = 1): + fill ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor white; + draw ((fullcircle scaled pointMarkSize) shifted (p scaled scaleFactor)) withcolor col withpen pencircle scaled lineWidthHair; + fi; + ) +enddef; + +vardef byNamedPointMark (text pointsList) = + image( + forsuffixes pN=pointsList: + if (attributeExists("point", "Color", str pN)): + appendList(pL, str pN, 1, false); + draw byPointMarkRender(pN, getAttribute("point", "Color", str pN), getAttribute("point", "Style", str pN)) rotated globalRotation; + if (autoLabelingMode and textLabels): + draw byTextLabel(pointLabel)(str pN, pN, 90, 1/2pointMarkSize + textLabelShift); + fi; + fi; + endfor; + ) +enddef; + +vardef byPointMark (suffix pointName)(expr col, sty) = + image( + byPointMarkDefine(pointName)(col, sty); + draw byNamedPointMark(pointName); + ) +enddef; + +vardef byNamedPointLines (suffix pointName)(expr omitLines) = +save linesList, lineNames, pointNames, distantEnd, distantEnds, n, no, distL, maxDist, maxDistEndA, maxDistEndB, a, angleL, minAngle, maxAngle, minAngleEndB, maxAngleEndA, maxAngleEndB, safeLine; +string linesList, lineNames[], pointNames[]; +pair distantEnd, distantEnds[]; +numeric n, no, distL, maxDist, maxDistEndA, maxDistEndB, a[], angleL, minAngle, maxAngle, minAngleEndB, maxAngleEndA, maxAngleEndB, safeLine; +picture labelImage, linesImage; +boolean omitLine[]; +if known pointLinesList.pointName: + linesList := pointLinesList.pointName; + n := 0; + no := 0; + maxDist := 0; + maxAngle := 0; + minAngle := 360; + forsuffixes i = scantokens(linesList): + n := n + 1; + lineNames[n] := str i; + omitLine[n] := false; + if (length(omitLines) > 0): + forsuffixes j = scantokens(omitLines): + if ((str i) = (str j)): + omitLine[n] := true; + no := no + 1; + fi; + endfor; + fi; + if (not omitLine[n]): + safeLine := n; + fi; + if (getAttribute("line", "EndAName", str i) = str pointName): + distantEnds[n] := + scantokens(getAttribute("line", "EndAName", str i)) + + (unitvector(scantokens(getAttribute("line", "EndBName", str i)) + - scantokens(getAttribute("line", "EndAName", str i))) scaled (pointLinesSize/scaleFactor) + ); + pointNames[n] := getAttribute("line", "EndBName", str i); + else: + distantEnds[n] := + scantokens(getAttribute("line", "EndBName", str i)) + + (unitvector(scantokens(getAttribute("line", "EndAName", str i)) + - scantokens(getAttribute("line", "EndBName", str i))) scaled (pointLinesSize/scaleFactor) + ); + pointNames[n] := getAttribute("line", "EndAName", str i); + fi; + endfor; + if (n - no > 1): + for i := 1 step 1 until n: + if (not omitLine[i]): + minAngle := 360; + for j := 1 step 1 until n: + if (not omitLine[j]): + distL := abs(distantEnds[i] - distantEnds[j]); + if (distL > maxDist): + maxDist := distL; + maxDistEndA := i; + maxDistEndB := j; + fi; + a1 := angle(distantEnds[i] - pointName); + a2 := angle(distantEnds[j] - pointName); + angleL := a2 - a1; + if (angleL < 0): angleL := angleL + 360; fi; + if (minAngle > angleL) and (angleL <> 0): + minAngle := angleL; + minAngleEndA := j; + fi; + fi; + endfor; + if (maxAngle <= minAngle + 1): + maxAngle := minAngle; + maxAngleEndA := minAngleEndA; + maxAngleEndB := i; + fi; + fi; + endfor; + linesImage := image( + for i := 1 step 1 until n: + if (i <> maxDistEndA) and (i <> maxDistEndB) and (not omitLine[i]): + draw byLineRender(pointName, distantEnds[i], + getAttribute("line", "Color", lineNames[i]), getAttribute("line", "Dashed", lineNames[i]), getAttribute("line", "Thin", lineNames[i]), + pointName, distantEnds[i], 0, 0, 0, scaleFactor); + fi; + endfor; + draw byLineRender(pointName, distantEnds[maxDistEndA], + getAttribute("line", "Color", lineNames[maxDistEndA]), + getAttribute("line", "Dashed", lineNames[maxDistEndA]), + getAttribute("line", "Thin", lineNames[maxDistEndA]), + distantEnds[maxDistEndB], distantEnds[maxDistEndA], 0, 0, 0, scaleFactor); + draw byLineRender(pointName, distantEnds[maxDistEndB], + getAttribute("line", "Color", lineNames[maxDistEndB]), + getAttribute("line", "Dashed", lineNames[maxDistEndB]), + getAttribute("line", "Thin", lineNames[maxDistEndB]), + distantEnds[maxDistEndA], distantEnds[maxDistEndB], 0, 0, 0, scaleFactor); + ); + labelImage := image( + if (textLabels): + draw byLabelsOnPolygon(scantokens(pointNames[maxAngleEndA]),pointName,scantokens(pointNames[maxAngleEndB]))(2, 0); + fi; + ); + else: + linesImage := image( + draw byLineRender(pointName, distantEnds[safeLine], + getAttribute("line", "Color", lineNames[safeLine]), + getAttribute("line", "Dashed", lineNames[safeLine]), + getAttribute("line", "Thin", lineNames[safeLine]), + pointName, distantEnds[safeLine], 0, 0, 0, scaleFactor); + ); + labelImage := image( + if (textLabels): + draw byLabelLineEnd (pointName, distantEnds[safeLine])(0); + fi; + ); + fi; +else: + labelImage := image(); + linesImage := image(); +fi; + image( + draw labelImage; + % this thing is intended to make the overall picture symmetrical + % when all the lines are horizontal. + draw labelImage rotatedabout(pointName scaled scaleFactor, 180) withcolor white; + draw linesImage; + ) +enddef; + +% +% Arcs and circles +% + +vardef byCirclePathGenerate (expr sr, pv) = + save circPath, xp, xang, yang; + path circPath; + color pvt; + numeric xang, yang; + pair xp; + circPath := fullcircle; + xp := (bluepart(pv), greenpart(pv)); + xang := angle(xp); + pvt := (redpart(pv), ypart(xp rotated -xang), xpart(xp rotated -xang)); + yang := angle((bluepart(pvt), redpart(pvt))); + circPath := spaceRotated(circPath)((-xang, yang, 0)); % plane vector to angles conversion should eventually go somewhere + circPath := spaceRotated(circPath)(sr); + circPath := spaceRotated(circPath)(projectionAngle); + circPath +enddef; + +vardef byArcRender (expr o, b, e, r, sr, pv, col, dp, th, s, et) = + save p, q, m, t, currentLineWidth, srCircle; + path p[], srCircle; + picture q; + pair t[]; + numeric m[]; + numeric currentLineWidth; + save thirdDimension; + image( + srCircle := byCirclePathGenerate(sr, pv); + currentLineWidth := lineThickness(th); + p0 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor + lineWidth*s + lineWidth)) shifted (o*scaleFactor); + p1 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor)) shifted (o*scaleFactor); + p2 := (srCircle scaled (r*scaleFactor + lineWidth*s + lineWidth)) shifted (o*scaleFactor); + p3 := ((subpath (b, e) of srCircle) scaled (r*scaleFactor + lineWidth*s)) shifted (o*scaleFactor); + p4 := ((subpath (b + sign(b-e)*1/16, b) of srCircle) scaled (r*scaleFactor + lineWidth*s)) shifted (o*scaleFactor); + p5 := ((subpath (e, e + sign(e-b)*1/16) of srCircle) scaled (r*scaleFactor + lineWidth*s)) shifted (o*scaleFactor); + q := image( + draw p3 withpen pencircle scaled currentLineWidth withcolor col if (dp > 0): dashed byDashPattern(arclength(p3), dp) fi; + draw p4 withpen pencircle scaled currentLineWidth withcolor col; + draw p5 withpen pencircle scaled currentLineWidth withcolor col; + ); + if mainPictureMode and ghostLines: + save gP; + picture gP; + gP := ghostPicture; + ghostPicture := image( + draw gP; + draw p3 withcolor white withpen pencircle scaled lineWidthHair; + ); + fi; + if (et = 0): + p6 := p0--(o*scaleFactor)--cycle; + else: + t0 := point 0 of p1; + t1 := point 0 of reverse(p1); + t2 := 1/2[t0, t1]; + m0 := xpart (p2 intersectiontimes (t2 -- 2[t2, t0])); + m1 := xpart (p2 intersectiontimes (t2 -- 2[t2, t1])); + for i := -(8*3) step 8 until (8*3): + if (abs(b - m0) > abs(b - (m0 + i))): + m0 := m0 + i; + fi; + if (abs(e - m1) > abs(e - (m1 + i))): + m1 := m1 + i; + fi; + endfor; + p6 := (subpath(m0, m1) of p2) -- cycle; + fi; + clip q to p6; + draw q; + ) rotated globalRotation +enddef; + +vardef byArcDefineBE@#(suffix o)(expr b, e, r, col, dp, th, s, et) = +if str @# = "": + byArcDefineBE.scantokens(str o)(b, e, r, col, dp, th, s, et); +else: + setAttribute("arc", "Color", str @#, col); + setAttribute("arc", "Dashed", str @#, dp); + setAttribute("arc", "Thin", str @#, th); + setAttribute("arc", "Shift", str @#, s); + setAttribute("arc", "Diameter", str @#, 2r); + setAttribute("arc", "Center", str @#, o); + setAttribute("arc", "CenterName", str @#, str o); + setAttribute("arc", "BName", str @#, ""); + setAttribute("arc", "EName", str @#, ""); + setAttribute("arc", "Begin", str @#, b); + setAttribute("arc", "End", str @#, e); + setAttribute("arc", "EndType", str @#, et); + setAttribute("arc", "Angle", str @#, angle((point b of fullcircle) - (point e of fullcircle))); + setAttribute("arc", "SpaceRotation", str @#, (0,0,0)); + setAttribute("arc", "PlaneVector", str @#, (0,0,1)); + setAttribute("arc", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byArcBE@#(suffix o)(expr b, e, r, col, dp, th, s, et) = +if str @# = "": + byArcBE.scantokens(str o)(o)(b, e, r, col, dp, th, s, et) +else: + byArcDefineBE.@#(o, b, e, r, col, dp, th, s, et); + byNamedArcExact(@#) +fi +enddef; + +vardef byArcDefine@#(suffix o, pb, pe)(expr r, col, dp, th, s, et) = +if str @# = "": + byArcDefine.scantokens(str pb & str o & str pe)(o, pb, pe)(r, col, dp, th, s, et); +else: + save b, e; + numeric b, e; + b := xpart(((fullcircle scaled 2r) shifted o) intersectiontimes (o -- 2[o, pb])); + e := xpart(((fullcircle scaled 2r) shifted o) intersectiontimes (o -- 2[o, pe])); + if (b > e): + b := b - 8; + fi; + byArcDefineBE.@#(o, b, e, r, col, dp, th, s, et); + setAttribute("arc", "BName", str @#, str pb); + setAttribute("arc", "EName", str @#, str pe); +fi; +enddef; + +vardef byArc@#(suffix o, pb, pe)(expr r, col, dp, th, s, et) = +if str @# = "": + byArc.scantokens(str pb & str o & str pe)(o, pb, pe)(r, col, dp, th, s, et) +else: + byArcDefine.@#(o, pb, pe, r, col, dp, th, s, et); + byNamedArcExact(@#) +fi +enddef; + +vardef byNamedArcExact (text arcslist) = + image( + forsuffixes aN=arcslist: + draw byArcRender( + scantokens(getAttribute("arc", "CenterName", str aN)), + getAttribute("arc", "Begin", str aN), + getAttribute("arc", "End", str aN), + getAttribute("arc", "Diameter", str aN), + getAttribute("arc", "SpaceRotation", str aN), + getAttribute("arc", "PlaneVector", str aN), + getAttribute("arc", "Color", str aN), + getAttribute("arc", "Dashed", str aN), + getAttribute("arc", "Thin", str aN), + getAttribute("arc", "Shift", str aN), + getAttribute("arc", "EndType", str aN)); + endfor; + if (autoLabelingMode): + draw byNamedArcLabel(arcslist); + fi; + ) +enddef; + +vardef byNamedArc (text arcslist) = + image( + forsuffixes aN=arcslist: + draw byArcRender( + scantokens(getAttribute("arc", "CenterName", str aN)), + getAttribute("arc", "Begin", str aN), + getAttribute("arc", "End", str aN), + getAttribute("arc", "Diameter", str aN), + getAttribute("arc", "SpaceRotation", str aN), + getAttribute("arc", "PlaneVector", str aN), + getAttribute("arc", "Color", str aN), + getAttribute("arc", "Dashed", str aN), + getAttribute("arc", "Thin", str aN), 0, 0); + endfor; + if (autoLabelingMode): + draw byNamedArcLabel(arcslist); + fi; + ) +enddef; + +vardef byNamedArcLabel (text arcslist) = + save pointsListB, pointsListE, pointsListO, pointsListD, labelPos, labelVec, labelAng, i, j, k, allArcsDefined, beginFree, endFree, pointsListCrv; + string pointsListB[], pointsListE[], pointsListO[]; + numeric i, j, k, pointsListD[], labelAng; + boolean allArcsDefined, beginFree, endFree; + color labelVec; + pair labelPos; + path pointsListCrv[]; + image( + if (textLabels): + i := -1; + allArcsDefined := true; + forsuffixes aN=arcslist: + i := i + 1; + pointsListB[i] := getAttribute("arc", "BName", str aN); + pointsListE[i] := getAttribute("arc", "EName", str aN); + pointsListO[i] := getAttribute("arc", "CenterName", str aN); + pointsListR[i] := getAttribute("arc", "Diameter", str aN); + begingroup + save thirdDimension; + pointsListCrv[i] := subpath + (getAttribute("arc", "Begin", str aN), getAttribute("arc", "End", str aN)) + of byCirclePathGenerate (getAttribute("arc", "SpaceRotation", str aN), getAttribute("arc", "PlaneVector", str aN)); + endgroup; + if (getAttribute("arc", "BName", str aN) = "") + or (getAttribute("arc", "EName", str aN) = ""): + allArcsDefined := false; + fi; + endfor; + if allArcsDefined: + for j := 0 step 1 until i: + beginFree := true; + endFree := true; + for k := 0 step 1 until i: + if (j <> k): + if (pointsListB[j] = pointsListE[k]): + beginFree := false; + fi; + if (pointsListE[j] = pointsListB[k]): + endFree := false; + fi; + fi; + endfor; + if beginFree: + labelAng := angle(direction 0 of pointsListCrv[j]) + 180; + labelPos := ((point 0 of pointsListCrv[j]) * pointsListR[j]) + scantokens(pointsListO[j]); + draw byTextLabel(pointLabel)( + pointsListB[j], + labelPos, + labelAng, + textLabelShift); + fi; + if endFree: + labelAng := angle(direction 0 of reverse(pointsListCrv[j])) + 180; + labelPos := ((point 0 of reverse(pointsListCrv[j])) * pointsListR[j]) + scantokens(pointsListO[j]); + draw byTextLabel(pointLabel)( + pointsListE[j], + labelPos, + labelAng, + textLabelShift); + fi; + endfor; + fi; + fi; + ) +enddef; + +vardef byCircleDefineFree@#(expr o, r, col, dp, th, s) = +if str @# = "": + byCircleDefineFree.aCircle(o, r, col, dp, th, s); +else: + setAttribute("circle", "Color", str @#, col); + setAttribute("circle", "Dashed", str @#, dp); + setAttribute("circle", "Thin", str @#, th); + setAttribute("circle", "Shift", str @#, s); + setAttribute("circle", "Diameter", str @#, 2r); + setAttribute("circle", "Center", str @#, o); + setAttribute("circle", "CenterName", str @#, ""); + setAttribute("circle", "AName", str @#, ""); + setAttribute("circle", "BName", str @#, ""); + setAttribute("circle", "CName", str @#, ""); + setAttribute("circle", "SpaceRotation", str @#, (0,0,0)); + setAttribute("circle", "PlaneVector", str @#, (0,0,1)); + setAttribute("circle", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byCircleDefineR@#(suffix o)(expr r, col, dp, th, s) = +if str @# = "": + byCircleDefineR.scantokens(str o)(o)(r, col, dp, th, s) +else: + byCircleDefineFree.@#(o, r, col, dp, th, s); + setAttribute("circle", "CenterName", str @#, str o); +fi; +enddef; + +vardef byCircleR@#(suffix o)(expr r, col, dp, th, s) = +if str @# = "": + byCircleR.scantokens(str o)(o)(r, col, dp, th, s) +else: + byCircleDefineR.@#(o, r, col, dp, th, s); + byNamedCircle(@#) +fi +enddef; + +vardef byCircleDefine@#(suffix o, a)(expr col, dp, th, s) = +if str @# = "": + byCircleDefine.scantokens(str o & str a)(o, a)(col, dp, th, s) +else: + byCircleDefineR.@#(o, abs(a-o), col, dp, th, s); + setAttribute("circle", "AName", str @#, str a); +fi; +enddef; + +vardef byCircle@#(suffix o, a)(expr col, dp, th, s) = +if str @# = "": + byCircle.scantokens(str o & str a)(o, a)(col, dp, th, s) +else: + byCircleDefine.@#(o, a, col, dp, th, s); + byNamedCircle(@#) +fi +enddef; + +vardef byCircleABC@#(suffix a, b, c)(expr col, dp, th, s) = +if str @# = "": + byCircleABC.scantokens(str a & str b & str c)(a, b, c)(col, dp, th, s) +else: + save o, r; + pair o; + numeric r; + o := byFindCircleCenterABC(a, b, c); + if byIsPointInSpace(a, b, c): + r := absXYZ(pointXYZ.a - byFindCircleCenterABCinSpace(pointXYZ.a, pointXYZ.b, pointXYZ.c)); + else: + r := abs(o - a); + fi; + byCircleDefineFree.@#(o, r, col, dp, th, s); + setAttribute("circle", "AName", str @#, str a); + setAttribute("circle", "BName", str @#, str b); + setAttribute("circle", "CName", str @#, str c); + if byIsPointInSpace(a, b, c): + save pv; + color pv; + pv := unitvectorXYZ((pointXYZ.a - pointXYZ.b) crossproduct (pointXYZ.c - pointXYZ.b)); + setAttribute("circle", "PlaneVector", str @#, pv); + fi; + byNamedCircle(@#) +fi +enddef; + +vardef byFindCircleCenterABC (suffix a, b, c) = + save rv; + pair rv; + if byIsPointInSpace(a, b, c): + save rvInSpace; + color rvInSpace; + rvInSpace := byFindCircleCenterABCinSpace(pointXYZ.a, pointXYZ.b, pointXYZ.c) colorrotated projectionAngle; + rv := (redpart(rvInSpace), greenpart(rvInSpace)); + else: + rv = whatever[1/2[a, b], 1/2[a, b] + ((a - b) rotated 90)] + = whatever[1/2[b, c], 1/2[b, c] + ((b - c) rotated 90)]; + fi; + rv +enddef; + +vardef byFindCircleCenterABCinSpace (expr a, b, c) = + save vec; + color vec[]; + vec0 := ((a - b) crossproduct (c - b)); + vec1 := byRotateAroundAxis (a - b, (0, 0, 0), 90, vec0); + vec2 := byRotateAroundAxis (c - b, (0, 0, 0), 90, vec0); + vec3 = whatever[1/2[a, b], 1/2[a, b] + vec1] + = whatever[1/2[b, c], 1/2[b, c] + vec2]; % Fails from time to time because of some roundoff errors + vec3 +enddef; + +vardef byReturnCircleCenter (expr circleName) = + if (getAttribute("circle", "CenterName", circleName) <> ""): + scantokens(getAttribute("circle", "CenterName", circleName)) + elseif (getAttribute("circle", "BName", circleName) <> ""): + byFindCircleCenterABC( + scantokens(getAttribute("circle", "AName", circleName)), + scantokens(getAttribute("circle", "BName", circleName)), + scantokens(getAttribute("circle", "CName", circleName)) + ) + else: + getAttribute("circle", "Center", circleName) + fi +enddef; + +vardef byNamedCircle (text circlesList) = + save circlecenter; + pair circlecenter; + image( + forsuffixes cN=circlesList: + circlecenter := byReturnCircleCenter(str cN); + draw byArcRender ( + circlecenter, 0, 8, + getAttribute("circle", "Diameter", str cN), + getAttribute("circle", "SpaceRotation", str cN), + getAttribute("circle", "PlaneVector", str cN), + getAttribute("circle", "Color", str cN), + getAttribute("circle", "Dashed", str cN), + getAttribute("circle", "Thin", str cN), + getAttribute("circle", "Shift", str cN), 0); + if (textLabels and autoLabelingMode): + if (getAttribute("circle", "CenterName", str cN) <> ""): + draw byTextLabel(pointLabel)( + getAttribute("circle", "CenterName", str cN), + scantokens(getAttribute("circle", "CenterName", str cN)), + 0, 0); + fi; + if (getAttribute("circle", "AName", str cN) <> ""): + draw byLabelCircle(getAttribute("circle", "AName", str cN), str cN); + fi; + if (getAttribute("circle", "BName", str cN) <> ""): + draw byLabelCircle(getAttribute("circle", "BName", str cN), str cN); + fi; + if (getAttribute("circle", "CName", str cN) <> ""): + draw byLabelCircle(getAttribute("circle", "CName", str cN), str cN); + fi; + fi; + endfor; + ) +enddef; + +% +% Arbitraty figures +% + +vardef byArbitraryFigureDefine@#(expr p, col, dp, th) = +if str @# = "": + byArbitraryFigureDefine.anArbitraryFigure(p, col, dp, th); +else: + setAttribute("arbitraryFigure", "Path", str @#, p); + setAttribute("arbitraryFigure", "Color", str @#, col); + setAttribute("arbitraryFigure", "Dashed", str @#, dp); + setAttribute("arbitraryFigure", "Thin", str @#, th); + setAttribute("arbitraryFigure", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byArbitraryFigure@#(expr p, col, dp, th) = +if str @# = "": + byArbitraryFigure.anArbitraryFigure(p, col, dp, th) +else: + byArbitraryFigureDefine@#(p, col, dp, th); + byNamedArbitraryFigure(@#) +fi +enddef; + +vardef byNamedArbitraryFigure (text arbitraryFiguresList) = + save p; + path p; + image( + forsuffixes pN=arbitraryFiguresList: + p := getAttribute("arbitraryFigure", "Path", str pN); + draw byArbitraryFigureRender + (p, getAttribute("arbitraryFigure", "Color", str pN), + getAttribute("arbitraryFigure", "Dashed", str pN), + getAttribute("arbitraryFigure", "Thin", str pN)); + endfor; + ) rotated globalRotation +enddef; + +vardef byArbitraryFigureRender (expr p, col, dp, th) = + image( + draw (p scaled scaleFactor) + withpen pencircle scaled lineThickness(th) + withcolor col + if (dp > 0): + dashed byDashPattern(arclength(p), dp) + fi; + ) +enddef; + +vardef byArbitraryCurveDefine@#(text pointsList)(expr col, dp, th) = +if str @# = "": + byArbitraryCurveDefine.anArbitraryCurve(pointsList)(col, dp, th); +else: + save pL, p; + string pL; + path p; + pL := ""; + p := byListToPath(pointsList); + forsuffixes pN=pointsList: + appendList(pL, str pN, 1, false); + endfor; + setAttribute("arbitraryCurve", "PointsList", str @#, pL); + setAttribute("arbitraryCurve", "Path", str @#, p); + setAttribute("arbitraryCurve", "Color", str @#, col); + setAttribute("arbitraryCurve", "Dashed", str @#, dp); + setAttribute("arbitraryCurve", "Thin", str @#, th); + setAttribute("arbitraryCurve", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byNamedArbitraryCurve (text curvesList) = + save p; + path p; + image( + forsuffixes cN=curvesList: + p := byListToPath(scantokens(getAttribute("arbitraryCurve", "PointsList", str cN))); + draw byArbitraryFigureRender + (p, getAttribute("arbitraryCurve", "Color", str cN), + getAttribute("arbitraryCurve", "Dashed", str cN), + getAttribute("arbitraryCurve", "Thin", str cN)) rotated globalRotation; + if (autoLabelingMode): + if (textLabels): + draw byLabelsOnPolygon(scantokens(getAttribute("arbitraryCurve", "PointsList", str cN)), noPoint)(1, 0); + fi; + fi; + endfor; + ) +enddef; + +vardef byListToPath(text pointsList) = + save pathString, rp; + string pathString; + path rp; + pathString := ""; + forsuffixes pN=pointsList: + pathString := pathString & str pN & ".."; + endfor; + rp := scantokens(substring (0, length(pathString) - 2) of pathString); + rp +enddef; + +vardef byArbitraryCurve@#(text pointsList)(expr col, dp, th) = +if str @# = "": + byArbitraryCurve.anArbitraryCurve(pointsList)(col, dp, th) +else: + byArbitraryCurveDefine@#(pointsList)(col, dp, th); + byNamedArbitraryCurve(@#) +fi +enddef; + +% +% Filled figures +% + +vardef byFilledCircleSegment@#(suffix o)(expr r, b, e, col) = +if str @# = "": + byFilledCircleSegment.o(o, r, b, e, col) +else: + setAttribute("filledCircleSegment", "Color", str @#, col); + setAttribute("filledCircleSegment", "Diameter", str @#, 2r); + setAttribute("filledCircleSegment", "Begin", str @#, b); + setAttribute("filledCircleSegment", "End", str @#, e); + setAttribute("filledCircleSegment", "CenterName", str @#, str o); + setAttribute("filledCircleSegment", "Synonym", str @#, str @#); + byNamedFilledCircleSegment(@#) +fi +enddef; + +vardef byNamedFilledCircleSegment (text filledCircleSegmentList) = + save p; + path p; + image( + forsuffixes csN=filledCircleSegmentList: + fill ( + (subpath + (getAttribute("filledCircleSegment", "Begin", str csN), + getAttribute("filledCircleSegment", "End", str csN)) + of fullcircle) + --cycle) + scaled (getAttribute("filledCircleSegment", "Diameter", str csN)*scaleFactor) + shifted (scantokens(getAttribute("filledCircleSegment", "CenterName", str csN))*scaleFactor) + withcolor getAttribute("filledCircleSegment", "Color", str csN); + endfor; + ) rotated globalRotation +enddef; + +vardef byFilledCircleSector@#(suffix o)(expr r, b, e, col) = +if str @# = "": + byFilledCircleSector.o(o, r, b, e, col) +else: + setAttribute("filledCircleSector", "Color", str @#, col); + setAttribute("filledCircleSector", "Diameter", str @#, 2r); + setAttribute("filledCircleSector", "Begin", str @#, b); + setAttribute("filledCircleSector", "End", str @#, e); + setAttribute("filledCircleSector", "CenterName", str @#, str o); + byNamedFilledCircleSector(@#) +fi +enddef; + +vardef byNamedFilledCircleSector (text filledCircleSectorList) = + image( + forsuffixes csN=filledCircleSectorList: + fill ((subpath + (getAttribute("filledCircleSector", "Begin", str csN), + getAttribute("filledCircleSector", "End", str csN)) + of fullcircle) -- (0, 0) -- cycle) + scaled (getAttribute("filledCircleSector", "Diameter", str csN)*scaleFactor) + shifted (scantokens(getAttribute("filledCircleSector", "CenterName", str csN))*scaleFactor) + withcolor getAttribute("filledCircleSector", "Color", str csN); + endfor; + ) rotated globalRotation +enddef; + +vardef byPolygonDefine@#(text pointsList)(suffix col) = +if str @# = "": + byPolygonDefine.scantokens(byPolygonName(pointsList))(pointsList)(col); +else: + save pL, pLraw, p, tn; + path p; + string pL, pLraw; + numeric tn; + pL := ""; + pLraw := ""; + p := forsuffixes pN=pointsList: pN -- endfor cycle; + tn := turningnumber(p); + forsuffixes pN=pointsList: + appendList(pL, str pN, -tn, false); + appendList(pLraw, str pN, 1, false); + endfor; + setAttribute("polygon", "Color", str @#, col); + setAttribute("polygon", "ColorName", str @#, str col); + setAttribute("polygon", "Transparency", str @#, 1); + setAttribute("polygon", "PointsList", str @#, pL); + setAttribute("polygon", "PointsListRaw", str @#, pLraw); + setAttribute("polygon", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byPolygonPathFromList (expr pointsList) = + forsuffixes pN=scantokens(pointsList): pN -- endfor cycle +enddef; + +vardef byPolygon@#(text pointsList)(suffix col) = +if str @# = "": + byPolygon.scantokens(byPolygonName(pointsList))(pointsList)(col) +else: + byPolygonDefine.@#(pointsList)(col); + byNamedPolygon(@#) +fi +enddef; + +vardef byPolygonName(text pointsList) = + save polygonName; + string polygonName; + polygonName := ""; + forsuffixes pN=pointsList: + polygonName := polygonName & str pN; + endfor; + polygonName +enddef; + +vardef byNamedPolygon (text polygonsList) = + save polyPath, polyCol, polyTransp, polyGhost, shadingFunctionExists; + path polyPath; + numeric polyTransp; + picture polyGhost; + boolean shadingFunctionExists; + image( + draw image( + forsuffixes pN=polygonsList: + polyPath := byPolygonPathFromList(getAttribute("polygon", "PointsList", str pN)) scaled scaleFactor; + defineColor.polyCol(getAttribute("polygon", "Color", str pN)); + polyTransp := getAttribute("polygon", "Transparency", str pN); + if known shadingFor.scantokens(getAttribute("polygon", "ColorName", str pN)): + shadingFunctionExists := true; + else: + shadingFunctionExists := false; + fi; + if (not isWhite(polyCol)): + if not shadingFunctionExists: + fill polyPath + withcolor + if polyTransp = 1: + polyCol + else: + transparent(1, polyTransp, polyCol) + fi; + else: + shadingFunction.scantokens(getAttribute("polygon", "ColorName", str pN)) + (polyPath, polyCol)(scantokens(getAttribute("polygon", "PointsListRaw", str pN))); + fi; + fi; + polyGhost := image( + if not shadingFunctionExists: + if (not isLight(polyCol)): + draw ghostPicture; + fi; + draw polyPath + withcolor polyCol + withpen pencircle scaled 2lineWidth; + fi; + ); + clip polyGhost to polyPath; + draw polyGhost; + if (isLight(polyCol)) and not shadingFunctionExists: + draw (byPolygonPathFromList(getAttribute("polygon", "PointsList", str pN)) scaled scaleFactor) + withcolor selectOutlineColor.scantokens(getAttribute("polygon", "ColorName", str pN)) + withpen pencircle scaled lineWidthThin; + fi; + if mainPictureMode and ghostLines: + save gP; + picture gP; + gP := ghostPicture; + ghostPicture := image( + draw gP; + draw polyPath withcolor white withpen pencircle scaled lineWidthHair; + ); + fi; + endfor; + ) rotated globalRotation; + if (autoLabelingMode): + draw byLabelPolygon(-1)(polygonsList); + fi; + ) +enddef; + +vardef byMergePolygons (text polygonsList) = + save pointsList, preList, postList, mergedList, points, postPoints, i, j, k, l, m, n, o, iteration, beforeJoin, polygonChecked, pointChecked; + string pointsList[], preList[], postList[], mergedList, points[], postPoints[]; + numeric l, m, n, o, iteration; + boolean beforeJoin, polygonChecked[], pointChecked[]; + n := -1; + forsuffixes pN=polygonsList: + n := n + 1; + pointsList[n] := getAttribute("polygon", "PointsList", str pN); + polygonChecked[n] := false; + endfor; + if (n > 0): + iteration := -1; + m := -1; + forever: + preList0 := ""; + postList0 := ""; + beforeJoin := true; + iteration := iteration + 1; + l := -1; + forsuffixes i = scantokens(pointsList0): + l := l + 1; + if (l > m): + pointChecked[l] := false; + fi; + if (beforeJoin): + preList0 := preList0 & ", " & str i; + if (not pointChecked[l]): + for j := 1 step 1 until n: + if (beforeJoin and not polygonChecked[j]): + preList1 := ""; + postList1 := ""; + forsuffixes k=scantokens(pointsList[j]): + if (beforeJoin): + preList1 := preList1 & ", " & str k; + if (k = i): + beforeJoin := false; + polygonChecked[j] := true; + pointChecked[l] := true; + o := l; + fi; + else: + postList1 := postList1 & ", " & str k; + fi; + endfor; + fi; + endfor; + fi; + else: + postList0 := postList0 & ", " & str i; + fi; + endfor; + if (not beforeJoin): + mergedList := preList0 & postList1 & preList1 & postList0; + mergedList := substring (2, length(mergedList)) of mergedList; + pointsList0 := polygonCleanup(scantokens(mergedList)); + if (length(pointsList0) < length(mergedList)): + pointChecked[o] := false; + mergedList := pointsList0; + fi; + m := l; + fi; + exitif(beforeJoin or (iteration > 100)); + endfor; + else: + mergedList := pointsList0; + fi; + mergedList +enddef; + +vardef polygonCleanup (text pointsList) = + save cleanedList, points, m, n, iteration; + string cleanedList, points[]; + numeric m, n, iteration; + n := -1; + forsuffixes pL = pointsList: + n := n + 1; + points[n] := str pL; + endfor; + iteration := -1; + forever: + iteration := iteration + 1; + m := 0; + for i := 0 step 1 until n: + if (points[cycleval(i-2, n + 1)] = points[i]): + m := m + 2; + else: + points[cycleval(i-m, n + 1)] := points[i]; + fi; + endfor; + n := n - m; + exitif((m = 0) or (iteration > 100)); + endfor; + for i := 0 step 1 until n: + appendList(cleanedList, points[i], 1, false); + endfor; + cleanedList +enddef; + +% +% Angles +% + +vardef byAngleDefine@#(suffix a, b, c)(expr col, sty) = +if str @# = "": + byAngleDefine.scantokens(str a & str b & str c)(a, b, c)(col, sty); +else: + if mainPictureMode: + appendList(pointAnglesList.b, str @#, 1, true); + appendList(anglePointsList, str b, 1, true); + fi; + save p, q, d, ad, ac, at; + path p; + picture q; + numeric ad, at; + boolean ac; + if autoRightAngles: + if isRightAngle(a, b, c): + at := 1; + else: + at := 0; + fi; + else: + at := 0; + fi; + p := byConstructAngleArc(a, b, c, at); + ad := angle(point (arctime arclength(p)/2 of p) of p); + if (ypart(unitvector(a-b) rotated -ad)) > 0: + ac := true; + else: + ac := false; + fi; + setAttribute("angle", "Arc", str @#, p); + setAttribute("angle", "ArcClockwise", str @#, ac); + setAttribute("angle", "ArcType", str @#, at); + setAttribute("angle", "Val", str @#, 180*arclength(p scaled 2)/pi); + setAttribute("angle", "AVal", str @#, angle(a-b)); + setAttribute("angle", "CVal", str @#, angle(c-b)); + setAttribute("angle", "Direction", str @#, ad); + setAttribute("angle", "Color", str @#, col); + setAttribute0("angle", "OptionalColor", str @#, byDefaultAngleOptionalColor); + setAttribute("angle", "Style", str @#, sty); + setAttribute("angle", "AName", str @#, str a); + setAttribute("angle", "BName", str @#, str b); + setAttribute("angle", "CName", str @#, str c); + setAttribute("angle", "ScaleCorrection", str @#, 1); + setAttribute("angle", "Standalone", str @#, 0); + setAttribute("angle", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byConsiderAngleRight(suffix angleName) = + setAttribute("angle", "ArcType", str angleName, 1); +enddef; + +vardef byConstructAngleArc (suffix a, b, c)(expr s) = + save p, d, cutTime, q; + pair d; + path p[]; + color q[]; + numeric cutTime[]; + d := 1/2[unitvector(a-b),unitvector(c-b)]; + if (abs(d) = 0): + d := (unitvector(a-c) rotated 90); + fi; + if (s = 0): + p0 := fullcircle; + if (attributeExists("point", "XYZ", str a) + and attributeExists("point", "XYZ", str b) + and attributeExists("point", "XYZ", str c)): + p0 := byFullCircleToPlane( + getAttribute("point", "XYZ", str a), + getAttribute("point", "XYZ", str b), + getAttribute("point", "XYZ", str c) + ); + fi; + cutTime1 := xpart((p0 scaled angleSize) intersectiontimes ((0, 0)--(unitvector(a - b) scaled 2angleSize))); + cutTime2 := xpart((p0 scaled angleSize) intersectiontimes ((0, 0)--(unitvector(c - b) scaled 2angleSize))); + p1 := subpath (cutTime1, cutTime2) of p0; + p2 := subpath (cutTime1 - length(p0), cutTime2) of p0; + p3 := subpath (cutTime1 + length(p0), cutTime2) of p0; + if (arclength(p2) > arclength(p3)): + p4 := p3; + else: + p4 := p2; + fi; + if (arclength(p1) < arclength(p4)): + p4 := p1; + fi; + elseif (s = 1): + if (attributeExists("point", "XYZ", str a) + and attributeExists("point", "XYZ", str b) + and attributeExists("point", "XYZ", str c)): + q1 := unitvectorXYZ(pointXYZ.a - pointXYZ.b) colorrotated projectionAngle; + q3 := unitvectorXYZ(pointXYZ.c - pointXYZ.b) colorrotated projectionAngle; + q2 := q1 + q3; + p4 := 1/2(redpart(q1), greenpart(q1)) + -- 1/2(redpart(q2), greenpart(q2)) + -- 1/2(redpart(q3), greenpart(q3)); + else: + p4 := 1/2unitvector(a - b) + -- 1/2(unitvector(a - b) + unitvector(c - b)) + -- 1/2unitvector(c - b); + fi; + fi; + p4 +enddef; + +vardef byIsArcClockwise (expr p) = + save rv; + boolean rv; + if ypart(direction (1/2length(p)) of (p rotated -angle(point 1/2length(p) of p))) < 0: + rv := true; + else: + rv := false; + fi; + rv +enddef; + +vardef byFullCircleToPlane (expr colPlaneA, colPlaneB, colPlaneC) = + save dvec, fvec, p; + color dvec; + pair fvec; + path p; + dvec := unitvectorXYZ((colPlaneA-colPlaneB) crossproduct (colPlaneC-colPlaneB)); + dvec := dvec colorrotated projectionAngle; + fvec := (redpart dvec, greenpart dvec); + if (abs(fvec) > 0): + p := ((fullcircle rotated -angle(fvec)) xscaled sqrt(1-abs(fvec)**2)) rotated angle(fvec); + else: + p := fullcircle; + fi; + p +enddef; + +vardef byAngle@#(suffix a, b, c)(expr col, sty) = +if str @# = "": + byAngle.scantokens(str a & str b & str c)(a, b, c, col, sty) +else: + byAngleDefine@#(a, b, c, col, sty); + byNamedAngle(@#) +fi +enddef; + +vardef byAngleDefineExtended@#(suffix a, b, c)(expr col, sty)(text optionalColors) = +if str @# = "": + byAngleDefineExtended.scantokens(str a & str b & str c)(a, b, c)(col, sty)(optionalColors); +else: + save n; + numeric n; + n := 0; + byAngleDefine.@#(a, b, c, col, sty); + forsuffixes oC=optionalColors: + setAttribute[n]("angle", "OptionalColor", str @#, oC); + n := n + 1; + endfor; +fi; +enddef; + +vardef byAngleExtended@#(suffix a, b, c)(expr col, sty)(text optionalColors) = +if str @# = "": + byAngleExtended.scantokens(str a & str b & str c)(a, b, c)(col, sty)(optionalColors) +else: + byAngleDefineExtended@#(a, b, c)(col, sty)(optionalColors); + byNamedAngle(@#) +fi +enddef; + +vardef byNamedAngle (text anglesList) = + save p, q, processedAL; + picture q; + path p; + string processedAL; + image( + processedAL := byProcessAnglesList(anglesList); + forsuffixes aN=scantokens(processedAL): + setAttribute("angle", "ScaleCorrection", str aN, angleScale); + p := byConstructAngleArc( + scantokens(getAttribute("angle", "AName", str aN)), + scantokens(getAttribute("angle", "BName", str aN)), + scantokens(getAttribute("angle", "CName", str aN)), + getAttribute("angle", "ArcType", str aN)); + q := image( + if (getAttribute("angle", "Color", str aN) <> white): + if (typeOf(getAttribute("angle", "Style", str aN)) = "numeric"): + draw scantokens(byAngleMacroName[getAttribute("angle", "Style", str aN)]) (p, getAttribute("angle", "Color", str aN), angleOptionalColor.scantokens(angleSynonym.aN)); + elseif (typeOf(getAttribute("angle", "Style", str aN)) = "string"): + draw scantokens("byAngleM" & getAttribute("angle", "Style", str aN)) (p, getAttribute("angle", "Color", str aN), angleOptionalColor.scantokens(angleSynonym.aN)); + fi; + else: + draw byAngleMWhite(p, 0, 0); + fi; + if solidAngleMode and (textLabels and autoLabelingMode): + draw (p scaled (1/3angleScale*angleSize))--(0,0)--cycle withcolor white; + fill (p scaled (1/3angleScale*angleSize))--(0,0)--cycle withcolor white; + fi; + ); + draw (q shifted (scantokens(getAttribute("angle", "BName", str aN)) scaled scaleFactor)) rotated globalRotation; + endfor; + ) +enddef; + +vardef byNamedSolidAngle (text anglesList) = + save solidAngleMode, processedAL, centerName, rv; + boolean solidAngleMode; + string processedAL, centerName; + processedAL := byProcessAnglesList(anglesList); + solidAngleMode := true; + image( + forsuffixes aN := scantokens(processedAL): + centerName := getAttribute("angle", "BName", str aN); + endfor; + draw byNamedAngle(anglesList); + if (textLabels and autoLabelingMode): + draw byTextLabel(pointLabel)(centerName, scantokens(centerName), 0, 0); + fi; + ) +enddef; + +vardef byProcessAnglesList@#(text anglesListToProcess)= + save processedAnglesList, compoundAngleString, splitAnglePoints; + string processedAnglesList, compoundAngleString, splitAnglePoints; + processedAnglesList := ""; + splitAnglePoints := ""; + forsuffixes aN=anglesListToProcess: + if attributeExists("angle", "Synonym", str aN): + appendList(processedAnglesList, str aN, 1, false); + else: + splitAnglePoints := bySplitStringIntoAngles(str aN); + if (known aN) and (length(splitAnglePoints) = 0): + compoundAngleString := pointAnglesList.aN; + else: + compoundAngleString := byConstructCompoundAngle(scantokens(splitAnglePoints)); + fi; + if compoundAngleString <> "": + appendList(processedAnglesList, compoundAngleString, 1, false); + if str @# <> "": + appendList(@#, splitAnglePoints, 1, false); + fi; + else: + errmessage("Unknown angle: " & str aN); + fi; + fi; + endfor; + processedAnglesList +enddef; + +string byAngleMacroName[]; +byAngleMacroName[-2] := "byAngleMWhite"; +byAngleMacroName[-1] := "byAngleMThinLine"; +byAngleMacroName[0] := "byAngleMSolid"; +byAngleMacroName[1] := "byAngleMLine"; +byAngleMacroName[2] := "byAngleMDashedLine"; + +vardef byAngleMWhite (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p; + path p; + p := angleArc scaled ((angleScale*angleSize) - lineWidthThin); + image( + fill ((0, 0)--p--cycle) withcolor white; + draw p withpen pencircle scaled lineWidthThin withcolor black; + ) +enddef; + +vardef byAngleMSolid (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p; + path p; + p := angleArc scaled (angleScale*angleSize); + image( + fill ((0, 0)--p--cycle) withcolor angleColor; + ) +enddef; + +vardef byAngleMLine (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p, rv; + path p[]; + picture rv; + p0 := angleArc scaled ((angleScale*angleSize) - lineWidth); + p1 := (0, 0)--p0--cycle; + p2 := (0, 0)--(angleArc scaled (angleScale*angleSize))--cycle; + rv := image( + if (angleOptionalColors[0] <> bytransparent): + fill p2 withcolor angleOptionalColors[0]; + fi; + draw p0 withpen pencircle scaled lineWidth withcolor angleColor; + ); + clip rv to p2; + rv +enddef; + +vardef byAngleMThinLine (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p; + path p; + p := angleArc scaled ((angleScale*angleSize) - lineWidthThin); + image( + if (angleOptionalColors[0] <> bytransparent): + fill ((0, 0)--p--cycle) withcolor angleOptionalColors[0]; + fi; + draw p withpen pencircle scaled lineWidthThin withcolor angleColor; + ) +enddef; + +vardef byAngleMDashedLine (expr angleArc, angleColor)(suffix angleOptionalColors) = + save p, q; + path p; + p := angleArc scaled ((angleScale*angleSize) - lineWidth); + image( + if (angleOptionalColors[0] <> bytransparent): + fill (p--(0,0)--cycle) withcolor angleOptionalColors[0]; + fi; + draw p withpen pencircle scaled lineWidth withcolor angleColor dashed byDashPattern(arclength(p), 1); + draw (0,0); % draws nothing. for some reason dashing seems to "leak" without this. + ) +enddef; + +% Following monstrosity draws dummy sides for angles which require them, +% essentially, it takes a list of angles, checks if they all are constructed +% on one point; then it browses through all angles, finds their sides +% and counts them; ones that occur twice it draws as is; ones that occur +% once it shifts inside an angle; and after that it clips everything +% to angle outline. +vardef byNamedAngleSidesFull (text anglesList)(text linesList)= + save centerName, uniqueCenter, angleGenArc, angleDir, angleCount, angleType, angleSide, anglePart, angleNum, angleCheck, + ac, as, p, n, compoundAnglePath, compoundAngleArc, b, e, outputImage, lineCol, lineTh, lineDash, pointName, scaleToUse, + processedAnglesList, processedLinesList, + planePoints, threeDMode, projectedCircle, labelVector, + enteredAnglePointsList, pointNameToUse; + string centerName, pointName[], processedAnglesList, processedLinesList,enteredAnglePointsList, pointNameToUse; + pair ac, p[], labelVector; + boolean uniqueCenter, angleCheck, doNotLabelCenter, threeDMode; + numeric angleDir[], angleCount[], angleType[], angleSide[], angleNum, anglePart[], as[], n[], b, e, lineTh, lineDash, scaleToUse; + path angleGenArc, compoundAnglePath, compoundAngleArc, projectedCircle; + color planePoints[]; + picture outputImage; + centerName := ""; + uniqueCenter := true; + doNotLabelCenter := false; + scaleToUse := -1; + enteredAnglePointsList := ""; + processedAnglesList := byProcessAnglesList.enteredAnglePointsList(anglesList); + forsuffixes lN=linesList: + appendList(processedLinesList, str lN, 1, false); + endfor; + threeDMode := false; + forsuffixes aN=scantokens(processedAnglesList): + ac := scantokens(getAttribute("angle", "BName", str aN)); + if (centerName = ""): + centerName := getAttribute("angle", "BName", str aN); + if (attributeExists("point", "XYZ", getAttribute("angle", "AName", str aN)) + and attributeExists("point", "XYZ", getAttribute("angle", "BName", str aN)) + and attributeExists("point", "XYZ", getAttribute("angle", "CName", str aN))): + threeDMode := true; + planePoints1 := getAttribute("point", "XYZ", getAttribute("angle", "AName", str aN)); + planePoints2 := getAttribute("point", "XYZ", getAttribute("angle", "BName", str aN)); + planePoints3 := getAttribute("point", "XYZ", getAttribute("angle", "CName", str aN)); + projectedCircle := byFullCircleToPlane(planePoints1, planePoints2, planePoints3); + else: + projectedCircle := fullcircle; + fi; + elseif (centerName <> getAttribute("angle", "BName", str aN)): + uniqueCenter := false; + elseif threeDMode: + if (not isInPlane(getAttribute("point", "XYZ", getAttribute("angle", "AName", str aN)), + planePoints1, planePoints2, planePoints3)) + or (not isInPlane(getAttribute("point", "XYZ", getAttribute("angle", "CName", str aN)), + planePoints1, planePoints2, planePoints3)): + errmessage("The angles " & processedAnglesList & " are not coplanar"); + fi; + fi; + if (getAttribute("angle", "Standalone", str aN) > 1): + doNotLabelCenter := true; + fi; + if (getAttribute("angle", "ScaleCorrection", str aN) <> angleScale) + and (getAttribute("angle", "ScaleCorrection", str aN) > scaleToUse): + scaleToUse := getAttribute("angle", "ScaleCorrection", str aN); + fi; + endfor; + if (length(processedLinesList) = 0): + if (known pointLinesList.scantokens(centerName)): + processedLinesList := pointLinesList.scantokens(centerName); + fi; + fi; + if (scaleToUse < 0): + scaleToUse := angleScale; + fi; + outputImage := image( + startTempAngleScale(scaleToUse); + if (uniqueCenter): + angleNum := 0; + forsuffixes aN=scantokens(processedAnglesList): + angleGenArc := byConstructAngleArc( + scantokens(getAttribute("angle", "AName", str aN)), + scantokens(getAttribute("angle", "BName", str aN)), + scantokens(getAttribute("angle", "CName", str aN)), + getAttribute("angle", "ArcType", str aN)); + anglePart1 := angle(point 0 of angleGenArc); + as1 := sign(ypart((direction 0 of angleGenArc) rotated -anglePart1)); + anglePart2 := angle(point 0 of reverse(angleGenArc)); + as2 := sign(ypart((direction 0 of reverse(angleGenArc)) rotated -anglePart2)); + for j=1,2: + angleCheck := true; + for i := 1 step 1 until angleNum: + if ((abs(angleDir[i] - anglePart[j]) mod 360) < 1): + angleCount[i] := angleCount[i] + 1; + angleCheck := false; + if ((getAttribute("angle", "Style", str aN) = 0) + or ((getAttribute0("angle", "OptionalColor", str aN) <> white) + and (getAttribute0("angle", "OptionalColor", str aN) <> bytransparent))) + and (getAttribute("angle", "ArcType", str aN) <> 1): + angleType[i] := 0; + fi; + fi; + endfor; + if angleCheck: + angleNum := angleNum + 1; + angleDir[angleNum] := anglePart[j]; + angleSide[angleNum] := as[j]; + if ((getAttribute("angle", "Style", str aN) <> 0) + and (isWhite(getAttribute0("angle", "OptionalColor", str aN)) + or (getAttribute0("angle", "OptionalColor", str aN) = bytransparent))) + or ((getAttribute("angle", "Style", str aN) = 0) + and isWhite(getAttribute("angle", "Color", str aN))) + or (getAttribute("angle", "ArcType", str aN) = 1): + angleType[angleNum] := 1; + else: + angleType[angleNum] := 0; + fi; + if (j = 1): + if (getAttribute("angle", "Standalone", str aN) > 0): + pointName[angleNum] := ""; + else: + pointName[angleNum] := getAttribute("angle", "AName", str aN); + fi; + elseif (j = 2): + if (getAttribute("angle", "Standalone", str aN) > 0): + pointName[angleNum] := ""; + else: + pointName[angleNum] := getAttribute("angle", "CName", str aN); + fi; + fi; + angleCount[angleNum] := 1; + fi; + endfor; + endfor; + j := 0; + for i := 1 step 1 until angleNum: + if (angleCount[i] = 1): + j := j + 1; + n[j] := i; + fi; + if (angleType[i] <> 0): + p1 := (ac*scaleFactor); + p2 := ((dir(angleDir[i]) scaled (1/2angleSize * angleScale))) shifted p1; + defineColor.lineCol(black); + lineDash := 0; + lineTh := 1; + forsuffixes lN=scantokens(processedLinesList): + if (getAttribute("line", "EndAName", str lN) = centerName): + if (abs(angle( + scantokens(getAttribute("line", "EndBName", str lN)) + - scantokens(getAttribute("line", "EndAName", str lN))) + - angleDir[i]) < 1): + defineColor.lineCol(getAttribute("line", "Color", str lN)); + lineDash := getAttribute("line", "Dashed", str lN); + lineTh := getAttribute("line", "Thin", str lN); + fi; + elseif (getAttribute("line", "EndBName", str lN) = centerName): + if (abs(angle( + scantokens(getAttribute("line", "EndAName", str lN)) + - scantokens(getAttribute("line", "EndBName", str lN))) + - angleDir[i]) < 1): + defineColor.lineCol(getAttribute("line", "Color", str lN)); + lineDash := getAttribute("line", "Dashed", str lN); + lineTh := getAttribute("line", "Thin", str lN); + fi; + fi; + endfor; + if (angleCount[i] > 1): + draw byLineRender (p1, p2, lineCol, lineDash, lineTh, p1, p2, t, 0, 0, 1); + else: + draw byLineRender (p1, p2, lineCol, lineDash, lineTh, p1, p2, 0, 0, -angleSide[i], 1); + fi; + fi; + endfor; + if (j = 2): + if not threeDMode: + b := (angleDir[n[1]]/360)*8; + e := (angleDir[n[2]]/360)*8; + else: + b := xpart(projectedCircle intersectiontimes ((0, 0) -- dir(angleDir[n[1]])*10)); + e := xpart(projectedCircle intersectiontimes ((0, 0) -- dir(angleDir[n[2]])*10)); + fi; + if (b > e): b := b - length(projectedCircle); fi; + if (angleSide[n[1]] > angleSide[n[2]]): + compoundAngleArc := ((subpath (b, e) of projectedCircle) scaled (angleScale*angleSize)) shifted (ac*scaleFactor); + else: + compoundAngleArc := ((subpath (b + 8, e) of projectedCircle) scaled (angleScale*angleSize)) shifted (ac*scaleFactor); + fi; + compoundAnglePath := compoundAngleArc -- (ac*scaleFactor) -- cycle; + elseif (j <> 0): + errmessage("There are gaps in a compound angle"); + fi; + else: + errmessage("The angles are not concentric"); + fi; + stopTempAngleScale; + ); + if (known compoundAnglePath): + clip outputImage to compoundAnglePath; + fi; + image( + startTempAngleScale(scaleToUse); + draw outputImage rotated globalRotation; + if (textLabels and autoLabelingMode) and (j=2) and (known compoundAnglePath): + for i=1,2: + if not threeDMode: + labelVector := dir(angleDir[n[i]]); + else: + labelVector := (projectedCircle intersectionpoint ((0, 0) -- dir(angleDir[n[i]])*10)) scaled 2; + fi; + pointNameToUse := pointName[n[i]]; + if length(enteredAnglePointsList) > 0: + show(enteredAnglePointsList); + forsuffixes j = scantokens(enteredAnglePointsList): + if (str j <> centerName): + if ((abs( + angle(scantokens(pointNameToUse) - scantokens(centerName)) + - angle(j - scantokens(centerName)) + ) mod 360) < 1): + pointNameToUse := str j; + fi; + fi; + endfor; + fi; + draw byTextLabel(pointLabel)(pointNameToUse, + ac + (labelVector*1/2angleSize*angleScale)/scaleFactor, angleDir[n[i]], textLabelShift); + endfor; + if not doNotLabelCenter: + draw byTextLabel(pointLabel)(centerName, ac, angle((point (arctime(1/2arclength(compoundAngleArc)) of compoundAngleArc) of compoundAngleArc) shifted (-ac*scaleFactor)) + 180, textLabelShift); + fi; + fi; + stopTempAngleScale; + ) +enddef; + +vardef byNamedAngleDummySides(text anglesList)= + byNamedAngleSidesFull(anglesList)(noLine) +enddef; + +vardef byNamedAngleSides(text anglesList)(text linesList) = + image( + draw byNamedAngle(anglesList); + draw byNamedAngleSidesFull(anglesList)(linesList); + ) +enddef; + +vardef byNamedAngleWithDummySides(text anglesList)= + image( + draw byNamedAngle(anglesList); + draw byNamedAngleDummySides(anglesList); + ) +enddef; + +vardef byNamedAngleResized (text anglesToUse) = + save modAnglesList, modAnglesListNew, anglesToDraw, anglesAdded, angleIsAdded, useThisAngle, sc, v, numOfAngles, i, j, k; + string modAnglesList, modAnglesListNew, anglesToDraw; + numeric anglesAdded, sc, v, numOfAngles; + boolean angleIsAdded, useThisAngle; + numOfAngles := 0; + forsuffixes k=anglesToUse: + if (length(str k) > 0): + if typeOf(getAttribute("angle", "Synonym", str k)) <> "string": + errmessage("Angle " & str k & " is missing"); + fi; + numOfAngles := numOfAngles + 1; + fi; + endfor; + image( + forsuffixes i=scantokens(anglePointsList): + modAnglesList := pointAnglesList.i; + modAnglesListNew := ""; + forsuffixes j=scantokens(modAnglesList): + if (numOfAngles > 0): + useThisAngle := false; + forsuffixes k=anglesToUse: + if (getAttribute("angle", "Synonym", str j) = getAttribute("angle", "Synonym", str k)): + useThisAngle := true; + fi; + endfor; + else: + useThisAngle := true; + fi; + if (useThisAngle): + if (length(modAnglesListNew) = 0): + modAnglesListNew := str j; + else: + modAnglesListNew := str j & ", " & modAnglesListNew; + fi; + fi; + endfor; + modAnglesList := modAnglesListNew; + if (length(modAnglesList)>0): + forever: + anglesToDraw := ""; + forever: + modAnglesListNew := ""; + anglesAdded := 0; + forsuffixes j=scantokens(modAnglesList): + if (length(anglesToDraw) = 0): + anglesToDraw := str j; + else: + forsuffixes k=scantokens(anglesToDraw): + if ((abs(getAttribute("angle", "AVal", str k) - getAttribute("angle", "AVal", str j)) mod 360) < 1) + or ((abs(getAttribute("angle", "CVal", str k) - getAttribute("angle", "AVal", str j)) mod 360) < 1) + or ((abs(getAttribute("angle", "AVal", str k) - getAttribute("angle", "CVal", str j)) mod 360) < 1) + or ((abs(getAttribute("angle", "CVal", str k) - getAttribute("angle", "CVal", str j)) mod 360) < 1): + appendList(anglesToDraw, str j, 1, true); + anglesAdded := anglesAdded + 1; + fi; + endfor; + fi; + endfor; + forsuffixes j=scantokens(modAnglesList): + angleIsAdded := false; + forsuffixes k=scantokens(anglesToDraw): + if ((str j) = (str k)): + angleIsAdded := true; + fi; + endfor; + if not angleIsAdded: + appendList(modAnglesListNew, str j, 1, false); + fi; + endfor; + modAnglesList := modAnglesListNew; + exitif (anglesAdded = 0) or (length(modAnglesList)=0); + endfor; + v := 0; + forsuffixes k=scantokens(anglesToDraw): + %v := v + angleVal.k; + if (getAttribute("angle", "Val", str k) > v): v := getAttribute("angle", "Val", str k); fi; + endfor; + sc := angleOpticalScale(v); + startTempAngleScale(angleScale*sc); + draw byNamedAngle(scantokens(anglesToDraw)); + stopTempAngleScale; + exitif (length(modAnglesList)=0); + endfor; + fi; + endfor; + ) +enddef; + +vardef generateAngleSynonyms = + if string anglePointsList: + save n, candidateName, originalName; + numeric n; + string candidateName[], originalName; + forsuffixes i=scantokens(anglePointsList): + n := 0; + pointAnglesList.i := sortPointAnglesList(pointAnglesList.i); + forsuffixes j=scantokens(pointAnglesList.i): + if (length(str j) > 0): + n := n + 1; + originalName := str j; + candidateName0 := getAttribute("angle", "BName", originalName); + candidateName1 := getAttribute("angle", "AName", originalName) + & getAttribute("angle", "BName", originalName) + & getAttribute("angle", "CName", originalName); + candidateName2 := getAttribute("angle", "CName", originalName) + & getAttribute("angle", "BName", originalName) + & getAttribute("angle", "AName", originalName); + if not string getAttribute("angle", "Synonym", candidateName1): + setAttribute("angle", "SynonymPartial", candidateName1, false); + setAttribute("angle", "Synonym", candidateName1, originalName); + fi; + if not string getAttribute("angle", "Synonym", candidateName2): + setAttribute("angle", "SynonymPartial", candidateName2, false); + setAttribute("angle", "Synonym", candidateName2, originalName); + fi; + fi; + endfor; + if (n = 1): + if not string getAttribute("angle", "Synonym", candidateName0): + setAttribute("angle", "SynonymPartial", candidateName0, false); + setAttribute("angle", "Synonym", candidateName0, originalName); + fi; + fi; + endfor; + fi; +enddef; + +vardef sortPointAnglesList (expr anglesListToSort) = + save preList, postList, nPre, nPost, isSorted, value, firstValue, lastValue, rv; + string preList, postList, rv; + boolean isSorted; + numeric value, firstValue, lastValue, nPre, nPost; + lastValue := -360; + isSorted := true; + preList := ""; + postList := ""; + nPre := 0; + nPost := 0; + forsuffixes i=scantokens(anglesListToSort): + value := getAttribute("angle", "Direction", str i); + if (unknown firstValue): + firstValue := value; + appendList(postList, str i, 0, true); + fi; + if (value < firstValue): + appendList(preList, str i, 0, true); + nPre := nPre + 1; + else: + appendList(postList, str i, 0, true); + nPost := nPost + 1; + fi; + if (lastValue > value): + isSorted := false; + fi; + lastValue := value; + endfor; + if (isSorted): + rv := anglesListToSort; + else: + if (nPre > 1): + preList := sortPointAnglesList(preList); + fi; + if (nPre > 0): + preList := preList & ", "; + fi; + if (nPost > 1): + postList := sortPointAnglesList(postList); + fi; + rv := preList & postList; + fi; + rv +enddef; + +vardef bySplitStringIntoAngles (expr angleName) = + save pointA, pointB, pointC, cPointA, cPointB, cPointC, n; + string pointA, pointB, pointC, cPointA, cPointB, cPointC, rv; + numeric n; + n := 0; + for i := 1 step 1 until (length(angleName) - 2): + for j := i + 1 step 1 until (length(angleName) - 1): + cPointA := substring (0, i) of angleName; + cPointB := substring (i, j) of angleName; + cPointC := substring (j, length(angleName)) of angleName; + if known scantokens(cPointA): + if known scantokens(cPointB): + if known scantokens(cPointC): + n := n + 1; + pointA := cPointA; + pointB := cPointB; + pointC := cPointC; + fi; + fi; + fi; + endfor; + endfor; + if (known scantokens(angleName)) and (n = 0): + rv := ""; + else: + if n = 0: + errmessage("Can't find points for angle: " & lineName); + fi; + if n > 1: + errmessage("Ambiguous angle name: " & lineName); + fi; + rv := pointA & ", " & pointB & ", " & pointC; + fi; + rv +enddef; + +vardef byConstructCompoundAngle (suffix pointA, pointB, pointC) = + save angleList, isFirst, isLast, cwA, cwC, cwAName, cwCName, dirA, dirC, rv, threeDMode, notInPlane; + string cwAName, cwCName, angleList, rv; + boolean isFirst, isLast, threeDMode, notInPlane; + pair cwA, cwC, dirA, dirC; + angleList := ""; + dirA := pointA - pointB; + dirC := pointC - pointB; + threeDMode := false; + if byIsPointInSpace(pointA, pointB, pointC): + threeDMode := true; + fi; + isFirst := false; + isLast := false; + forsuffixes i=scantokens(pointAnglesList.pointB & ", " & pointAnglesList.pointB): + if byIsArcClockwise(byConstructAngleArc( + scantokens(getAttribute("angle", "AName", str i)), + scantokens(getAttribute("angle", "BName", str i)), + scantokens(getAttribute("angle", "CName", str i)), + getAttribute("angle", "ArcType", str i))): + cwAName := getAttribute("angle", "CName", str i); + cwCName := getAttribute("angle", "AName", str i); + else: + cwAName := getAttribute("angle", "AName", str i); + cwCName := getAttribute("angle", "CName", str i); + fi; + cwA := scantokens(cwAName) - scantokens(getAttribute("angle", "BName", str i)); + cwC := scantokens(cwCName) - scantokens(getAttribute("angle", "BName", str i)); + notInPlane := false; + if threeDMode: + if (not isInPlane(pointXYZ.scantokens(cwAName), + pointXYZ.pointA, + pointXYZ.pointB, + pointXYZ.pointC)) + or (not isInPlane(pointXYZ.scantokens(cwCName), + pointXYZ.pointA, + pointXYZ.pointB, + pointXYZ.pointC)): + notInPlane := true; + fi; + fi; + if not notInPlane: + if ((abs(angle(dirA) - angle(cwA)) mod 360) < 1) and (not isFirst): + isFirst := true; + fi; + if isFirst and not isLast: + appendList(angleList, str i, 1, true) + fi; + if ((abs(angle(dirC) - angle(cwC)) mod 360) < 1) and isFirst: + isLast := true; + fi; + fi; + endfor; + if (not isFirst) or (not isLast): + errmessage("Can't construct angle: " & str pointA & str pointB & str pointC); + fi; + rv := angleList; + rv +enddef; + +vardef twoRightAngles = + save textLabels; + boolean textLabels; + textLabels := false; + image( + draw byNamedAngle(rightAngleWN,rightAngleNE); + draw byNamedAngleDummySides(rightAngleWN,rightAngleNE); + ) +enddef; + +vardef rightAngle = + save textLabels; + boolean textLabels; + textLabels := false; + image( + draw byNamedAngle(rightAngleWN); + draw byNamedAngleDummySides(rightAngleWN); + ) +enddef; + +% Angles we often need +pair pointN, pointE, pointS, pointW, pointO; +pointN := (0, 2angleSize*angleScale); +pointE := (2angleSize*angleScale, 0); +pointS := (0, -2angleSize*angleScale); +pointW := (-2angleSize*angleScale, 0); +pointO := (0, 0); + +vardef byDefineGenericRightAngles(expr angleInsteadOfArc) = + byAngleDefine.rightAngleNE (pointN, pointO, pointE)(black, 1); + byAngleDefine.rightAngleES (pointE, pointO, pointS)(black, 1); + byAngleDefine.rightAngleSW (pointS, pointO, pointW)(black, 1); + byAngleDefine.rightAngleWN (pointW, pointO, pointN)(black, 1); + if (angleInsteadOfArc): + byConsiderAngleRight(rightAngleNE); + byConsiderAngleRight(rightAngleES); + byConsiderAngleRight(rightAngleSW); + byConsiderAngleRight(rightAngleWN); + fi; +enddef; + +byDefineGenericRightAngles(false); + +% +% Arrows +% + +def commonArrowSettings = + save ahlength, ahangle; + ahlength := 7pt; + ahangle := 30; +enddef; + +vardef byRotationArrowDefine@#(suffix axisA, axisB, pointA) (expr ang, col, sty) = +if str @# = "": + byRotationArrowDefine.scantokens(str axisA & str axisB & str pointA)(axisA, axisB, pointA) (ang, col, sty); +else: + setAttribute("rotationarrow", "AxisA", str @#, axisA); + setAttribute("rotationarrow", "AxisB", str @#, axisB); + setAttribute("rotationarrow", "Point", str @#, pointA); + setAttribute("rotationarrow", "AxisAName", str @#, str axisA); + setAttribute("rotationarrow", "AxisBName", str @#, str axisB); + setAttribute("rotationarrow", "PointName", str @#, str pointA); + setAttribute("rotationarrow", "Angle", str @#, ang); + setAttribute("rotationarrow", "Color", str @#, col); + setAttribute("rotationarrow", "Style", str @#, sty); + setAttribute("rotationarrow", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byGenerateRotationArrowPath (expr axisA, axisB, pointA, ang) = + save arrowPath, spacePoint; + path arrowPath; + color spacePoint; + spacePoint := pointA colorrotated projectionAngle; + arrowPath := (redpart(spacePoint), greenpart(spacePoint)); + for i := 0 step (ang/floor(ang/5)) until ang: + spacePoint := byRotateAroundAxis (axisA, axisB, i, pointA) colorrotated projectionAngle; + arrowPath := arrowPath -- (redpart(spacePoint), greenpart(spacePoint)); + endfor; + arrowPath +enddef; + +vardef byNamedRotationArrow (text arrowsList) = + save arrowPath, arrowPoint, arrowStyle; + path arrowPath; + color arrowPoint; + numeric arrowStyle; + commonArrowSettings; + image( + forsuffixes aN=arrowsList: + arrowPath := byGenerateRotationArrowPath( + pointXYZ.scantokens(getAttribute("rotationarrow", "AxisAName", str aN)), + pointXYZ.scantokens(getAttribute("rotationarrow", "AxisBName", str aN)), + pointXYZ.scantokens(getAttribute("rotationarrow", "PointName", str aN)), + getAttribute("rotationarrow", "Angle", str aN) + ) scaled scaleFactor; + arrowStyle := getAttribute("rotationarrow", "Style", str aN); + if arrowStyle = 0: + arrowPath := subpath (0, (arctime (arclength(arrowPath) - ahlength) of arrowPath)) of arrowPath; + drawarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("rotationarrow", "Color", str aN); + else: + arrowPath := subpath ((arctime ahlength of arrowPath), (arctime (arclength(arrowPath) - ahlength) of arrowPath)) of arrowPath; + drawdblarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("rotationarrow", "Color", str aN); + fi; + endfor; + ) rotated globalRotation +enddef; + +vardef byFlatArrowDefine@#(suffix pointA, pointB) (expr col, sty, typ) = +if str @# = "": + byFlatArrowDefine.scantokens(str pointA & str pointB)(pointA, pointB) (col, sty, typ); +else: + setAttribute("flatarrow", "PointA", str @#, pointA); + setAttribute("flatarrow", "PointB", str @#, pointB); + setAttribute("flatarrow", "PointAName", str @#, str pointA); + setAttribute("flatarrow", "PointBName", str @#, str pointB); + setAttribute("flatarrow", "Type", str @#, typ); + setAttribute("flatarrow", "Color", str @#, col); + setAttribute("flatarrow", "Style", str @#, sty); + setAttribute("flatarrow", "Synonym", str @#, str @#); +fi; +enddef; + +vardef byNamedFlatArrow (text arrowsList) = + save arrowPath, arrowStyle, arrowPoint; + path arrowPath; + numeric arrowStyle; + pair arrowPoint[]; + commonArrowSettings; + image( + forsuffixes aN=arrowsList: + arrowType := getAttribute("flatarrow", "Type", str aN); + arrowStyle := getAttribute("flatarrow", "Style", str aN); + arrowPoint1 := scantokens(getAttribute("flatarrow", "PointAName", str aN)); + arrowPoint2 := scantokens(getAttribute("flatarrow", "PointBName", str aN)); + if arrowType = 0: + arrowPath := arrowPoint1 -- arrowPoint2; + elseif arrowType = 1: + arrowPath := arrowPoint1 + .. 1/2[arrowPoint1, arrowPoint2] + shifted (((arrowPoint2 - arrowPoint1) scaled 1/8) rotated 90) + .. arrowPoint2; + fi; + arrowPath := arrowPath scaled scaleFactor; + if arrowStyle = 0: + arrowPath := subpath ( + 0, + arctime (arclength(arrowPath) - ahlength) of arrowPath + ) of arrowPath; + drawarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("flatarrow", "Color", str aN); + else: + arrowPath := subpath ( + arctime ahlength of arrowPath, + arctime (arclength(arrowPath) - ahlength) of arrowPath + ) of arrowPath; + drawdblarrow arrowPath withpen (pencircle scaled lineWidthThin) withcolor getAttribute("flatarrow", "Color", str aN); + fi; + endfor; + ) rotated globalRotation +enddef; + +% +% Spheres +% + +%vardef bySphereDefine@#(expr ox, oy, oz, r) = +%if str @# = "": +% errmessage("bySphereDefine needs a name (bySphereDefine.somename...)"); +%else: +% if mainPictureMode: +% appendList(allSpheresList, str @#, 1, true); +% fi; +% setAttribute("sphere", "XYZ", str @#, (ox, oy, oz)); +% setAttribute("sphere", "radius", str @#, r); +%fi; +%enddef; +% +%vardef byNamedSphere (test spheresList) = +% image( +% forsuffixes cN=spheresList: +% endfor; +% ) +%enddef; +% +%vardef byFindSpheresIntersection (text spheresList) = +%enddef; + +% +% Magnitudes +% + +% along with 'magnitudesymbol' one symbol long 'magnitude' is also defined at the same time. +vardef byMagnitudeSymbolDefine@#(expr shp, col, sty) = +if str @# = "": + byMagnitudeSymbolDefine.scantokens(shp)(shp, col, sty); +else: + setAttribute("magnitude", "Color", str @#, col); + setAttribute("magnitude", "Shape", str @#, shp); + setAttribute("magnitude", "Style", str @#, sty); + setAttribute1("magnitude", "Symbol", str @#, str @#); + setAttribute1("magnitude", "N", str @#, 1); + setAttribute("magnitude", "Alignment", str @#, 0); + setAttribute("magnitude", "NumberOfRows", str @#, 1); + setAttribute("magnitude", "Horizontal", str @#, false); +fi; +enddef; + +vardef byNamedMagnitudeSymbol (expr n, hor)(suffix magnitudeSymbolName) = + save p, q, i, s, magnitudeSizeCor; + path p; + pair s[]; + picture q; + numeric magnitudeSizeCor; + p := (0, 0); + magnitudeSizeCor := 1; + if (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "circle"): + p := fullcircle scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "semicircleUp"): + p := ((subpath (0, 4) of fullcircle) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "semicircleDown"): + p := (((subpath (0, 4) of fullcircle) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) yscaled -1) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "sectorDown"): + p := ((subpath (0, 4) of fullcircle) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "sectorUp"): + p := ((subpath (0, 4) of fullcircle) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize) yscaled -1; + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "wedgeUp"): + p := (((1/2, 0) -- (1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (0, -1/2) -- cycle) yscaled -1) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "wedgeDown"): + p := ((1/2, 0) -- (1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (0, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "square"): + p := ((1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, -1/2) -- (1/2, -1/2) -- cycle) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "halfsquare"): + p := ((1/2, 1/2) -- (-1/2, 1/2) -- (-1/2, 0) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "rhombus"): + p := ((0, 1/2) -- (-1/2, 0) -- (0, -1/2) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "halfrhombusUp"): + p := ((0, 1/2) -- (-1/2, 0) -- (1/2, 0) -- cycle) scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniTriangleUp"): + p := ((-1/4, -1/4) -- (1/4, -1/4) -- (0, 1/4) -- cycle) scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniTriangleDown"): + p := ((-1/4, 1/4) -- (1/4, 1/4) -- (0, -1/4) -- cycle) scaled (magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniSquare"): + p := ((-1/4, -1/4) -- (1/4, -1/4) -- (1/4, 1/4) -- (-1/4, 1/4) -- cycle) scaled (magnitudeScale*magnitudeSize); + elseif (getAttribute("magnitude", "Shape", str magnitudeSymbolName) = "miniCircle"): + p := fullcircle scaled (1/2magnitudeScale*magnitudeSize); + magnitudeSizeCor := 8/7; + fi; + if (hor): + s1 := (0, ypart(urcorner(p)) - ypart(lrcorner(p))); + s2 := (0, magnitudeGap); + else: + s1 := (xpart(urcorner(p)) - xpart(ulcorner(p)), 0); + s2 := (magnitudeGap, 0); + fi; + p := p scaled magnitudeSizeCor; + q := image( + draw p withpen (pencircle scaled lineWidthThin) withcolor getAttribute("magnitude", "Color", str magnitudeSymbolName); + if (getAttribute("magnitude", "Style", str magnitudeSymbolName) = 0): + fill p withcolor getAttribute("magnitude", "Color", str magnitudeSymbolName); + fi; + ); + image( + for i := 1 step 1 until n: + draw q shifted (s1 scaled i) shifted (s2 scaled i); + endfor; + ) +enddef; + +vardef byMagnitudeDefine@#(expr al, hor)(text rowsList)(text magnitudeSymbolsList) = +if str @# = "": + errmessage("byMagnitudeDefine needs a name (byMagnitudeDefine.somename...)"); +else: + setAttribute("magnitude", "Horizontal", str @#, hor); + setAttribute("magnitude", "Alignment", str @#, al); + save i; + numeric i; + i := 0; + forsuffixes rN=rowsList: + i := i + 1; + setAttribute[i]("magnitude", "N", str @#, rN); + endfor; + setAttribute("magnitude", "NumberOfRows", str @#, i); + i := 0; + forever: + forsuffixes sN=magnitudeSymbolsList: + i := i + 1; + if (i <= getAttribute("magnitude", "NumberOfRows", str @#)): + setAttribute[i]("magnitude", "Symbol", str @#, str sN); + fi; + endfor; + exitif (i >= getAttribute("magnitude", "NumberOfRows", str @#)); + endfor; +fi; +enddef; + +vardef byNamedMagnitude (expr excl) (suffix magnitudeName) = + save p, s, h, magC; + picture p; + pair s[]; + numeric h; + h := 0; + image( + for magC := 1 step 1 until getAttribute("magnitude", "NumberOfRows", str magnitudeName): + if (excl = 0) or ((excl < 0) and (magC <> -excl)) or ((excl > 0) and (magC = excl)): + p := byNamedMagnitudeSymbol(getAttribute[magC]("magnitude", "N", str magnitudeName), getAttribute("magnitude", "Horizontal", str magnitudeName))(scantokens(getAttribute[magC]("magnitude", "Symbol", str magnitudeName))); + if (getAttribute("magnitude", "Horizontal", str magnitudeName)): + if (getAttribute("magnitude", "Alignment", str magnitudeName) = 0): + s1 := 1/2[urcorner(p),lrcorner(p)]; + elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = 1): + s1 := lrcorner(p); + elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = -1): + s1 := urcorner(p); + fi; + s2 := (h, 0); + else: + if (getAttribute("magnitude", "Alignment", str magnitudeName) = 0): + s1 := 1/2[ulcorner(p),urcorner(p)]; + elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = 1): + s1 := urcorner(p); + elseif (getAttribute("magnitude", "Alignment", str magnitudeName) = -1): + s1 := ulcorner(p); + fi; + s2 := (0, -h); + fi; + draw p shifted -s1 shifted s2; + if (getAttribute("magnitude", "Horizontal", str magnitudeName)): + h := h + (abs(ulcorner(p)-urcorner(p))) + magnitudeGap; + else: + h := h + (abs(ulcorner(p)-llcorner(p))) + magnitudeGap; + fi; + fi; + endfor; + ) +enddef; + +% +% Text labels +% + +% If using metafun, use textext + +if known metafunversion: + vardef textLabelRender(expr t) = + textext("\textsf{" & t & "}") scaled 2/5 + enddef; +else: + input TEX; + TEXPRE("%&latex" & char(10) & "\documentclass{article}\usepackage[utf8]{inputenc}\usepackage[russian]{babel}\begin{document}"); + TEXPOST("\end{document}"); + vardef textLabelRender(expr t) = + TEX("\textsf{" & t & "}") scaled 2/5 + enddef; +fi; + +numeric textLabelAvSize; +textLabelAvSize := abs(ulcorner(textLabelRender("A")) - llcorner(textLabelRender("A"))); + +vardef byTextLabel(suffix labelType)(expr t, p, a, d) = + save labelItself, labelString, bb; + path bb; + picture labelItself; + string labelString; + if not string labelType.scantokens(t): + string labelType.scantokens(t); + labelType.scantokens(t) := t; + fi; + labelString := str labelType & "." & t; + if (not isInList(labelString, uniqueTextLabels)) or (not omitDuplicateTextLabels): + appendList(uniqueTextLabels, labelString, 1, false); + labelItself := textLabelRender(labelType.scantokens(t)); + labelItself := labelItself shifted -1/2[ulcorner(labelItself), lrcorner(labelItself)]; + if (d > 0): + bb := ulcorner(labelItself) -- urcorner(labelItself) -- lrcorner(labelItself) -- llcorner(labelItself)--cycle; + labelItself := labelItself shifted -(bb intersectionpoint ((0,0)--(dir(a+180+globalRotation)*1cm))); + fi; + labelItself := image( + draw labelItself shifted ((p scaled scaleFactor) rotated globalRotation) shifted (dir(a+globalRotation)*d); + ); + else: + labelItself := image(); + fi; + labelItself +enddef; + +vardef byLabelPoint(suffix p)(expr a, d) = + image( + if (textLabels): + draw byTextLabel(pointLabel)(str p, p, a, textLabelShift*d); + fi; + ) +enddef; + +vardef byLabelLine(expr d)(text linesList) = + save a; + numeric a; + if (d = 0): + a := 90; + else: + a := -90; + fi; + image( + if (textLabels): + forsuffixes lN=linesList: + if (getAttribute("line", "UseLineLabel", str lN)): + draw byTextLabel(pointLabel)(getAttribute("line", "Label", str lN), + 1/2[scantokens(getAttribute("line", "EndAName", str lN)), + scantokens(getAttribute("line", "EndBName", str lN))], + getAttribute("line", "Angle", str lN) + a, textLabelShift); + else: + draw byTextLabel(pointLabel)( + getAttribute("line", "EndAName", str lN), + scantokens(getAttribute("line", "EndAName", str lN)), + getAttribute("line", "Angle", str lN) + a, + textLabelShift); + draw byTextLabel(pointLabel)( + getAttribute("line", "EndBName", str lN), + scantokens(getAttribute("line", "EndBName", str lN)), + getAttribute("line", "Angle", str lN) + a, + textLabelShift); + fi; + endfor; + fi; + ) +enddef; + +vardef byLabelPolygon(expr d)(text polygonsList) = + image( + if (textLabels): + draw byLabelsOnPolygon(scantokens byMergePolygons(polygonsList))(1, d); + fi; + ) +enddef; + +vardef byLabelCircle(expr l, cn) = + save o, d; + pair o; + numeric d; + o := getAttribute("circle", "Center", cn); + d := lineThickness(getAttribute("circle", "Thin", cn))*(1/2-1/2getAttribute("circle", "Shift", cn)); + byTextLabel(pointLabel)(l, scantokens(l), angle(o - scantokens(l)), textLabelShift + d) +enddef; + +vardef byLabelsOnCircle(text pointsList)(suffix cn) = + save d, t, s, c; + numeric t, s; + pair c; + if string getAttribute("circle", "CenterName", str cn): + t := getAttribute("circle", "Thin", str cn); + s := getAttribute("circle", "Shift", str cn); + c := byReturnCircleCenter(str cn); + elseif string getAttribute("arc", "CenterName", str cn): + t := getAttribute("arc", "Thin", str cn); + s := getAttribute("arc", "Shift", str cn); + c := scantokens(getAttribute("arc", "CenterName", str cn)); + else: + errmessage("There is no circle or arc named " & str cn); + fi; + d := lineThickness(t)*(1/2+1/2s); + image( + if (textLabels): + forsuffixes pN=pointsList: + draw byTextLabel(pointLabel)(str pN, pN, angle(pN - c), textLabelShift + d); + endfor; + fi; + ) +enddef; + +vardef byLabelsOnPolygon(text pointsList)(expr sty, shft)= + image( + if(textLabels): + save pointName, pointLoc, i, j, k, l, p, q, stump, sv; + numeric i, j, k, l, stump, sv; + string pointName[]; + pair pointLoc[], p[]; + i := -1; + forsuffixes pN=pointsList: + boolean pointDrawn.pN; + pointDrawn.pN := false; + i := i + 1; + pointName[i] := str pN; + if (pointName[i] <> "noPoint"): + pointLoc[i] := pN; + fi; + if (i > 0): + if (pointName[i] = pointName[i-1]): + i := i - 1; + fi; + fi; + endfor; + if (pointName[i] = pointName[0]): + i := i - 1; + fi; + if (i = 1): + i := i + 1; + pointName[i] := "noPoint"; + fi; + for j := 0 step 1 until i: + k := cycleval(j - 1, i + 1); + l := cycleval(j + 1, i + 1); + if (pointName[j] <> "noPoint"): + p1 := pointLoc[j]; + stump := 0; + if (pointName[k] <> "noPoint") or (pointName[l] <> "noPoint"): + if (pointName[k] = "noPoint"): + p2 := pointLoc[l]; + p0 := 2[p2, p1]; + stump := -1; + elseif (pointName[l] = "noPoint"): + p0 := pointLoc[k]; + p2 := 2[p0, p1]; + stump := +1; + else: + p0 := pointLoc[k]; + p2 := pointLoc[l]; + fi; + fi; + pair q[]; + q4 := unitvector(p1-p0) rotated 90; + q5 := unitvector(p2-p1) rotated 90; + if ((abs(angle(q4) - angle(q5)) mod 180) > 1): + q0 := p0 shifted q4; + q1 := p1 shifted q4; + q2 := p1 shifted q5; + q3 := p2 shifted q5; + q6 = whatever[q0, q1] = whatever[q2, q3]; + else: + if (sty <> 1) or (stump <> 0): + q6 := p1 shifted q4; + else: + q6 := p1; + fi; + fi; + if (sty = 2) and ((j = 0) or (j = i)): + pointDrawn.scantokens(pointName[j]) := true; + fi; + if (sty = 3) and (j = 0): + pointDrawn.scantokens(pointName[j]) := true; + fi; + if (sty = 4) and (j = i): + pointDrawn.scantokens(pointName[j]) := true; + fi; + if (q6 <> p1) and (not pointDrawn.scantokens(pointName[j])): + sv := byLabelAngleCompensate(p0, p1, p2, shft); + draw byTextLabel(pointLabel)(pointName[j], pointLoc[j], angle(q6-p1), textLabelShift+sv); + pointDrawn.scantokens(pointName[j]):= true; + fi; + fi; + endfor; + fi; + ) +enddef; + +vardef byLabelLineEnd (suffix a, b)(expr d) = + image( + if (textLabels): + draw byTextLabel(pointLabel)(str a, a, angle(a-b), textLabelShift+(d*lineWidth)); + fi; + ) +enddef; + +vardef byLabelAngleCompensate (expr a, b, c, s) = + save ang, ins; + numeric ang, ins; + ang := angleValue(a, b, c)/2; + if (sind(ang*2)>0): + ins := (cosd(ang)/(2*sind(ang)))*textLabelAvSize; + else: + ins := 0; + fi; + (((abs(sind(ang)) + abs(cosd(ang)/abs(sind(ang))))*(lineWidth*(1/2+1/2s)))/2) + ins +enddef; diff --git a/Master/texmf-dist/tex/latex/byrne/byrne.sty b/Master/texmf-dist/tex/latex/byrne/byrne.sty new file mode 100644 index 00000000000..c7c9666f295 --- /dev/null +++ b/Master/texmf-dist/tex/latex/byrne/byrne.sty @@ -0,0 +1,298 @@ +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{byrne}[2022/01/06 Byrne] + +\RequirePackage{xparse} +\RequirePackage{ifmtarg} +\RequirePackage{luamplib} % version v2.22.0 is required +\mplibsetformat{metafun} + +\mplibglobaltextext{enable} +\newcount\mpInst +\mpInst = 0 + +\def\mpPre{} +\def\mpPost{} + +\newif\ifCreateNewInstanceForPicture +\CreateNewInstanceForPicturetrue + +\def\undefineList{} + +\directlua{ +function byFormatImageName(s) + local resultingName = s:gsub ("," , "") + tex.print("instanceOffspringPicture"..resultingName) +end +} + +\def\formatImageName#1{\directlua{byFormatImageName("#1")}} + + +\DeclareDocumentCommand{\defineNewPicture}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\sfA{1/3}}% + {\global\def\sfA{#1}}% + \IfNoValueTF{#2}% + {\global\def\sfB{defaultScaleFactor}}% + {\global\def\sfB{#2}}% + \undefineList% + \def\undefineList{}% + \ifCreateNewInstanceForPicture% + \global\advance \mpInst by 1% + \global\def\currentInstance{byInstance\the\mpInst}% + \fi% + \setbox0\vbox{% + \everymplib[\currentInstance]{beginfig(0);}% + \everyendmplib[\currentInstance]{endfig;}% + \begin{mplibcode}[\currentInstance]% + picture instanceMainPicture; + input byrne; + \mpPre + instanceMainPicture := image( + startMainPictureMode; + projectionAngle := (0, 0, 0); + linecap := butt; + scaleFactor := \sfB; + #3 + scaleFactor := \sfA; + generateAngleSynonyms; + generateLineSynonyms; + stopMainPictureMode; + ); + \mpPost + \end{mplibcode}% + }\global\addToUndefineList{#2}% +} + +\def\drawImageFromCurrentInstance#1{ + \begin{mplibcode}[\currentInstance]% + draw #1; + \end{mplibcode}% +} + +% Every time some simple picture is drawn, new macro is defined +% with \markPictAsReady. If macro is defined, predefined picture is used. +% Every time new picture is added, instructions to undefine it are added with +% \addToUndefineList. \undefineList is thus generated command, that, once executed, +% undefines everything that was appended to it with \addToUndefineList. + +\def\unmarkPictAsReady#1{\expandafter\let\csname #1\endcsname\@undefined} + +\def\addToUndefineList#1{\expandafter\def\expandafter\undefineList\expandafter{\undefineList \unmarkPictAsReady{#1}}} + +\def\undefineList{} +\addToUndefineList{lastPicture} + +% When larger picture is defined, we can visualize its parts with \drawFromCurrentPicture +% First (optional) argument is for picture's vertical alignment relative to the string +% Currently only `middle' and anything other (for `top') is working +% Second (optional) argument is for picture's name for future reuse +% If picture with the same name exists, it's not defined again. +% Third argument is MP code for picture itself. +% To actually define an inline picture \defineFromCurrentPicture is called. +% The latter can also be called separately, which can be convenient +% if one wants to define multiple inline pictures in one place. +% Whenever everything is defined, \drawDefinedPicture macro is called for + +\def\defineFromCurrentPicture#1#2#3{% + \begin{mplibcode}[\currentInstance]% + picture \formatImageName{#2}; + \formatImageName{#2} = image( + startOffspringPictureMode; + linecap := butt; + #3 + stopOffspringPictureMode; + ); + \end{mplibcode}% +\global\def\lastPict{#2}% +\global\expandafter\edef\csname\lastPict\endcsname{{\noexpand\drawDefinedPicture{\expandafter\lastPict}{\expandafter#1}}}} + +\DeclareDocumentCommand{\drawFromCurrentPicture}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\currentInlinePicturePlacement{middle}}% + {\global\def\currentInlinePicturePlacement{#1}}% + \IfNoValueTF{#2}% + {\defineFromCurrentPicture{\currentInlinePicturePlacement}{lastPicture}{#3}}% + {\expandafter\ifx\csname #2\endcsname\relax% + \defineFromCurrentPicture{\currentInlinePicturePlacement}{#2}{#3}% + \global\addToUndefineList{#2}% + \else% + \global\def\lastPict{#2}% + \fi}% + \csname\lastPict\endcsname% +} + +% \drawDefinedPicture draws defined and named picture, aligned as needed and, +% possibly, with margins modified with \offsetPicture (i. e., if we want picture to act as if it's less tall that it actually is, +% we put some value as its first argument, if we want picture to be less deep, we use second argument, and place picture +% itself as third). + +\newdimen\pictOffsetTop +\newdimen\pictOffsetBottom + +\pictOffsetTop=0pt +\pictOffsetBottom=0pt + +\def\offsetPicture#1#2#3{{\pictOffsetTop=#1\pictOffsetBottom=#2#3\pictOffsetTop=0pt\pictOffsetBottom=0pt}} + +\newdimen\midht +\newdimen\middp +\def\drawDefinedPicture#1#2{% + \setbox0\vbox{\drawImageFromCurrentInstance{\formatImageName{#1}}}% + \ifdim\ht0<\baselineskip\advance\pictOffsetTop by 1pt\advance\pictOffsetBottom by 1pt\fi% + \setbox0\vbox{\vskip2pt\vskip-\pictOffsetTop\box0\vskip2pt\vskip-\pictOffsetBottom}% + \def\tmpmiddle{middle}% + \def\tmpalignment{#2}% + \ifx\tmpalignment\tmpmiddle% + \middp=0.5\ht0 + \midht=0.5\ht0 + \advance\middp by -3pt + \advance\midht by 3pt + \else% + \middp=0pt + \midht=\ht0 + \advance\middp by 3pt + \advance\midht by -3pt + \fi% + \advance\middp by 0.5\pictOffsetTop + \advance\midht by -0.5\pictOffsetTop + \advance\middp by -0.5\pictOffsetBottom + \advance\midht by 0.5\pictOffsetBottom + \dp0=\middp + \ht0=\midht + \nobreak\hskip0pt\nobreak\,\nobreak\box0\,% +} + +% Some convenient shorthand macros for commonly used derivations of definition picture are defined below + +\def\drawCurrentPicture{\drawImageFromCurrentInstance{instanceMainPicture}} + + +\DeclareDocumentCommand{\drawUnitLine}{o m}{% + \IfNoValueTF{#1}% + {\drawFromCurrentPicture[middle][uline#2]{startAutoLabeling;draw byNamedCompoundLine(1cm, 0)(#2);stopAutoLabeling;}}% + {\drawFromCurrentPicture[middle][uline#2#1]{startAutoLabeling;draw byNamedCompoundLine(#1, 0)(#2);stopAutoLabeling;}}% +} + +\def\drawProportionalLine#1{\drawFromCurrentPicture[middle][pline#1]{startAutoLabeling;draw byNamedCompoundLine(1cm, 1)(#1);stopAutoLabeling;}} + +\DeclareDocumentCommand{\drawSizedLine}{o m}{% + \IfNoValueTF{#1}% + {\drawFromCurrentPicture[middle][sline#2]{startAutoLabeling;draw byNamedCompoundLine(2cm, 2)(#2);stopAutoLabeling;}}% + {\drawFromCurrentPicture[middle][sline#2#1]{startAutoLabeling;draw byNamedCompoundLine(#1, 2)(#2);stopAutoLabeling;}}% +} + +\DeclareDocumentCommand{\drawUnitRay}{o m}{% + \IfNoValueTF{#1}% + {\drawFromCurrentPicture[middle][uray#2]{startAutoLabeling;draw byNamedCompoundRay(1cm, 0)(#2);stopAutoLabeling;}}% + {\drawFromCurrentPicture[middle][uray#2#1]{startAutoLabeling;draw byNamedCompoundRay(#1, 0)(#2);stopAutoLabeling;}}% +} + +\def\drawProportionalRay#1{\drawFromCurrentPicture[middle][pray#1]{startAutoLabeling;draw byNamedCompoundRay(1cm, 1)(#1);stopAutoLabeling;}} + +\DeclareDocumentCommand{\drawSizedRay}{o m}{% + \IfNoValueTF{#1}% + {\drawFromCurrentPicture[middle][sray#2]{startAutoLabeling;draw byNamedCompoundRay(2cm, 2)(#2);stopAutoLabeling;}}% + {\drawFromCurrentPicture[middle][sray#2#1]{startAutoLabeling;draw byNamedCompoundRay(#1, 2)(#2);stopAutoLabeling;}}% +} + +\def\drawProportionalIndLine#1{\drawFromCurrentPicture[middle][lineindprop#1]{startAutoLabeling;draw byNamedCompoundIndLine(1cm, 1)(#1);stopAutoLabeling;}} + +\DeclareDocumentCommand{\drawUnitIndLine}{o m}{% + \IfNoValueTF{#1}% + {\drawFromCurrentPicture[middle][lineindsized#2]{startAutoLabeling;draw byNamedCompoundIndLine(2cm, 2)(#2);stopAutoLabeling;}}% + {\drawFromCurrentPicture[middle][lineindsized#2#1]{startAutoLabeling;draw byNamedCompoundIndLine(#1, 2)(#2);stopAutoLabeling;}}% +} + +\def\drawRightAngle{% +\drawFromCurrentPicture[middle][onerightangle]{draw rightAngle;}} + +\def\drawTwoRightAngles{% +\drawFromCurrentPicture[middle][tworightangles]{draw twoRightAngles;}} + +\def\drawAngle#1{\drawFromCurrentPicture[middle][angle#1]{startAutoLabeling;draw byNamedAngleWithDummySides(#1);stopAutoLabeling;}} + +\def\drawAngleWithSides#1{\drawFromCurrentPicture[middle][anglewithsides#1]{startAutoLabeling;draw byNamedAngleSides(#1)();stopAutoLabeling;}} + +\DeclareDocumentCommand{\drawPolygon}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][polygon#3]{startAutoLabeling;draw byNamedPolygon(#3);stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][#2]{startAutoLabeling;draw byNamedPolygon(#3);stopAutoLabeling;}}% +} + +\DeclareDocumentCommand{\drawCircle}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][circle#3]{startAutoLabeling;draw byNamedCircle(#3);stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][circle#3]{ + startAutoLabeling; + startTempScale(#2); + draw byNamedCircle(#3); + stopTempScale; + stopAutoLabeling; + }}% +} + +\DeclareDocumentCommand{\drawArc}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][arc#3]{startAutoLabeling;draw byNamedArc(#3);stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][arc#3]{ + startAutoLabeling; + startTempScale(#2); + draw byNamedArc(#3); + stopTempScale; + stopAutoLabeling; + }}% +} + +\DeclareDocumentCommand{\drawLine}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][line#3]{startAutoLabeling;draw byNamedLineSeq(0)(#3);stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][#2]{startAutoLabeling;draw byNamedLineSeq(0)(#3);stopAutoLabeling;}}% +} + +\def\drawPointM#1{\drawFromCurrentPicture[middle][pointm#1]{startAutoLabeling; draw byNamedPointMark(#1); stopAutoLabeling;}} + +\DeclareDocumentCommand{\drawPointL}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][pointl#3]{startAutoLabeling; draw byNamedPointLines(#3,""); stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][pointl#3Minus#2]{startAutoLabeling; draw byNamedPointLines(#3,"#2"); stopAutoLabeling;}}% +} + +\DeclareDocumentCommand{\drawPoint}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][point#3]{ + startAutoLabeling; draw byNamedPointLines(#3,""); stopAutoLabeling; + draw byNamedPointMark(#3); + }}% + {\drawFromCurrentPicture[\plal][point#3Minus#2]{ + startAutoLabeling; draw byNamedPointLines(#3,"#2"); stopAutoLabeling; + draw byNamedPointMark(#3); + }}% +} + +\DeclareDocumentCommand{\drawMagnitude}{o o m}{% + \IfNoValueTF{#1}% + {\global\def\plal{middle}}% + {\global\def\plal{#1}}% + \IfNoValueTF{#2}% + {\drawFromCurrentPicture[\plal][magnitude#3]{startAutoLabeling;draw byNamedMagnitude(0)(#3);stopAutoLabeling;}}% + {\drawFromCurrentPicture[\plal][magnitude#2#3]{startAutoLabeling;draw byNamedMagnitude(#2)(#3);stopAutoLabeling;}}% +}
\ No newline at end of file diff --git a/Master/tlpkg/bin/tlpkg-ctan-check b/Master/tlpkg/bin/tlpkg-ctan-check index 435f4374fd9..0945703842d 100755 --- a/Master/tlpkg/bin/tlpkg-ctan-check +++ b/Master/tlpkg/bin/tlpkg-ctan-check @@ -152,7 +152,7 @@ my @TLP_working = qw( bxbase bxcalc bxcjkjatype bxdpx-beamer bxdvidriver bxghost bxjaholiday bxjaprnind bxjatoucs bxpapersize bxpdfver bxeepic bxenclose bxjalipsum bxjscls bxnewfont bxorigcapt bxtexlogo bxwareki - byo-twemojis bytefield + byo-twemojis byrne bytefield c90 c-pascal cabin cachepic caladea calcage calctab calculation calculator calligra calligra-type1 callouts calrsfs cals calxxxx-yyyy cancel canoniclayout cantarell diff --git a/Master/tlpkg/tlpsrc/byrne.tlpsrc b/Master/tlpkg/tlpsrc/byrne.tlpsrc new file mode 100644 index 00000000000..e69de29bb2d --- /dev/null +++ b/Master/tlpkg/tlpsrc/byrne.tlpsrc diff --git a/Master/tlpkg/tlpsrc/collection-pictures.tlpsrc b/Master/tlpkg/tlpsrc/collection-pictures.tlpsrc index c784a88c3af..8a78106eedb 100644 --- a/Master/tlpkg/tlpsrc/collection-pictures.tlpsrc +++ b/Master/tlpkg/tlpsrc/collection-pictures.tlpsrc @@ -22,6 +22,7 @@ depend bondgraphs depend braids depend bxeepic depend byo-twemojis +depend byrne depend cachepic depend callouts depend celtic |