|
269 | 269 | }% |
270 | 270 |
|
271 | 271 |
|
| 272 | +% This handler converts each plot stream command into a curveto |
| 273 | +% command, except for the first, which is converted to the previously |
| 274 | +% specified action. The curveto control points are computed and adjusted |
| 275 | +% so that the resulting curve respects the monotonicity of the points. |
| 276 | +% If a point is a local extremum of the point set, it will also be |
| 277 | +% a local extremum of the generated curve. |
| 278 | +% This handler treats the x and y direction differently, and expects |
| 279 | +% x values to be monotonic. |
| 280 | +% See https://en.wikipedia.org/wiki/Monotone_cubic_interpolation for an |
| 281 | +% explanation of the algorithm. The last part uses \pgfpathcurveto |
| 282 | +% instead of using Hermitte base functions. |
| 283 | +\pgfdeclareplothandler{\pgfplothandlermonotonic}{}{% |
| 284 | + point macro=\pgf@plot@monotonic@dopointone, |
| 285 | + jump macro=\pgf@plot@monotonic@dojump, |
| 286 | + end macro=\pgf@plot@monotonic@doend |
| 287 | +}% |
| 288 | + |
| 289 | +\def\pgf@plot@monotonic@dojump{% |
| 290 | + \pgf@plot@monotonic@doend |
| 291 | + \global\pgf@plot@startedfalse |
| 292 | + \global\let\pgf@plotstreampoint\pgf@plot@monotonic@dopointone |
| 293 | +}% |
| 294 | + |
| 295 | +\def\pgf@plot@monotonic@dopointone#1{% |
| 296 | + \pgf@process{#1}% |
| 297 | + \pgf@plot@first@action{\pgfqpoint{\pgf@x}{\pgf@y}}% |
| 298 | + \xdef\pgf@plot@monotonic@pointone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}% |
| 299 | + \global\let\pgf@plotstreampoint=\pgf@plot@monotonic@dopointtwo |
| 300 | +}% |
| 301 | + |
| 302 | +\def\pgf@plot@monotonic@dopointtwo#1{% |
| 303 | + \pgf@process{#1}% |
| 304 | + \xdef\pgf@plot@monotonic@pointtwo{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}% |
| 305 | + % compute vector: |
| 306 | + \pgf@xa=\pgf@x% |
| 307 | + \pgf@ya=\pgf@y% |
| 308 | + \pgf@process{\pgf@plot@monotonic@pointone}% |
| 309 | + \advance\pgf@xa by-\pgf@x |
| 310 | + \advance\pgf@ya by-\pgf@y |
| 311 | + % save left delta |
| 312 | + \xdef\pgf@plot@monotonic@deltaleft{\pgf@sys@tonumber\pgf@xa}% |
| 313 | + % compute slope |
| 314 | + \pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}% |
| 315 | + \global\let\pgf@plot@monotonic@rateleft\pgfmathresult |
| 316 | + % init tangent one |
| 317 | + \global\let\pgf@plot@monotonic@slopeone\pgf@plot@monotonic@rateleft |
| 318 | + % prepare for next step |
| 319 | + \global\let\pgf@plotstreampoint=\pgf@plot@monotonic@dopointthree |
| 320 | + \global\pgf@plot@startedtrue% |
| 321 | +}% |
| 322 | + |
| 323 | +\def\pgf@plot@monotonic@dopointthree#1{% |
| 324 | + \pgf@process{#1}% |
| 325 | + \xdef\pgf@plot@monotonic@pointthree{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}% |
| 326 | + % compute vector: |
| 327 | + \pgf@xa=\pgf@x% |
| 328 | + \pgf@ya=\pgf@y% |
| 329 | + \pgf@process{\pgf@plot@monotonic@pointtwo}% |
| 330 | + \advance\pgf@xa by-\pgf@x |
| 331 | + \advance\pgf@ya by-\pgf@y |
| 332 | + % save right delta |
| 333 | + \xdef\pgf@plot@monotonic@deltaright{\pgf@sys@tonumber\pgf@xa}% |
| 334 | + % compute slope |
| 335 | + \pgfmathdivide@{\the\pgf@ya}{\the\pgf@xa}% |
| 336 | + \global\let\pgf@plot@monotonic@rateright\pgfmathresult |
| 337 | + % compute tangent slope |
| 338 | + \pgfmathadd@{\pgf@plot@monotonic@rateleft}{\pgf@plot@monotonic@rateright}% |
| 339 | + \pgf@xa=\pgfmathresult pt\relax |
| 340 | + \pgf@xa=0.5\pgf@xa |
| 341 | + % Fix slopes & draw curve |
| 342 | + \pgf@plot@monotoniccurveto@fixdraw |
| 343 | + % Prepare next: |
| 344 | + \global\let\pgf@plot@monotonic@slopeone\pgf@plot@monotonic@slopetwo |
| 345 | + \global\let\pgf@plot@monotonic@pointone\pgf@plot@monotonic@pointtwo |
| 346 | + \global\let\pgf@plot@monotonic@pointtwo\pgf@plot@monotonic@pointthree |
| 347 | + \global\let\pgf@plot@monotonic@deltaleft\pgf@plot@monotonic@deltaright |
| 348 | + \global\let\pgf@plot@monotonic@rateleft\pgf@plot@monotonic@rateright |
| 349 | +}% |
| 350 | + |
| 351 | +\def\pgf@plot@monotoniccurveto@fixdraw{% |
| 352 | + % fix extremums |
| 353 | + % \pgf@xa is slope two |
| 354 | + \pgf@ya=\pgf@plot@monotonic@rateleft pt\relax |
| 355 | + \pgf@yb=\pgf@plot@monotonic@rateright\pgf@ya |
| 356 | + \ifdim\pgf@yb<0.0001pt\relax |
| 357 | + \pgf@xa=0pt\relax |
| 358 | + \else |
| 359 | + % fix motonicity |
| 360 | + \pgf@ya=3\pgf@ya |
| 361 | + \ifdim\pgf@xa>0pt\relax |
| 362 | + \ifdim\pgf@xa>\pgf@ya \pgf@xa=\pgf@ya\fi |
| 363 | + \else |
| 364 | + \ifdim\pgf@xa<\pgf@ya \pgf@xa=\pgf@ya\fi |
| 365 | + \fi |
| 366 | + \pgf@ya=\pgf@plot@monotonic@rateright pt\relax |
| 367 | + \pgf@ya=3\pgf@ya |
| 368 | + \ifdim\pgf@xa>0pt\relax |
| 369 | + \ifdim\pgf@xa>\pgf@ya \pgf@xa=\pgf@ya\fi |
| 370 | + \else |
| 371 | + \ifdim\pgf@xa<\pgf@ya \pgf@xa=\pgf@ya\fi |
| 372 | + \fi |
| 373 | + \fi |
| 374 | + \xdef\pgf@plot@monotonic@slopetwo{\pgf@sys@tonumber\pgf@xa}% |
| 375 | + % compute points for left bezier |
| 376 | + % (\pgf@x,\pgf@y) is point two |
| 377 | + \pgf@ya=\pgf@plot@monotonic@deltaleft pt\relax |
| 378 | + \pgf@ya=0.333333\pgf@ya |
| 379 | + \advance \pgf@x by -\pgf@ya |
| 380 | + \advance \pgf@y by -\pgf@plot@monotonic@slopetwo\pgf@ya |
| 381 | + \xdef\pgf@plot@monotonic@supporttwo{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}% |
| 382 | + \pgf@process{\pgf@plot@monotonic@pointone}% |
| 383 | + \advance \pgf@x by \pgf@ya |
| 384 | + \advance \pgf@y by \pgf@plot@monotonic@slopeone\pgf@ya |
| 385 | + \xdef\pgf@plot@monotonic@supportone{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}% |
| 386 | + % draw left curve: |
| 387 | + \pgfpathcurveto{\pgf@plot@monotonic@supportone}{\pgf@plot@monotonic@supporttwo}{\pgf@plot@monotonic@pointtwo}% |
| 388 | +}% |
| 389 | + |
| 390 | +\def\pgf@plot@monotonic@doend{% |
| 391 | + \ifpgf@plot@started |
| 392 | + % fixdraw needs (\pgf@x,\pgf@y) as point two |
| 393 | + % and \pgf@xa as slope two |
| 394 | + \pgf@process{\pgf@plot@monotonic@pointtwo}% |
| 395 | + \pgf@xa=\pgf@plot@monotonic@rateleft pt\relax |
| 396 | + \pgf@plot@monotoniccurveto@fixdraw |
| 397 | + \fi |
| 398 | +}% |
| 399 | + |
| 400 | + |
272 | 401 |
|
273 | 402 |
|
274 | 403 |
|
|
0 commit comments