From 64b5a634f0679d68b9f3e2e228187c0cd6bfd34e Mon Sep 17 00:00:00 2001 From: jemmybutton Date: Sun, 2 Jul 2023 15:14:57 +0300 Subject: [PATCH] added invertedLight global variable --- fiziko.mkiv | 15 ++++++++++++++- fiziko.mp | 40 +++++++++++++++++++++++++++++++--------- fiziko.tex | 21 +++++++++++++++++++-- 3 files changed, 64 insertions(+), 12 deletions(-) diff --git a/fiziko.mkiv b/fiziko.mkiv index b44c750..978d56a 100644 --- a/fiziko.mkiv +++ b/fiziko.mkiv @@ -44,7 +44,7 @@ input fiziko.mp; \startdocument[ author=Sergey Slyusarev, - title={\type{fiziko}\par v.0.2.0 package for \MetaPost}, + title={\type{fiziko}\par v.0.2.1 package for \MetaPost}, abstract={This document describes a bunch of macros provided by the \quotation{fiziko} library for \MetaPost.}, license={This document is distributed under the CC-BY-SA 4.0 license\par\symbol[cc][by]~\symbol[cc][sa]}, url={https://github.com/jemmybutton/fiziko}, @@ -77,6 +77,19 @@ This variable controls direction from which light falls on shaded objects. It’ lightDirection := (-1/8pi, 1/8pi); \stopMP +\subsection{lightDirection} +This variable determines whether the light is inverted. This comes in handy when you need to have your shaded objects white on black. It's of \type{boolean} type. Default value is false: + +\def\BufferName{lightDirection} +\startbuffer[\BufferName] +draw sphere.c(1cm); +invertedLight := true; +fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black; +draw sphere.c(1cm) shifted (3/2cm, 0) withcolor white; +\stopbuffer +\typebuffer[\BufferName] \processMPbuffer[\BufferName] + + \section{\quotation{Lower level} macros} Currently, algorithms are quite stupid and will produce decent results only in certain simple circumstances. diff --git a/fiziko.mp b/fiziko.mp index f492b58..38ed7b4 100644 --- a/fiziko.mp +++ b/fiziko.mp @@ -1,6 +1,6 @@ -% fiziko 0.2.0 +% fiziko 0.2.1 % MetaPost library for physics textbook illustrations -% Copyright 2022 Sergey Slyusarev +% Copyright 2023 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 @@ -358,9 +358,10 @@ enddef; % vardef offsetPathGenerate (expr p, q, i) = - save returnPath, c, d, pl, ps; + save returnPath, c, d, a, pl, ps; path returnPath, pl[]; pair c[], d[]; + numeric a[]; c1 := precontrol i of p; c2 := point i of p; c3 := postcontrol i of p; @@ -401,8 +402,18 @@ vardef offsetPathGenerate (expr p, q, i) = and (abs(c1-c4)>0) and (abs(direction i of q) > 0): c6 := c4 shifted (unitvector(c4 - c3) rotated 90 scaled ypart(point i + 1 of q)); - c7 := (c2 - c1) scaled (abs(c5-c6)/abs(c1-c4)) rotated angle(direction i of q) shifted c5; - c8 := (c3 - c4) scaled (abs(c5-c6)/abs(c1-c4)) rotated angle(direction i + 1 of q) shifted c6; + %if abs(direction i of q) > 0: + a1 := angle(direction i of q); + %else: + % a1 := angle((point (i + 1/10) of q) - (point (i - 1/10) of q)); + %fi; + %if abs(direction i + 1 of q) > 0: + a2 := angle(direction i + 1 of q); + %else: + % a2 := angle((point (i + 1 + 1/10) of q) - (point (i + 1 - 1/10) of q)); + %fi; + c7 := (c2 - c1) scaled (abs(c5-c6)/abs(c1-c4)) rotated a1 shifted c5; + c8 := (c3 - c4) scaled (abs(c5-c6)/abs(c1-c4)) rotated a2 shifted c6; returnPath := returnPath .. controls c7 and c8 .. offsetPathGenerate (p, q, i + 1); else: returnPath := returnPath -- offsetPathGenerate (p, q, i + 1); @@ -1027,6 +1038,12 @@ enddef; % Once we get two angles at some point of some surface, we can compute light intensity there. % +% In case you need to have your shaded objects white-on-black, +% you can just switch invertedLight invertedLight to 'true' + +boolean invertedLight; +invertedLight := false; + vardef normalVectorToLightness (expr normalVector, d, q) = save returnValue, shiftedShadowPath; path shiftedShadowPath; @@ -1045,10 +1062,15 @@ vardef normalVectorToLightness (expr normalVector, d, q) = returnValue := lightnessPP(returnValue); fi; if returnValue > 1: - 1 - else: - returnValue - fi + returnValue := 1; + fi; + if returnValue < 0: + returnValue := 0; + fi; + if invertedLight: + returnValue := 1 - returnValue; + fi; + returnValue enddef; vardef lightnessPP (expr v) = diff --git a/fiziko.tex b/fiziko.tex index f30b648..0a3f592 100644 --- a/fiziko.tex +++ b/fiziko.tex @@ -12,7 +12,7 @@ } \author{Sergey Slyusarev} -\title{``fiziko'' v. 0.2.0 package for \METAPOST} +\title{``fiziko'' v. 0.2.1 package for \METAPOST} \begin{document} \maketitle @@ -53,11 +53,28 @@ \subsection{minStrokeWidth} \subsection{lightDirection} This variable controls direction from which light falls on shaded objects. It's of \texttt{pair} type and is set in radians. Default direction is top-left: - \begin{lstlisting} lightDirection := (-1/8pi, 1/8pi); \end{lstlisting} + +\subsection{invertedLight} +This variable determines whether the light is inverted. This comes in handy when you need to have your shaded objects white on black. It's of \texttt{boolean} type. Default value is false: + +\begin{lstlisting} +draw sphere.s(1cm); +invertedLight := true; +fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black; +draw sphere.s(1cm) shifted (3/2cm, 0) withcolor white; +\end{lstlisting} + +\begin{mplibcode} +draw sphere.c(1cm); +invertedLight := true; +fill ((unitsquare shifted (-1/2,-1/2)) scaled 3/2cm) shifted (3/2cm, 0) withcolor black; +draw sphere.c(1cm) shifted (3/2cm, 0) withcolor white; +\end{mplibcode} + \section{``Lower level'' macros} Currently, algorithms are quite stupid and will produce decent results only in certain simple circumstances.