summaryrefslogtreecommitdiff
path: root/graphics/metapost
diff options
context:
space:
mode:
authorNorbert Preining <norbert@preining.info>2022-02-08 03:01:03 +0000
committerNorbert Preining <norbert@preining.info>2022-02-08 03:01:03 +0000
commit98810f6d7d064e61186d0ce674c4ee9de47a1f55 (patch)
tree66a7f325cde31d66d1ff1955cb1ab4aa4bd51aa9 /graphics/metapost
parentf689c91581665257602a841d03f73078c3495e14 (diff)
CTAN sync 202202080301
Diffstat (limited to 'graphics/metapost')
-rw-r--r--graphics/metapost/contrib/macros/byrne/README20
-rw-r--r--graphics/metapost/contrib/macros/byrne/byrne-latex.pdfbin0 -> 156788 bytes
-rw-r--r--graphics/metapost/contrib/macros/byrne/byrne-latex.tex1244
-rw-r--r--graphics/metapost/contrib/macros/byrne/byrne.mp3852
-rw-r--r--graphics/metapost/contrib/macros/byrne/byrne.sty298
-rw-r--r--graphics/metapost/contrib/macros/fiziko/README2
-rw-r--r--graphics/metapost/contrib/macros/fiziko/fiziko.mp715
-rw-r--r--graphics/metapost/contrib/macros/fiziko/fiziko.pdfbin4559755 -> 5338496 bytes
-rw-r--r--graphics/metapost/contrib/macros/fiziko/fiziko.tex113
9 files changed, 6014 insertions, 230 deletions
diff --git a/graphics/metapost/contrib/macros/byrne/README b/graphics/metapost/contrib/macros/byrne/README
new file mode 100644
index 0000000000..39cb5c1d21
--- /dev/null
+++ b/graphics/metapost/contrib/macros/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/graphics/metapost/contrib/macros/byrne/byrne-latex.pdf b/graphics/metapost/contrib/macros/byrne/byrne-latex.pdf
new file mode 100644
index 0000000000..d1b46ed317
--- /dev/null
+++ b/graphics/metapost/contrib/macros/byrne/byrne-latex.pdf
Binary files differ
diff --git a/graphics/metapost/contrib/macros/byrne/byrne-latex.tex b/graphics/metapost/contrib/macros/byrne/byrne-latex.tex
new file mode 100644
index 0000000000..bbd12af27e
--- /dev/null
+++ b/graphics/metapost/contrib/macros/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/graphics/metapost/contrib/macros/byrne/byrne.mp b/graphics/metapost/contrib/macros/byrne/byrne.mp
new file mode 100644
index 0000000000..115998e257
--- /dev/null
+++ b/graphics/metapost/contrib/macros/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/graphics/metapost/contrib/macros/byrne/byrne.sty b/graphics/metapost/contrib/macros/byrne/byrne.sty
new file mode 100644
index 0000000000..c7c9666f29
--- /dev/null
+++ b/graphics/metapost/contrib/macros/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/graphics/metapost/contrib/macros/fiziko/README b/graphics/metapost/contrib/macros/fiziko/README
index 8eda66ae7e..5b7e9ada0e 100644
--- a/graphics/metapost/contrib/macros/fiziko/README
+++ b/graphics/metapost/contrib/macros/fiziko/README
@@ -1,5 +1,5 @@
Name: fiziko
-Version: 0.1.3
+Version: 0.2.0
License: GNU GPLv3 or later
Author: Sergey Slyusarev
Repository: https://github.com/jemmybutton/fiziko
diff --git a/graphics/metapost/contrib/macros/fiziko/fiziko.mp b/graphics/metapost/contrib/macros/fiziko/fiziko.mp
index cf134b0fed..fc5d6065fc 100644
--- a/graphics/metapost/contrib/macros/fiziko/fiziko.mp
+++ b/graphics/metapost/contrib/macros/fiziko/fiziko.mp
@@ -1,6 +1,6 @@
-% fiziko 0.1.3
+% fiziko 0.2.0
% MetaPost library for physics textbook illustrations
-% Copyright 2019 Sergey Slyusarev
+% 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
@@ -63,7 +63,7 @@ enddef;
primarydef i maskedWith p =
begingroup
- save q, invertedmask, resultimage;
+ save q, invertedmask, resultimage, breakpoint;
pair q[];
path invertedmask;
picture resultimage;
@@ -72,8 +72,8 @@ begingroup
q3 := lrcorner(i) shifted (1, -1);
q2 := (xpart(q3), ypart(q1));
q4 := (xpart(q1), ypart(q3));
- bp := ypart((ulcorner(p)--llcorner(p)) firstIntersectionTimes p);
- invertedmask := (subpath (bp, length(p) + bp) of p) -- q1 -- q2 -- q3 -- q4 -- q1 -- cycle;
+ breakpoint := ypart((ulcorner(p)--llcorner(p)) firstIntersectionTimes p);
+ invertedmask := (subpath (breakpoint, length(p) + breakpoint) of p) -- q1 -- q2 -- q3 -- q4 -- q1 -- cycle;
clip resultimage to invertedmask;
resultimage
endgroup
@@ -128,6 +128,32 @@ begingroup
endgroup
enddef;
+% rotation in radians
+
+primarydef somethingToRotate radRotated radAngle =
+ somethingToRotate rotated ((radAngle/pi)*180)
+enddef;
+
+%
+% some 3D stuff
+%
+
+% this one's from byrne.mp
+
+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;
+
%
% sometimes it's useful to put some arrows along the path. this macro puts them
% in the middles of the segments that have length no less than midArrowLimit;
@@ -188,7 +214,7 @@ def defineMinStrokeWidth (expr msw) =
% We don't want to display strokes that are too thin to print. Default value
% is subject to change when needed.
minStrokeWidth := msw;
- maxShadingStrokeWidth := 2minStrokeWidth;
+ maxShadingStrokeWidth := 3/2minStrokeWidth;
% At some point it's useless to display even dashes
minDashStrokeWidth := 1/3minStrokeWidth;
@@ -201,12 +227,18 @@ def defineMinStrokeWidth (expr msw) =
% all the shading algorithms need to know how close lines should be packed
shadingDensity := 3maxShadingStrokeWidth;
+ stippleSize := 3/2minStrokeWidth;
+ minStippleStep := 1/2stippleSize;
+ stippleShadingDensity := 3minStippleStep;
+ minStippleStrokeWidth := 1/20stippleSize;
+
% here are some pens
- pen thinpen, thickpen, fatpen;
+ pen thinpen, thickpen, fatpen, stipplepen;
thinpen := pencircle scaled minStrokeWidth;
thickpen := pencircle scaled 3minStrokeWidth;
fatpen := pencircle scaled 6minStrokeWidth;
+ stipplepen := pencircle scaled stippleSize;
enddef;
defineMinStrokeWidth(1/5pt);
@@ -214,9 +246,31 @@ defineMinStrokeWidth(1/5pt);
% here we set global light direction
def defineLightDirection (expr ldx, ldy) =
- pair lightDirection, lightDirectionVector;
+ pair lightDirection;
+ color lightDirectionVectorXYZ;
lightDirection := (ldx, ldy);
- lightDirectionVector := (sin(xpart(lightDirection)), sin(ypart(lightDirection)));
+ lightDirectionVectorXYZ := (0, 0, 1);
+ lightDirectionVectorXYZ := rotateXYZaround.x(lightDirectionVectorXYZ, ldy);
+ lightDirectionVectorXYZ := rotateXYZaround.y(lightDirectionVectorXYZ, ldx);
+enddef;
+
+vardef rotateXYZaround@# (expr p, a) =
+ save partProj, rv;
+ pair partProj;
+ color rv;
+ if str @# = "x":
+ partProj := (greenpart(p), bluepart(p)) radRotated -a;
+ rv := (redpart(p), xpart(partProj), ypart(partProj));
+ elseif str @# = "y":
+ partProj := (redpart(p), bluepart(p)) radRotated -a;
+ rv := (xpart(partProj), greenpart(p), ypart(partProj));
+ elseif str @# = "z":
+ partProj := (redpart(p), greenpart(p)) radRotated -a;
+ rv := (xpart(partProj), ypart(partProj), bluepart(p));
+ else:
+ errmessage("What axis is " & str @# & "?");
+ fi;
+ rv
enddef;
defineLightDirection(-1/8pi, 1/8pi);
@@ -373,9 +427,9 @@ enddef;
def brushGenerate (expr p, q, i) =
begingroup
- save w, bp, bt, t;
+ save w, brushPath, bt, t;
numeric w[], t[];
- path bp[], bt;
+ path brushPath[], bt;
bt := q;
w0 := (ypart(urcorner(bt)));
w1 := (ypart(lrcorner(bt)));
@@ -391,9 +445,9 @@ begingroup
elseif (arclength(p) > 0):
if (w0 > 99/100minStrokeWidth)
and (w1 > 99/100minStrokeWidth):
- bp1 := offsetPathGenerate (p, q yscaled 1/2, 0);
- bp2 := offsetPathGenerate (p, q yscaled -1/2, 0);
- fill bp1 -- reverse(bp2) -- cycle;
+ brushPath1 := offsetPathGenerate (p, q yscaled 1/2, 0);
+ brushPath2 := offsetPathGenerate (p, q yscaled -1/2, 0);
+ fill brushPath1 -- reverse(brushPath2) -- cycle;
elseif (w0 < 101/100minStrokeWidth) and (w1 < 101/100minStrokeWidth):
thinBrushGenerate (p, q, 0)
fi;
@@ -405,30 +459,66 @@ enddef;
% macro for thin lines which are actually dashed
%
-def thinBrushGenerate (expr p, q, i) =
+vardef thinBrushGenerate@#(expr p, q, i) =
begingroup
- save w, bp, bt, t, h, linecap;
+ save w, brushPath, bt, t, h, minLength, minWidth, dashPatternImage;
numeric w[], t[];
- path bp[], bt;
+ path brushPath[], bt;
+ picture dashPatternImage;
bt := q;
w0 := (ypart(urcorner(bt)));
w1 := (ypart(lrcorner(bt)));
w2 := floor((1/2(w0 + w1))/dashStrokeWidthStep)*dashStrokeWidthStep;
t := cutPathTime(bt, w2);
- bp1 := subpath (0, t) of p;
- bp2 := subpath (t, length(p)) of p;
+ brushPath1 := subpath (0, t) of p;
+ brushPath2 := subpath (t, length(p)) of p;
+ if (str @# = "") or (str @# = "hatches"):
+ minLength := minDashStrokeLength;
+ minWidth := minDashStrokeWidth;
+ elseif str @# = "stipples":
+ minLength := minStippleStep;
+ minWidth := minStippleStrokeWidth;
+ fi;
if (((w0 - w1) > dashStrokeWidthStep) and (i < 15))
- and ((arclength(bp1) > minDashStrokeLength)
- or (arclength(bp2) > minDashStrokeLength)):
- thinBrushGenerate (bp1, subpath (0, t) of q, i + 1);
- thinBrushGenerate (bp2, subpath (t, length(q)) of q, i + 1);
+ and ((arclength(brushPath1) > minLength)
+ or (arclength(brushPath2) > minLength)):
+ thinBrushGenerate@#(brushPath1, subpath (0, t) of q, i + 1);
+ thinBrushGenerate@#(brushPath2, subpath (t, length(q)) of q, i + 1);
else:
- linecap := butt;
- if (w2 > minStrokeWidth):
- w2 := minStrokeWidth;
- fi;
- if (w2 >= minDashStrokeWidth) and (arclength(p) > 0):
- draw p withpen thinpen dashed thinBrushPattern(w2, arclength(p));
+ if (str @# = "") or (str @# = "hatches"):
+ if (w2 > minStrokeWidth):
+ w2 := minStrokeWidth;
+ fi;
+ if (w2 >= minWidth) and (arclength(p) > 0):
+ draw p withpen thinpen dashed thinBrushPattern(w2, arclength(p));
+ fi;
+ elseif str @# = "stipples":
+ begingroup
+ interim linecap := rounded;
+ save stippleSizeVar;
+ stippleSizeVar := stippleSize;
+ save stippleSize;
+ w2 := 1/3w2;
+ if (w2 >= minWidth) and (arclength(p) > 0):
+ stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
+ dashPatternImage := stipplesBrushPattern(w2, arclength(p));
+ if urcorner(dashPatternImage) <> (0,0):
+ brushPath1 := offsetPathGenerate (p, (q yscaled 0) shifted (0, 1/3stippleShadingDensity), 0);
+ draw brushPath1 withpen (pencircle scaled stippleSize) dashed dashPatternImage;
+ fi;
+ stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
+ dashPatternImage := stipplesBrushPattern(w2, arclength(p));
+ if urcorner(dashPatternImage) <> (0,0):
+ brushPath2 := offsetPathGenerate (p, (q yscaled 0) shifted (0, -1/3stippleShadingDensity), 0);
+ draw brushPath2 withpen (pencircle scaled stippleSize) dashed dashPatternImage;
+ fi;
+ stippleSize := stippleSizeVar * (0.9 + uniformdeviate(0.3));
+ dashPatternImage := stipplesBrushPattern(w2, arclength(p));
+ if urcorner(dashPatternImage) <> (0,0):
+ draw p withpen (pencircle scaled stippleSize) dashed dashPatternImage;
+ fi;
+ fi;
+ endgroup
fi;
fi;
endgroup
@@ -443,9 +533,9 @@ vardef shadedEdge (expr p) =
brushGenerate (p,
offsetPathTemplate (p, 0) (
1/2minStrokeWidth + 2*minStrokeWidth
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(direction offsetPathTime of p), 1/2)
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of p), arcsin(1/2))
), 0, point offsetPathTime of p
)
), 0);
@@ -482,6 +572,51 @@ vardef thinBrushPattern (expr w, l) =
fi
enddef;
+
+%
+% Stipples are also dashes
+%
+
+vardef stipplesBrushPattern (expr w, l) =
+ save d, n, rn, rv, ss;
+ ss := 1/1000;
+ numeric d[];
+ picture rv;
+ %if w > stippleSize:
+ % d0 := minStippleStep;
+ %else:
+ n := (w*l)/(stippleSize**2);
+ rn := floor(n);
+ if rn > 0:
+ d0 := l/rn;
+ fi;
+ %fi;
+ if rn > 0:
+ d1 := uniformdeviate(d0);
+ d2 := d0-d1;
+ if rn >=3:
+ d3 := uniformdeviate(d0);
+ d4 := d0-d3;
+ %if uniformdeviate(2) > 1:
+ % d5 := uniformdeviate(d0);
+ % d6 := d0-d5;
+ % rv := dashpattern (off d1 on ss off (d2+d5)-ss on ss off (d4+d6)-ss on ss off d3-ss);
+ %else:
+ rv := dashpattern (off d1 on ss off (d2+d3)-ss on ss off d4-ss);
+ %fi;
+ else:
+ rv := dashpattern (off d1 on ss off d2-ss);
+ fi;
+ else:
+ if uniformdeviate(1) < n:
+ rv := dashpattern (off uniformdeviate(l-ss) on ss off l);
+ else:
+ rv := image();
+ fi;
+ fi;
+ rv
+enddef;
+
%
% macro that actually draws line of variable width
%
@@ -493,52 +628,86 @@ vardef brush (expr p) (text offsetFunction) =
enddef;
%
+% same, but only for thin brushes
+%
+
+vardef thinBrush@#(expr p) (text offsetFunction) =
+ image(
+ thinBrushGenerate@#(p, offsetPathTemplate(p, 0)(offsetFunction), 0);
+ )
+enddef;
+
+%
% This macro generates tube between paths p and q, of variable width d
% Tube is subdivided into segments in such a way that within every segment
% we need 2**n lines to generate even fill
%
-def tubeGenerate (expr p, q, d, i) =
-begingroup
- save w, bw, k, t, tubeWidth, sp, currentPath, currentTubePath, currentDepth;
+vardef tubeGenerate@#(expr p, q, d, i) =
+ save w, bw, k, t, tubeWidth, sp, currentPath, currentTubePath, currentDepth, lineDensity;
numeric w[], bw[], t, currentDepth;
path tubeWidth, sp, currentPath, currentTubePath;
tubeWidth := d yscaled 2;
+ if (str @# = "") or (str @# = "hatches"):
+ lineDensity := shadingDensity;
+ elseif (str @# = "stipples"):
+ lineDensity := stippleShadingDensity;
+ fi;
w0 := (ypart(urcorner(tubeWidth))) - 1/1000;
w1 := (ypart(lrcorner(tubeWidth))) + 1/1000;
- w2 := ceiling(log(w0/shadingDensity, 2));
- w3 := ceiling(log(w1/shadingDensity, 2));
+ w2 := ceiling(log(w0/lineDensity, 2));
+ w3 := ceiling(log(w1/lineDensity, 2));
if ((w2 > w3) and (i<20)):
- t := cutPathTime(tubeWidth, shadingDensity*(2**(w2-1)));
- tubeGenerate (subpath (0, t) of p, subpath (0, t) of q, subpath (0, t) of d, i + 1);
- tubeGenerate (subpath (t, length(p)) of p, subpath (t, length(q)) of q, subpath (t, length(d)) of d, i + 1);
+ t := cutPathTime(tubeWidth, lineDensity*(2**(w2-1)));
+ tubeGenerate@#(subpath (0, t) of p, subpath (0, t) of q, subpath (0, t) of d, i + 1);
+ tubeGenerate@#(subpath (t, length(p)) of p, subpath (t, length(q)) of q, subpath (t, length(d)) of d, i + 1);
else:
if (arclength(p) > 0) and (arclength(q) > 0):
bw1 := 2**w2;
currentTubePath := interpath (1/2, q, p);
for k := 0 upto bw1:
currentPath := interpath (k/bw1, q, p);
- angleOnTube := arccos(((k/bw1)*2) - 1);
- currentDepth := -abs((1-sin(angleOnTube))*w0);
+ angleOnTube := arcsin(((k/bw1)*2) - 1);
+ currentDepth := -abs((1-sin(angleOnTube + 1/2pi))*w0);
if shadowsEnabled:
currentPath := shadowCut(currentPath, currentDepth);
fi;
- brushGenerate (currentPath,
- offsetPathTemplate(currentPath, 0)(
- maxShadingStrokeWidth
- if odd (k): * (abs(ypart(point offsetPathTime of tubeWidth)/bw1) - 1/2shadingDensity) fi
- %* orderFade(xpart(unitvector(direction offsetPathTime of tubeWidth yscaled 1/2cos(angleOnTube))), k) % why was it even here?
- * angleToLightness(
- tubeAngleToAbsoulteAngle((
- angleOnTube,
- angleRad(direction offsetPathTime of currentTubePath),
- angleRad(direction offsetPathTime of tubeWidth yscaled 1/2)
- )), currentDepth, point offsetPathTime of currentPath)
- ), 0);
+ if (str @# = "") or (str @# = "hatches"):
+ brushGenerate (currentPath,
+ offsetPathTemplate(currentPath, 0)(
+ maxShadingStrokeWidth
+ if odd (k): * (abs(ypart(point offsetPathTime of tubeWidth)/bw1) - 1/2lineDensity) fi
+ * normalVectorToLightness(
+ tubeAnglesToNormalVector((
+ angleOnTube,
+ angleRad(direction offsetPathTime of currentTubePath),
+ angleRad(direction offsetPathTime of (tubeWidth yscaled 1/2))
+ )), currentDepth, point offsetPathTime of currentPath)
+ ), 0);
+ elseif (str @# = "stipples"):
+ begingroup
+ save stippleShadingDensity;
+ if w2 > 0:
+ stippleShadingDensity := 2w0/(2**w2); % When the distance between the lines changes, wtipples should spread further apart
+ else:
+ stippleShadingDensity := w0;
+ fi;
+ thinBrushGenerate.@#(currentPath,
+ offsetPathTemplate(currentPath, 0)(
+ stippleSize
+ if odd (k): * (abs(ypart(point offsetPathTime of tubeWidth)/bw1) - 1/2lineDensity) fi
+ * normalVectorToLightness(
+ tubeAnglesToNormalVector((
+ angleOnTube,
+ angleRad(direction offsetPathTime of currentTubePath),
+ angleRad(direction offsetPathTime of (tubeWidth yscaled 1/2))
+ )), currentDepth, point offsetPathTime of currentPath)
+ ), 0);
+ endgroup
+ fi;
endfor;
fi;
fi;
-endgroup
enddef;
%
@@ -587,12 +756,12 @@ begingroup
offsetPathTemplate(currentPath, 0)(
maxShadingStrokeWidth
* orderFade(offsetPathLength[1/l1, l2], j)
- * angleToLightness(
- tubeAngleToAbsoulteAngle((
- arccos(pthdir*((offsetPathLength*2)-1)),
- pathAngle,
- tubeAngle)
- ), -2(1/2arclength(currentPath))+sqrt(1 - (2offsetPathLength - 1)**2)*(1/2arclength(currentPath)), point offsetPathTime of currentPath)
+ * normalVectorToLightness(
+ tubeAnglesToNormalVector((
+ arcsin(pthdir*((offsetPathLength*2)-1)),
+ pathAngle,
+ tubeAngle)
+ ), -2(1/2arclength(currentPath))+sqrt(1 - (2offsetPathLength - 1)**2)*(1/2arclength(currentPath)), point offsetPathTime of currentPath)
)
, 0);
endfor;
@@ -604,84 +773,69 @@ enddef;
%
% This macro converts some measurements of point on tube to absolute angle.
% Since there are three such measurements, macro gets them as as a single
-% argument of "color" type, in case it will eventually appear as a result
+% argument of "color" type, in case it would eventually appear as a result
% of some other macro.
%
+% redpart is the angle on the tube's circumference
+% greenpart is the angle of the tube path
+% bluepart is the angle of the tube's outline
+%
-vardef tubeAngleToAbsoulteAngle (expr p) =
- save a;
- numeric a[];
- a1 := bluepart(p) + 1/2pi;
- a2 := arccos(cos(redpart(p))*sin(a1));
- a3 := greenpart(p) + 1/2pi;
- a4 := arccos((cos(a1) * cos(a3) - cos(a2) * sin(a3))*(99/100));
- a5 := arccos((cos(a1) * sin(a3) + cos(a2) * cos(a3))*(99/100));
- (a5, a4)
+vardef tubeAnglesToNormalVector (expr p) =
+ save normalVector;
+ color normalVector;
+ normalVector := (0, 0, 1);
+ normalVector := rotateXYZaround.y(normalVector, -bluepart(p));
+ normalVector := rotateXYZaround.x(normalVector, redpart(p));
+ normalVector := rotateXYZaround.z(normalVector, -greenpart(p));
+ normalVector
enddef;
%
-% frontends to simplify tube drawing. tubeOutline variable changes on every call
-% of any tube frontend function and can be used afterwards.
+% frontend to simplify tube drawing. tubeOutline variable changes on every call
+% of the function and can be used afterwards.
%
path tubeOutline;
boolean drawTubeEnds;
drawTubeEnds := true;
-vardef tube.l (expr p)(text offsetFunction)=
- save q;
+vardef tube@#(expr p)(text offsetFunction)=
+ save q, respic;
path q[];
+ picture respic;
q0 := offsetPathSubdivide(p);
q1 := offsetPathTemplate(q0, 0)(offsetFunction);
q2 := offsetPathGenerate (q0, q1, 0);
q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
tubeOutline := q3--reverse(q2)--cycle;
- image(
- tubeGenerate (q2, q3, q1, 0);
- if (cycle p) or (not drawTubeEnds):
- draw q2 withpen thinpen;
- draw q3 withpen thinpen;
- else:
- draw q2--reverse(q3)--cycle withpen thinpen;
- fi;
- )
-enddef;
-
-vardef tube.t (expr p)(text offsetFunction)=
- save q;
- path q[];
- q0 := offsetPathSubdivide(p);
- q1 := offsetPathTemplate(q0, 0)(offsetFunction);
- q2 := offsetPathGenerate (q0, q1, 0);
- q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
- tubeOutline := q3--reverse(q2)--cycle;
- image(
- tubeGenerateAlt (q2, q3, q1);
- if (cycle p) or (not drawTubeEnds):
- draw q2 withpen thinpen;
- draw q3 withpen thinpen;
+ if str @# = "e":
+ if not drawTubeEnds:
+ image(
+ draw q2 withpen thinpen;
+ draw q3 withpen thinpen;
+ )
else:
- draw q2--reverse(q3)--cycle withpen thinpen;
- fi;
- )
-enddef;
-
-vardef tube.e (expr p)(text offsetFunction)=
- save q;
- path q[];
- q0 := offsetPathSubdivide(p);
- q1 := offsetPathTemplate(q0, 0)(offsetFunction);
- q2 := offsetPathGenerate (q0, q1, 0);
- q3 := offsetPathGenerate (q0, q1 yscaled -1, 0);
- tubeOutline := q3--reverse(q2)--cycle;
- if not drawTubeEnds:
- image(
- draw q2 withpen thinpen;
- draw q3 withpen thinpen;
- )
+ tubeOutline
+ fi
else:
- tubeOutline := q3--reverse(q2)--cycle;
- tubeOutline
+ image(
+ if str @# = "l":
+ respic := image(tubeGenerate (q2, q3, q1, 0););
+ elseif str @# = "s":
+ respic := image(tubeGenerate.stipples(q2, q3, q1, 0);)
+ elseif str @# = "t":
+ respic := image(tubeGenerateAlt (q2, q3, q1););
+ fi;
+ if (cycle p) or (not drawTubeEnds):
+ draw q2 withpen thinpen;
+ draw q3 withpen thinpen;
+ else:
+ draw q2--reverse(q3)--cycle withpen thinpen;
+ fi;
+ clip respic to (q2--reverse(q3)--cycle);
+ draw respic;
+ )
fi
enddef;
@@ -689,36 +843,66 @@ enddef;
% Sphere can be used as a cap for a tube, so it has same 2**n lines.
%
-vardef sphere.c (expr d) =
- save currentCircle, origCircle, currentRadius, currentDepth, order, circleThickness;
+vardef sphere@#(expr d) =
+ save currentCircle, origCircle, currentRadius, currentDepth, order, circleThickness, lineDensity, shadingPicture;
path currentCircle, origCircle;
numeric currentRadius, currentDepth, order;
+ picture shadingPicture;
+ if (str @# = "") or (str @# = "c"):
+ lineDensity := shadingDensity;
+ elseif (str @# = "s"):
+ lineDensity := stippleShadingDensity;
+ fi;
origCircle := fullcircle;
- order := 2**ceiling(log((1/2d)/shadingDensity, 2));
+ order := 2**ceiling(log((1/2d)/lineDensity, 2));
image(
draw fullcircle scaled d withpen thinpen;
- for i := 1 upto order:
- currentRadius := i/order;
- currentCircle := origCircle scaled (currentRadius*d) rotated uniformdeviate (1/4pi);
- if odd(i):
- circleThickness := maxShadingStrokeWidth * ((abs(d - (shadingDensity*order)))/order);
- else:
- circleThickness := maxShadingStrokeWidth;
- fi;
- currentDepth:= -(1-sqrt(1-currentRadius**2))*(1/2d);
- if shadowsEnabled:
- currentCircle := shadowCut(currentCircle, currentDepth);
- fi;
- brushGenerate (currentCircle,
- offsetPathTemplate (currentCircle, 0) (
- circleThickness
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(direction offsetPathTime of currentCircle), currentRadius)
- ), currentDepth, point offsetPathTime of currentCircle
- )
- ), 0);
- endfor;
+ shadingPicture := image(
+ for i := 1 upto order:
+ currentRadius := i/order;
+ currentCircle := origCircle scaled (currentRadius*d) rotated uniformdeviate (1/4pi);
+ if odd(i):
+ circleThickness := maxShadingStrokeWidth * ((abs(d - (lineDensity*order)))/order);
+ else:
+ circleThickness := maxShadingStrokeWidth;
+ fi;
+ currentDepth:= -(1-sqrt(1-currentRadius**2))*(1/2d);
+ if shadowsEnabled:
+ currentCircle := shadowCut(currentCircle, currentDepth);
+ fi;
+ if (str @# = "") or (str @# = "c"):
+ brushGenerate (currentCircle,
+ offsetPathTemplate (currentCircle, 0) (
+ circleThickness
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of currentCircle), arcsin(currentRadius))
+ ), currentDepth, point offsetPathTime of currentCircle
+ )
+ ), 0);
+ elseif (str @# = "s"):
+ begingroup
+ save stippleShadingDensity;
+ if order > 0:
+ stippleShadingDensity := d/order; % When the distance between the lines changes, wtipples should spread further ap
+ else:
+ stippleShadingDensity := 1/2d;
+ fi;
+ thinBrushGenerate.stipples(currentCircle,
+ offsetPathTemplate (currentCircle, 0) (
+ circleThickness
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of currentCircle), arcsin(currentRadius))
+ ), currentDepth, point offsetPathTime of currentCircle
+ )
+ ), 0);
+ endgroup
+ fi;
+ endfor;
+ );
+ clip shadingPicture to (fullcircle scaled d);
+ draw shadingPicture;
)
enddef;
@@ -729,7 +913,7 @@ enddef;
% so all we need to do is to fade lines correspondingly
%
-vardef sphere.l (expr d, lat) =
+vardef sphereLat (expr d, lat) =
save p, a, x, y, sphlat, latrad, n, c, currentPath, nline, tlat;
path p[], currentPath, currentArc;
sphlat := 0;
@@ -779,15 +963,15 @@ vardef sphere.l (expr d, lat) =
)**abs(sind(lat))
)**2)
, nline)
- * angleToLightness(
- sphereAngleToAbsoulteAngle((
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector((
(
if (abs(point offsetPathTime of currentPath) > 0):
angleRad(point offsetPathTime of currentPath)
else:
0
fi
- + 1/2pi), 2abs(point offsetPathTime of currentPath)/(d+1))
+ ), arcsin(2abs(point offsetPathTime of currentPath)/(d+1)))
), 0, point offsetPathTime of currentPath)
), 0);
fi;
@@ -815,37 +999,38 @@ vardef orderFade (expr v, n)=
enddef;
%
-% This one converts point location on sphere to absolute angle
+% This one converts point location on sphere to normal vector
%
-vardef sphereAngleToAbsoulteAngle (expr p) =
- save a;
- numeric a[];
- a1 := xpart(p) - 1/2pi;
- a2 := arcsin(ypart(p));
- a3 := arccos(sin(a2)*cos(a1));
- a4 := pi - arccos(sin(a2)*sin(a1));
- (a3, a4)
+vardef sphereAnglesToNormalVector (expr p) =
+ save normalVector;
+ color normalVector;
+ normalVector := (0, 0, 1);
+ normalVector := rotateXYZaround.y(normalVector, ypart(p));
+ normalVector := rotateXYZaround.z(normalVector, -xpart(p));
+ normalVector
enddef;
%
% Once we get two angles at some point of some surface, we can compute light intensity there.
%
-vardef angleToLightness (expr p, d, q) =
+vardef normalVectorToLightness (expr normalVector, d, q) =
save returnValue, shiftedShadowPath;
path shiftedShadowPath;
if shadowsEnabled:
for i := 0 step 1 until numberOfShadows:
- shiftedShadowPath := shadowPath[i] shifted (lightDirectionVector scaled (d-shadowDepth[i]));
+ shiftedShadowPath := shadowPath[i] shifted
+ ((redpart(lightDirectionVectorXYZ), greenpart(lightDirectionVectorXYZ))
+ scaled ((d-shadowDepth[i])*bluepart(lightDirectionVectorXYZ)));
if q isInside shiftedShadowPath:
returnValue := 1;
fi;
endfor;
fi;
if not known returnValue:
- returnValue := (cos(xpart(p) + xpart(lightDirection))++cos(ypart(p) - ypart(lightDirection)));
- returnValue := angleToLightnessPP(returnValue);
+ returnValue := 1 - (normalVector dotprodXYZ lightDirectionVectorXYZ);
+ returnValue := lightnessPP(returnValue);
fi;
if returnValue > 1:
1
@@ -854,8 +1039,8 @@ vardef angleToLightness (expr p, d, q) =
fi
enddef;
-vardef angleToLightnessPP (expr v) =
- v**3
+vardef lightnessPP (expr v) =
+ v
enddef;
% Shadows are global
@@ -874,7 +1059,9 @@ vardef shadowCut (expr pathToCut, currentDepth)=
numeric pathShadowCut;
currentPath := pathToCut;
for j := 0 step 1 until numberOfShadows:
- shiftedShadowPath := shadowPath[j] shifted (lightDirectionVector scaled (currentDepth - shadowDepth[j]));
+ shiftedShadowPath := shadowPath[j] shifted
+ ((redpart(lightDirectionVectorXYZ), greenpart(lightDirectionVectorXYZ))
+ scaled ((currentDepth - shadowDepth[j])*bluepart(lightDirectionVectorXYZ)));
forever:
pathShadowIntersection := shiftedShadowPath firstIntersectionTimes currentPath;
pathShadowCut := ypart(pathShadowIntersection);
@@ -1256,19 +1443,19 @@ vardef globe (expr s, lon, lat) =
picture p[];
path lm;
begingroup
- save angleToLightnessPP;
- vardef angleToLightnessPP (expr v) =
- 1/2(v**3)
+ save lightnessPP;
+ vardef lightnessPP (expr v) =
+ 1/2v
enddef;
- p1 := image(draw sphere.l(2s, lat));
- vardef angleToLightnessPP (expr v) =
+ p1 := image(draw sphereLat(2s, lat));
+ vardef lightnessPP (expr v) =
if (abs(cos(sphlat)) > 7/8 + uniformdeviate (1/20)):
- 1/4(v**2)
+ 1/4v
else:
- 1/3(v**2) + 2/3
+ 1/3v + 2/3
fi
enddef;
- p2 := image(draw sphere.l(2s, lat));
+ p2 := image(draw sphereLat(2s, lat));
endgroup;
image(
draw fullcircle scaled 2s withpen thinpen;
@@ -1280,9 +1467,9 @@ vardef globe (expr s, lon, lat) =
thinBrushGenerate (lm,
offsetPathTemplate (lm, 0) (
2/3minStrokeWidth + 1/3minStrokeWidth
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(point offsetPathTime of lm) + 1/4pi, abs(point offsetPathTime of lm)/2s)
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of lm), arcsin(abs(point offsetPathTime of lm)/2s))
), 0, point offsetPathTime of lm
)
), 0);
@@ -1375,7 +1562,7 @@ vardef solidSurface (expr p) =
enddef;
vardef solid (expr p, a, t) =
- save stripes, stripeskind, d, i, j, c;
+ save stripes, stripeskind, d, i, j, c, strokeVariation;
picture stripes, stripeskind;
pair c;
stripes := image(
@@ -1385,18 +1572,19 @@ vardef solid (expr p, a, t) =
c := 1/2[ulcorner(p rotated (90 - a)), lrcorner(p rotated (90 - a))] rotated (a - 90);
for i:= 0 step (3/2shadingDensity)/d1 until 1:
if (t = 1):
+ strokeVariation := uniformdeviate(1)-1/2;
j := round(i*d1/(3/2shadingDensity));
if (j mod 4) = 0:
- stripeskind := dashpattern (on 8shadingDensity off 4shadingDensity);
+ stripeskind := dashpattern (on (8-strokeVariation)*shadingDensity off (4+strokeVariation)*shadingDensity);
fi;
if ((j mod 4) = 1) or ((j mod 4) = 3):
- stripeskind := dashpattern (off 1shadingDensity on 6shadingDensity off 5shadingDensity);
+ stripeskind := dashpattern (off 1shadingDensity on (6-strokeVariation)*shadingDensity off (5+strokeVariation)*shadingDensity);
fi;
if (j mod 4) = 2:
stripeskind := dashpattern (on 0 off 12shadingDensity);
fi;
fi;
- draw ((dir(a) scaled 1/2d2) -- (dir(a + 180) scaled 1/2d2)) shifted c shifted i[dir(a + 90) scaled 1/2d1, dir(a -90) scaled 1/2d1] withpen thinpen
+ draw ((dir(a) scaled 1/2(d2-uniformdeviate(3shadingDensity))) -- (dir(a + 180) scaled 1/2d2)) shifted c shifted i[dir(a + 90) scaled 1/2d1, dir(a -90) scaled 1/2d1] withpen thinpen
dashed stripeskind;
endfor;
);
@@ -1537,14 +1725,6 @@ vardef weight.h (expr h) =
) maskedWith (q3 -- reverse(q2) -- (q2 yscaled 0 shifted (0, -h)) -- (reverse(q3) yscaled 0 shifted (0, -h)) -- cycle)
maskedWith q6;
draw shadedEdge(q6);
- thinBrushGenerate (q5,
- offsetPathTemplate (q5, 0) (
- minDashStrokeWidth + minStrokeWidth
- * angleToLightness(
- (arccos(1 - offsetPathLength*2), 1/2pi)
- , 0, point offsetPathTime of q5
- )
- ), 0);
)
enddef;
@@ -1658,9 +1838,9 @@ vardef pulleyWheel (expr d) =
thinBrushGenerate (fullcircle scaled i,
offsetPathTemplate (fullcircle scaled i, 0) (
2/3minStrokeWidth + minStrokeWidth
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(direction offsetPathTime of fullcircle), i/4d)
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of fullcircle), arcsin(i/4d))
), 0, point offsetPathTime of fullcircle scaled i
)
), 0);
@@ -1668,9 +1848,9 @@ vardef pulleyWheel (expr d) =
thinBrushGenerate (fullcircle scaled i,
offsetPathTemplate (fullcircle scaled i, 0) (
1/4minStrokeWidth + minStrokeWidth
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(direction offsetPathTime of fullcircle) + pi, 1/2)
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of fullcircle) + pi, arcsin(1/2))
), 0, point offsetPathTime of fullcircle scaled i
)
), 0);
@@ -1709,9 +1889,9 @@ vardef wheel (expr d, a) =
thinBrushGenerate (fullcircle scaled i,
offsetPathTemplate (fullcircle scaled i, 0) (
2/3minStrokeWidth + minStrokeWidth
- * angleToLightness(
- sphereAngleToAbsoulteAngle(
- (angleRad(direction offsetPathTime of fullcircle) + pi, i/4d)
+ * normalVectorToLightness(
+ sphereAnglesToNormalVector(
+ (angleRad(point offsetPathTime of fullcircle) + pi, arcsin(i/4d))
), 0, point offsetPathTime of fullcircle scaled i
)
), 0);
@@ -1740,6 +1920,109 @@ vardef wheel (expr d, a) =
)
enddef;
+
+%
+% This one roughly finds the center of mass for a closed shape
+%
+
+vardef shapeCenterOfMass (expr p) =
+ save i, a, aTotal, q;
+ numeric i, a, aTotal;
+ pair rv, q[];
+ q0 := point 0 of p;
+ aTotal := 0;
+ rv := (0, 0);
+ for i := 1 step 1 until (length(p)-2):
+ q1 := point i of p;
+ q2 := point (i+1) of p;
+ if (xpart(q1-q0) = 0):
+ a := (abs(ypart(q1-q0)/cm)*abs(xpart(q2-q0)/cm))/2;
+ elseif (ypart(q1-q0) = 0):
+ a := (abs(xpart(q1-q0)/cm)*abs(ypart(q2-q0)/cm))/2;
+ elseif (abs(xpart(q1-q0)) > abs(ypart(q1-q0))):
+ begingroup
+ save qA;
+ pair qA;
+ qA = whatever[q0, q0 + (0,1)];
+ qA = whatever[q2, q2 + (q1-q0)];
+ a := (abs(ypart(qA-q0)/cm)*abs(xpart(q1-q0)/cm))/2;
+ endgroup
+ else:
+ begingroup
+ save qA;
+ pair qA;
+ qA = whatever[q0, q0 + (1,0)];
+ qA = whatever[q2, q2 + (q1-q0)];
+ a := (abs(xpart(qA-q0)/cm)*abs(ypart(q1-q0)/cm))/2;
+ endgroup
+ fi;
+ %a := 2;
+ aTotal := aTotal + a;
+ rv := rv + ((q0 + q1 + q2) scaled (a/3));
+ endfor;
+ if (aTotal > 0):
+ rv := rv scaled (1/aTotal);
+ else:
+ rv := (0, 0);
+ fi;
+ rv
+enddef;
+
+%
+% This macro is for drawing shaded flat surfaces
+%
+
+vardef flatSurface@#(expr surfPath, normalVector, hatchAngle) =
+ save p, aHatch, hatchImage, surfLight, totalHeight, totalWidth, lineDensity, distFromEdge, hatchLength;
+ path p, aHatch;
+ picture hatchImage;
+ numeric distFromEdge, hatchLength;
+ surfLight := normalVectorToLightness(normalVector, 0, (0, 0));
+ p := surfPath rotated -hatchAngle;
+ if (str @# = "") or (str @# = "hatches"):
+ lineDensity := shadingDensity;
+ elseif (str @# = "stipples"):
+ lineDensity := stippleShadingDensity;
+ fi;
+ hatchImage := image(
+ totalHeight := abs(ypart(llcorner(p)) - ypart(urcorner(p)));
+ totalWidth := abs(xpart(llcorner(p)) - xpart(urcorner(p)));
+ %fill surfPath withcolor (surfLight, surfLight, surfLight);
+ for i := ypart(llcorner(p)) step (totalHeight/round(totalHeight/lineDensity)) until ypart(urcorner(p)):
+ distFromEdge := abs((i - ypart(llcorner(p)))/(ypart(urcorner(p))-ypart(llcorner(p))));
+ aHatch := (xpart(llcorner(p)),i) -- (xpart(lrcorner(p)), i);
+ aHatch := (point xpart(aHatch firstIntersectionTimes p) of aHatch) --
+ (point xpart(reverse(aHatch) firstIntersectionTimes p) of reverse(aHatch));
+ hatchLength := arclength(aHatch);
+ if arclength(aHatch) > 0:
+ aHatch := aHatch rotated hatchAngle;
+ aHatch := pathSubdivide(aHatch,3+round(uniformdeviate(2)));
+ if (str @# = "") or (str @# = "hatches"):
+ draw brush(aHatch)(
+ (2minStrokeWidth*(1/15+surfLight))*
+ (1-6/7sqrt(
+ sin(offsetPathLength*pi)*(hatchLength/totalWidth)*
+ sin(distFromEdge*pi)
+ ))
+ );
+ elseif (str @# = "stipples"):
+ draw thinBrush.stipples(aHatch)(
+ (stippleSize*(1/15+surfLight))*
+ (1-5/6sqrt(
+ sin(offsetPathLength*pi)*(hatchLength/totalWidth)*
+ sin(distFromEdge*pi)
+ ))
+ );
+ fi;
+ fi;
+ endfor;
+ );
+ clip hatchImage to surfPath;
+ image(
+ draw hatchImage;
+ )
+enddef;
+
%
% These macros are for drawing wood texture. A bunch of wood-related global
% variables are also here.
@@ -1871,7 +2154,6 @@ vardef appendList@#(suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates
save v, valueExists;
string v;
boolean valueExists;
- valueExists := false;
if str @# = "":
if not string listName:
string listName;
@@ -1884,12 +2166,9 @@ vardef appendList@#(suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates
if unknown listName@#:
listName@# := "";
fi;
+ valueExists := false;
if omitDuplicates:
- for i=scantokens(listName@#):
- if (i = valueToAdd):
- valueExists := true;
- fi;
- endfor;
+ valueExists := isInList@#(valueToAdd, listName)
fi;
if not valueExists:
if string valueToAdd:
@@ -1909,6 +2188,38 @@ vardef appendList@#(suffix listName)(expr valueToAdd, whereToAdd, omitDuplicates
fi;
enddef;
+vardef isInList@#(expr valueToLookFor)(suffix listName) =
+ save rv, i;
+ boolean rv;
+ rv := false;
+ if str @# = "":
+ if not string listName:
+ string listName;
+ fi;
+ else:
+ if not string listName0:
+ string listName[];
+ fi;
+ fi;
+ if unknown listName@#:
+ listName@# := "";
+ fi;
+ if string valueToLookFor:
+ forsuffixes i=scantokens(listName@#):
+ if (str i = valueToLookFor):
+ rv := true;
+ fi;
+ endfor;
+ else:
+ for i=scantokens(listName@#):
+ if (i = valueToLookFor):
+ rv := true;
+ fi;
+ endfor;
+ fi;
+ rv
+enddef;
+
vardef sortList (expr listToSort, ascending) =
save nPre, nPost, pivot, isSorted, lastValue, preList, postList, rv;
numeric nPre, nPost, pivot;
diff --git a/graphics/metapost/contrib/macros/fiziko/fiziko.pdf b/graphics/metapost/contrib/macros/fiziko/fiziko.pdf
index aaf3cfff42..32b5300d22 100644
--- a/graphics/metapost/contrib/macros/fiziko/fiziko.pdf
+++ b/graphics/metapost/contrib/macros/fiziko/fiziko.pdf
Binary files differ
diff --git a/graphics/metapost/contrib/macros/fiziko/fiziko.tex b/graphics/metapost/contrib/macros/fiziko/fiziko.tex
index 4848055003..3e164278da 100644
--- a/graphics/metapost/contrib/macros/fiziko/fiziko.tex
+++ b/graphics/metapost/contrib/macros/fiziko/fiziko.tex
@@ -1,10 +1,9 @@
-\documentclass{article}
+\documentclass{ltxdoc}
-\usepackage{luamplib}
+\usepackage{luamplib, listings, bxtexlogo, ccicons}
\everymplib{verbatimtex \leavevmode etex; input fiziko.mp; beginfig(1);}
\everyendmplib{endfig;}
-\usepackage{listings}
\lstset{
language=MetaPost,
numbers=left,
@@ -12,33 +11,31 @@ numberstyle=\tiny,
basicstyle=\scriptsize
}
-\usepackage{cclicenses}
-
\author{Sergey Slyusarev}
-\title{``fiziko'' v. 0.1.3 package for MetaPost}
+\title{``fiziko'' v. 0.2.0 package for \METAPOST}
\begin{document}
\maketitle
\begin{abstract}
-This document describes a bunch of macros provided by ``fiziko'' library for MetaPost.
+This document describes a bunch of macros provided by ``fiziko'' library for \METAPOST.
\end{abstract}
\begin{centering}
This document is distributed under CC-BY-SA 4.0 license
-\cc\bysa
+\ccbysa
https://github.com/jemmybutton/fiziko
\end{centering}
\section{Introduction}
-This MetaPost library was initially written to automate some elements of black and white illustrations for a physics textbook. First and foremost it provides functions to draw things like lines of variable width, shaded spheres and tubes of different kinds, which can be used to produce images of a variety of objects. The library also contains functions to draw some objects constructed from these primitives.
+This \METAPOST\ library was initially written to automate some elements of black and white illustrations for a physics textbook. First and foremost it provides functions to draw things like lines of variable width, shaded spheres and tubes of different kinds, which can be used to produce images of a variety of objects. The library also contains functions to draw some objects constructed from these primitives.
\section{Usage}
-Simply include this in the beginning of your MetaPost document:
+Simply include this in the beginning of your \METAPOST\ document:
\begin{lstlisting}
input fiziko.mp
@@ -113,19 +110,34 @@ This macro returns a \texttt{picture} of a sphere with specified diameter shaded
endfor;
\end{mplibcode}
-\subsection{sphere.l (\emph{diameter, angle})}
+\subsection{sphere.s (\emph{diameter})}
+This macro returns a \texttt{picture} of a sphere with specified diameter shaded with stipples.
+
+\begin{lstlisting}
+ for i := 1 step 1 until 6:
+ draw sphere.s(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
+ endfor;
+\end{lstlisting}
+
+\begin{mplibcode}
+ for i := 1 step 1 until 6:
+ draw sphere.s(i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
+ endfor;
+\end{mplibcode}
+
+\subsection{sphereLat (\emph{diameter, angle})}
This macro returns a \texttt{picture} of a shaded sphere with specified diameter. Unlike \texttt{sphere.c} macro, this one draws latitudinal strokes around axis rotated at specified \texttt{angle}.
\begin{lstlisting}
for i := 1 step 1 until 6:
- draw sphere.l(i*1/4cm, -90 + i*30)
+ draw sphereLat(i*1/4cm, -90 + i*30)
shifted (1/2cm*(i*(i+1))/2, 0);
endfor;
\end{lstlisting}
\begin{mplibcode}
for i := 1 step 1 until 6:
- draw sphere.l(i*1/4cm, -90 + i*30)
+ draw sphereLat(i*1/4cm, -90 + i*30)
shifted (1/2cm*(i*(i+1))/2, 0);
endfor;
\end{mplibcode}
@@ -160,6 +172,21 @@ This macro returns a \texttt{picture} of a shaded ``tube'' of variable width alo
draw tube.t (p)(1/2cm*sin(offsetPathLength*pi));
\end{mplibcode}
+\subsection{tube.s (\emph{path})(\emph{offset function})}
+This macro returns a \texttt{picture} of a shaded ``tube'' of variable width along given path, which is controlled by some arbitrary function, analogous to \texttt{offsetPath}. ``Tube'' drawn by this macro is shaded with stipples. Once tube is generated, you can call \texttt{tubeOutline} path global variable, if you need one.
+
+\begin{lstlisting}
+ path p;
+ p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
+ draw tube.s (p)(1/2cm*sin(offsetPathLength*pi));
+\end{lstlisting}
+
+\begin{mplibcode}
+ path p;
+ p := (0,0){dir(30)}..(5cm, 0)..{dir(30)}(10cm, 0);
+ draw tube.s (p)(1/2cm*sin(offsetPathLength*pi));
+\end{mplibcode}
+
\subsection{tube.e (\emph{path})(\emph{offset function})}
This macro returns the outline of a tube as a path.
@@ -200,7 +227,7 @@ This macro returns a \texttt{picture} of a pulley with specified \texttt{diamete
endfor;
\end{lstlisting}
-\begin{mplibcode}
+\noindent\begin{mplibcode}
draw (-1/8cm, 0)--(12cm, 0);
for i := 1 step 1 until 6:
r := 1/7cm*i;
@@ -248,7 +275,8 @@ This macro returns a \texttt{picture} of a wheel with specified \texttt{diameter
endfor;
\end{lstlisting}
-\begin{mplibcode}
+
+\noindent\begin{mplibcode}
draw (-1/8cm, 0)--(12cm, 0);
for i := 1 step 1 until 6:
r := 1/7cm*i;
@@ -267,7 +295,7 @@ This macro returns a \texttt{picture} of a weight of a specific \texttt{height}
draw (-1/8cm, 0)--(12cm, 0);
\end{lstlisting}
-\begin{mplibcode}
+\noindent\begin{mplibcode}
for i := 1 step 1 until 6:
draw weight.s(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
endfor;
@@ -284,7 +312,7 @@ This macro returns a \texttt{picture} of a weight of a specific \texttt{height}
draw (12cm, 0)--(-1/8cm, 0);
\end{lstlisting}
-\begin{mplibcode}
+\noindent\begin{mplibcode}
for i := 1 step 1 until 6:
draw weight.h(1/4cm + i*1/4cm) shifted (1/2cm*(i*(i+1))/2, 0);
endfor;
@@ -333,6 +361,22 @@ This macro returns a \texttt{picture} of a solid surface on the right side of a
\subsection{solid (\emph{path, angle, type})}
Fills given \texttt{path} with strokes of specific type at a given \texttt{angle}. \texttt{type} can be 0 (``solid'' strokes) and 1 (``glass'' strokes).
+\begin{lstlisting}
+ path p[];
+ p1 := unitsquare scaled 2cm;
+ p2 := p1 shifted (4cm, 0);
+ draw solid(p1, 45, 0);
+ draw solid(p2, -45, 1);
+\end{lstlisting}
+
+\begin{mplibcode}
+ path p[];
+ p1 := unitsquare scaled 2cm;
+ p2 := p1 shifted (4cm, 0);
+ draw solid(p1, 45, 0);
+ draw solid(p2, -45, 1);
+\end{mplibcode}
+
\subsection{woodBlock (\emph{width, height})}
Returns a \texttt{picture} of a rectangular block of wood with its bottom-left corner in the origin.
@@ -383,7 +427,7 @@ This macro returns a \texttt{picture} of the globe of specified \texttt{radius}
\end{mplibcode}
\subsection{Knots}
-There are two macros to handle knot drawing: \texttt{addStrandToKnot} and \texttt{knotFromStrands}. Currently the algorithm is not especially stable.
+There are two macros to handle knot drawing: \texttt{addStrandToKnot} and \texttt{knot\-From\-Strands}. Currently the algorithm is not especially stable.
\subsubsection{addStrandToKnot (\emph{knotName}) (\emph{path, ropeWidth, ropeType, intersectionOrder})}
This macro adds a strand to knot named \texttt{knotName} and returns nothing. Strand follows the given \texttt{path} and has a given \texttt{ropeWidth}. \texttt{ropeType} can be \texttt{"l"}, \texttt{"t"} (as in \texttt{tube.l} and \texttt{tube.t}) or \texttt{"e"} (for an unshaded strand). \texttt{intersectionOrder} is a string of comma separated numbers which represent a ``layer'' to which intersections along the strand go.
@@ -401,15 +445,15 @@ This macro returns a picture of a knot with a given \texttt{knotName}.
p3 := (fullcircle scaled 4cm);
addStrandToKnot (theknot) (p1 shifted (4cm, -4cm), 1/5cm, "l",
"-1,1,-1,1,-1,1,-1,1,-1");
- addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "l",
+ addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "s",
"");
- addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "l",
+ addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "e",
"-1,1");
draw knotFromStrands (theknot);
\end{lstlisting}
\begin{mplibcode}
- path p[]; path p[];
+ path p[];
p1 := (dir(90)*4/3cm) {dir(0)} .. tension 3/2
.. (dir(90 + 120)*4/3cm){dir(90 + 30)} .. tension 3/2
.. (dir(90 - 120)*4/3cm){dir(-90 - 30)} .. tension 3/2
@@ -418,13 +462,28 @@ This macro returns a picture of a knot with a given \texttt{knotName}.
p3 := (fullcircle scaled 4cm);
addStrandToKnot (theknot) (p1 shifted (4cm, -4cm), 1/5cm, "l",
"-1,1,-1,1,-1,1,-1,1,-1");
- addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "l",
+ addStrandToKnot (theknot) (p2 shifted (4cm, -4cm), 1/6cm, "s",
"");
- addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "l",
+ addStrandToKnot (theknot) (p3 shifted (4cm, -4cm), 1/7cm, "e",
"-1,1");
draw knotFromStrands (theknot);
\end{mplibcode}
+\subsection{Other 3D contraptions}
+Some macros can be used to shade 3D polygons. Currently only flat surfaces re supported
+
+\subsubsection{flatSurface\emph{\#@}(\emph{surface outline path, normal vector, hatch angle})}
+This macro returns a \texttt{picture} of a flat surface with the specified \texttt{surface outline path} with the given \texttt{normal vector}, illuminated from the direction determined by \texttt{lightDirection} and with hatches aligned at the andle \texttt{hatch angle}. If \texttt{\#@} is \texttt{".hatches"} then the surface is shaded with hatches, if it's \texttt{".stipples"}, then the surface is shaded with stipples.
+
+\begin{mplibcode}
+ path p[];
+ p1 := unitsquare xscaled 1cm yscaled 2cm;
+ p2 := p1 shifted (1cm, 0);
+ draw flatSurface.hatches(p1, (-1,0,1), 45);
+ draw flatSurface.hatches(p2, (1,0,1), 45);
+draw p1; draw p2;
+\end{mplibcode}
+
\section{Other macros}
Some macros that are not directly related to drawing are listed below
@@ -473,10 +532,10 @@ This macro returns a \texttt{path} that represent a section of a lens with given
Some macros that not related to physical problems at all are listed below.
\subsection{\emph{picture} maskedWith \emph{path}}
-This macro masks a part of a \texttt{picture} with closed \texttt{path}. In fact this is inversion of metapost's built-in \texttt{clip} but, in contrast to the latter, it does not modify original image. Note that it requires that counter-clockwise \texttt{path} to work properly.
+This macro masks a part of a \texttt{picture} with closed \texttt{path}. In fact this is inversion of \METAPOST's built-in \texttt{clip} but, in contrast to the latter, it does not modify original image. Note that it requires that counter-clockwise \texttt{path} to work properly.
\subsection{\emph{path} firstIntersectionTimes \emph{path}}
-This macro is similar to metapost's \texttt{intersectiontimes} but it returns intersection times with smallest time on first path.
+This macro is similar to \METAPOST's \texttt{intersectiontimes} but it returns intersection times with smallest time on first path.
\subsection{pathSubdivide \emph{path, n}}
This macro returns original \texttt{path} with \texttt{n}-times more points.
@@ -824,7 +883,7 @@ Line 3 describe initial setup. Line 4 is problem's solution. Lines 8--11 set all
.. (-4/3cm, 0) .. (-1/2cm, -2/3cm) .. (1/2cm, 0)
.. (-1/2cm, 3/2cm) .. cycle;
p2 := p2 scaled 6/5;
- addStrandToKnot (primeTwo) (p2, 1/4cm, "l", "1, -1, 1, -1, 1");
+ addStrandToKnot (primeTwo) (p2, 1/4cm, "s", "1, -1, 1, -1, 1");
draw knotFromStrands (primeTwo) shifted (4cm, -2cm);
p3 := (dir(0)*3/2cm) .. (dir(1*72)*2/3cm)
.. (dir(2*72)*3/2cm) .. (dir(3*72)*2/3cm)
@@ -833,7 +892,7 @@ Line 3 describe initial setup. Line 4 is problem's solution. Lines 8--11 set all
.. (dir(3*72)*3/2cm) .. (dir(4*72)*2/3cm)
.. cycle;
p3 := (p3 rotated (72/4)) scaled 6/5;
- addStrandToKnot (primeThree) (p3, 1/4cm, "l", "-1, 1, -1, 1, -1");
+ addStrandToKnot (primeThree) (p3, 1/4cm, "e", "-1, 1, -1, 1, -1");
draw knotFromStrands (primeThree) shifted (8cm, 0);
\end{mplibcode}