summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/profcollege/metapost/PfCSketch.mp
blob: 6a634cfc4dc7597a2de25488976afad96a59fb1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
%D'après https://tex.stackexchange.com/questions/39296/simulating-hand-drawn-lines

let normaldraw = draw ;
let normalfill = fill ;
let normaldrawarrow = drawarrow ;
let normaldbldrawarrow = drawdblarrow ;
let normalarrowhead = normalarrowhead;

path fullsquare;
fullsquare := unitsquare shifted - center unitsquare ;

%D The variable \type{sketch_amount} determines the amount of randomness in the
%D drawing
numeric sketch_amount; sketch_amount := 3;
numeric old_sketch_amount;
%D The macro \type{sketchdraw} randomized the path before drawing it. The 
%D \type{expr} ... \type{text} trick is copied from the definition of 
%D \type{drawarrow}

def sketchdraw expr p =
  do_sketchdraw(p if (path p): randomized sketch_amount fi)
enddef;

def do_sketchdraw(expr p) text t =
    normaldraw p t ;
enddef;

%cp
%%%%%%%%%%%%%%%%%%
vardef sketcharrowhead expr p =
  save q,e; path q; pair e;
  e = point length p of p;
  q = gobble(p shifted -e cutafter makepath(pencircle scaled 2ahlength))
    cuttings;
  ((q rotated .5ahangle & reverse q rotated -.5ahangle -- cycle)  shifted e) randomized (sketch_amount/3)
enddef;

path _apth;
def sketchdrawarrow expr p = _apth:=p randomized sketch_amount; _sketchfinarr enddef;
def sketchdrawdblarrow expr p = _apth:=p; _sketchfindarr enddef;

def _sketchfinarr text t =
  old_sketch_amount:=sketch_amount;
  sketch_amount:=0;
  draw _apth t;
  sketch_amount:=old_sketch_amount;
  filldraw sketcharrowhead _apth  t
enddef;

def _sketchfindarr text t =
  old_sketch_amount:=sketch_amount;
  sketch_amount:=0;
  draw _apth t;
    sketch_amount:=old_sketch_amount;
  filldraw sketcharrowhead _apth withpen currentpen  t;
  filldraw sketcharrowhead  reverse _apth  withpen currentpen  t
enddef;
%%%%%%%%%%%%%%%%%%

%D The macro \type{sketchfill} randomizes the path before filling it.
path _sketch_path_;

def sketchfill expr p =
  _sketch_path_ := p randomized sketch_amount;
   do_sketchfill  
enddef ;

def do_sketchfill text t =
    normalfill _sketch_path_ t ;
enddef ;

%D The macro \type{sketchypaths} is modeled after \type{visualizepaths} from
%D \filename{mp-tool}.

def sketchypaths =
  let draw = sketchdraw ;
  let fill = sketchfill ;
  %cp
  let drawarrow = sketchdrawarrow ;
  let drawdblarrow = sketchdrawdblarrow ;
  let arrowhead = sketcharrowhead ;
  %fin cp
enddef ;

%cp
def normalpaths =
  let draw = normaldraw ;
  let fill = normalfill ;
  let drawarrow = normaldrawarrow ;
  let drawdblarrow = normaldrawdblarrow ;
  let arrowhead = normalarrowhead;
enddef ;
 %fincp

%By metafun
vardef mfun_randomized_picture(expr p,s)(text rnd) =
    save currentpicture ;
    picture currentpicture ;
    currentpicture := nullpicture ;
    for i within p :
        addto currentpicture
            if stroked i :
                doublepath pathpart i rnd s
                dashed dashpart i
                withpen penpart i
                withcolor colorpart i
                withprescript prescriptpart i
                withpostscript postscriptpart i
            elseif filled i :
                contour pathpart i rnd s
                withpen penpart i
                withcolor colorpart i
                withprescript prescriptpart i
                withpostscript postscriptpart i
            else :
                also i
            fi
        ;
    endfor ;
    currentpicture
enddef ;

primarydef p randomized s = (
    if path p :
      for i=0 upto length(p)-1 :
	((point       i    of p) randomshifted s) .. controls
	  ((postcontrol i    of p) randomshifted s) and
	  ((precontrol (i+1) of p) randomshifted s) ..
      endfor
      if cycle p :
	cycle
      else :
	((point length(p) of p) randomshifted s)
      fi
    elseif pair p :
      p randomshifted s
    elseif cmykcolor p :
      if cmykcolor s :
	((uniformdeviate cyanpart    s) * cyanpart    p,
            (uniformdeviate magentapart s) * magentapart p,
            (uniformdeviate yellowpart  s) * yellowpart  p,
            (uniformdeviate blackpart   s) * blackpart   p)
      elseif pair s :
	((xpart s + (uniformdeviate (ypart s - xpart s))) * p)
      else :
	((uniformdeviate s) * p)
      fi
    elseif rgbcolor p :
      if rgbcolor s :
	((uniformdeviate redpart   s) * redpart   p,
            (uniformdeviate greenpart s) * greenpart p,
            (uniformdeviate bluepart  s) * bluepart  p)
      elseif pair s :
	((xpart s + (uniformdeviate (ypart s - xpart s))) * p)
      else :
	((uniformdeviate s) * p)
      fi
    elseif color p :
      if color s :
	((uniformdeviate greypart s) * greypart p)
      elseif pair s :
	((xpart s + (uniformdeviate (ypart s - xpart s))) * p)
      else :
	((uniformdeviate s) * p)
      fi
    elseif string p :
      (resolvedcolor(p)) randomized s
    elseif picture p :
      mfun_randomized_picture(p,s)(randomized)
    else :
      % p - s/2 + uniformdeviate s % would have been better but we want to be positive
      p + uniformdeviate s
    fi
    )
enddef ;
  
primarydef p randomshifted s =
  begingroup ;
    save ss ; pair ss ;
    ss := paired(s) ;
    p shifted (-.5xpart ss + uniformdeviate xpart ss,-.5ypart ss + uniformdeviate ypart ss)
  endgroup
enddef ;

def resolvedcolor(expr s) =
    .5white
enddef ;

vardef paired primary d =
  if pair d : d else : (d,d) fi
enddef ;

  extra_endfig := "picture retiens; retiens=currentpicture; draw (retiens randomized 1) withpen pencircle scaled 0.5 withcolor 0.25white;" & extra_endfig;

endinput