| 1 |
local _version = '2.12.0' |
| 2 |
|
| 3 |
if tikz |
| 4 |
then |
| 5 |
if tikz.version ~= _version |
| 6 |
then |
| 7 |
error( 'version chaos' ) |
| 8 |
end |
| 9 |
return |
| 10 |
end |
| 11 |
|
| 12 |
tikz = { version = _version } |
| 13 |
|
| 14 |
--==========================================================================-- |
| 15 |
-- DEFINES |
| 16 |
--==========================================================================-- |
| 17 |
|
| 18 |
-- directions |
| 19 |
tikz.north = 'north' |
| 20 |
tikz.north_east = 'north east' |
| 21 |
tikz.east = 'east' |
| 22 |
tikz.south_east = 'south east' |
| 23 |
tikz.south = 'south' |
| 24 |
tikz.south_west = 'south west' |
| 25 |
tikz.west = 'west' |
| 26 |
tikz.north_west = 'north west' |
| 27 |
|
| 28 |
-- colors |
| 29 |
tikz.black = 'black' |
| 30 |
tikz.blue = 'blue' |
| 31 |
tikz.brown = 'brown' |
| 32 |
tikz.cyan = 'cyan' |
| 33 |
tikz.darkgray = 'darkgray' |
| 34 |
tikz.gray = 'gray' |
| 35 |
tikz.green = 'green' |
| 36 |
tikz.lightgray = 'lightgray' |
| 37 |
tikz.lime = 'lime' |
| 38 |
tikz.magenta = 'magenta' |
| 39 |
tikz.olive = 'olive' |
| 40 |
tikz.orange = 'orange' |
| 41 |
tikz.pink = 'pink' |
| 42 |
tikz.purple = 'purple' |
| 43 |
tikz.red = 'red' |
| 44 |
tikz.teal = 'teal' |
| 45 |
tikz.violet = 'violet' |
| 46 |
tikz.yellow = 'yellow' |
| 47 |
tikz.white = 'white' |
| 48 |
|
| 49 |
-- align |
| 50 |
tikz.center = 'center' |
| 51 |
tikz.left = 'left' |
| 52 |
tikz.right = 'right' |
| 53 |
|
| 54 |
tikz.none = 'none' |
| 55 |
|
| 56 |
-- line caps |
| 57 |
tikz.round = 'round' |
| 58 |
|
| 59 |
--==========================================================================-- |
| 60 |
-- HELPERS -- |
| 61 |
--==========================================================================-- |
| 62 |
|
| 63 |
-- if defined writes output here |
| 64 |
local outfile |
| 65 |
|
| 66 |
-- |
| 67 |
-- Prints arguments to tex and to stdout. |
| 68 |
-- |
| 69 |
local function tprint( ... ) |
| 70 |
local args = {...} |
| 71 |
print( table.concat( args ) ) |
| 72 |
|
| 73 |
if tex |
| 74 |
then |
| 75 |
tex.print( table.concat( args ) ) |
| 76 |
end |
| 77 |
|
| 78 |
if outfile |
| 79 |
then |
| 80 |
outfile:write( table.concat( args ) ) |
| 81 |
outfile:write( '\n%%% DO NOT EDIT THIS FILE %%%\n' ) |
| 82 |
end |
| 83 |
end |
| 84 |
tikz.tprint = tprint |
| 85 |
|
| 86 |
-- |
| 87 |
-- Flattens an object stack. |
| 88 |
-- |
| 89 |
-- The content of all numerated unclassed objects |
| 90 |
-- are put into a copy of obj. |
| 91 |
-- |
| 92 |
local function flatten( o ) |
| 93 |
local r = { } |
| 94 |
for _, v in ipairs( o ) |
| 95 |
do |
| 96 |
if type( v ) == 'table' and getmetatable( v ) == nil |
| 97 |
then |
| 98 |
local fv = flatten( v ) |
| 99 |
for _, fvv in ipairs( fv ) |
| 100 |
do |
| 101 |
table.insert( r, fvv ) |
| 102 |
end |
| 103 |
for k, vv in pairs( fv ) |
| 104 |
do |
| 105 |
if type( k ) ~= 'number' |
| 106 |
then |
| 107 |
r[ k ] = vv |
| 108 |
end |
| 109 |
end |
| 110 |
else |
| 111 |
table.insert( r, v ) |
| 112 |
end |
| 113 |
end |
| 114 |
-- copies (and overwrites) all named attributes |
| 115 |
for k, v in pairs( o ) |
| 116 |
do |
| 117 |
if type( k ) ~= 'number' |
| 118 |
then |
| 119 |
r[ k ] = v |
| 120 |
end |
| 121 |
end |
| 122 |
return r |
| 123 |
end |
| 124 |
|
| 125 |
-- |
| 126 |
-- A key marking an object having a zString function. |
| 127 |
-- |
| 128 |
local _zString = { } |
| 129 |
|
| 130 |
-- |
| 131 |
-- Converts everything to a tikz string. |
| 132 |
-- |
| 133 |
local function zString( v ) |
| 134 |
if type( v ) == 'table' and v[ _zString ] |
| 135 |
then |
| 136 |
return v[ _zString ]( v ) |
| 137 |
end |
| 138 |
return tostring( v ) |
| 139 |
end |
| 140 |
|
| 141 |
-- |
| 142 |
-- Creates an immutable class. |
| 143 |
-- |
| 144 |
local function immutable( definer ) |
| 145 |
local def = { } |
| 146 |
local lazy = { } |
| 147 |
local proto = { } |
| 148 |
def.lazy = lazy |
| 149 |
def.proto = proto |
| 150 |
definer( def ) -- the callee fills in data |
| 151 |
local k = { } -- key the hidden table |
| 152 |
|
| 153 |
local mt = { } |
| 154 |
mt.__index = |
| 155 |
function( self, key ) |
| 156 |
if key == 'id' then return def.id end |
| 157 |
|
| 158 |
-- first check if native value |
| 159 |
local v = self[ k ][ key ] |
| 160 |
if v then return v end |
| 161 |
|
| 162 |
-- then if a lazy function |
| 163 |
local f = lazy[ key ] |
| 164 |
if f |
| 165 |
then |
| 166 |
local v = f( self ) |
| 167 |
self[ k ][ key ] = v -- cache |
| 168 |
return v |
| 169 |
end |
| 170 |
|
| 171 |
-- then if a proto value |
| 172 |
v = proto[ key ] |
| 173 |
if v |
| 174 |
then |
| 175 |
if type( v ) == 'function' |
| 176 |
then |
| 177 |
return( |
| 178 |
function(...) |
| 179 |
return v( self, table.unpack( {...} ) ) |
| 180 |
end |
| 181 |
) |
| 182 |
else |
| 183 |
return v |
| 184 |
end |
| 185 |
end |
| 186 |
|
| 187 |
-- otherwise |
| 188 |
-- error( 'invalid property' ) |
| 189 |
end |
| 190 |
|
| 191 |
mt.__newindex = |
| 192 |
function( ) |
| 193 |
error( 'this is an immutable' ) |
| 194 |
end |
| 195 |
|
| 196 |
if def.add |
| 197 |
then |
| 198 |
mt.__add = def.add |
| 199 |
end |
| 200 |
|
| 201 |
if def.concat |
| 202 |
then |
| 203 |
mt.__concat = def.concat |
| 204 |
end |
| 205 |
|
| 206 |
if def.div |
| 207 |
then |
| 208 |
mt.__div = def.div |
| 209 |
end |
| 210 |
|
| 211 |
if def.mul |
| 212 |
then |
| 213 |
mt.__mul = def.mul |
| 214 |
end |
| 215 |
|
| 216 |
if def.sub |
| 217 |
then |
| 218 |
mt.__sub = def.sub |
| 219 |
end |
| 220 |
|
| 221 |
if def.tostring |
| 222 |
then |
| 223 |
mt.__tostring = def.tostring |
| 224 |
end |
| 225 |
|
| 226 |
return function(...) |
| 227 |
local args = {...} |
| 228 |
local nt = def.constructor( table.unpack( args ) ) |
| 229 |
local o = { [ k ] = nt, [ _zString ] = def.zString } |
| 230 |
setmetatable( o, mt ) |
| 231 |
return o |
| 232 |
end |
| 233 |
end |
| 234 |
tikz.immutable = immutable |
| 235 |
|
| 236 |
-- |
| 237 |
-- writes out direct options |
| 238 |
-- |
| 239 |
local function _opts( s, _opts, haveOpts ) |
| 240 |
if not _opts |
| 241 |
then |
| 242 |
return haveOpts |
| 243 |
end |
| 244 |
|
| 245 |
for k, v in pairs( _opts ) |
| 246 |
do |
| 247 |
if not haveOpts |
| 248 |
then |
| 249 |
table.insert( s, '[' ) |
| 250 |
else |
| 251 |
table.insert( s, ',' ) |
| 252 |
end |
| 253 |
haveOpts = true |
| 254 |
if type( k ) == 'number' |
| 255 |
then |
| 256 |
table.insert( s, v ) |
| 257 |
else |
| 258 |
table.insert( s, k..'='..v ) |
| 259 |
end |
| 260 |
end |
| 261 |
return haveOpts |
| 262 |
end |
| 263 |
|
| 264 |
------------------------------------------------------------------------------- |
| 265 |
-- Point. |
| 266 |
------------------------------------------------------------------------------- |
| 267 |
tikz.p = |
| 268 |
immutable( function( def ) |
| 269 |
def.id = 'point' |
| 270 |
|
| 271 |
def.add = |
| 272 |
function( left, right ) |
| 273 |
if right.id == 'line' |
| 274 |
or right.id == 'bezier3' |
| 275 |
then |
| 276 |
return right + left |
| 277 |
end |
| 278 |
|
| 279 |
return tikz.p{ left.x + right.x, left.y + right.y } |
| 280 |
end |
| 281 |
|
| 282 |
def.concat = |
| 283 |
function( left, right ) |
| 284 |
return zString( left )..zString( right ) |
| 285 |
end |
| 286 |
|
| 287 |
-- constructor |
| 288 |
def.constructor = |
| 289 |
function( args ) |
| 290 |
return { x = args[ 1 ], y = args[ 2 ] } |
| 291 |
end |
| 292 |
|
| 293 |
-- divisor |
| 294 |
def.div = |
| 295 |
function( left, right ) |
| 296 |
return tikz.p{ left.x / right, left.y / right } |
| 297 |
end |
| 298 |
|
| 299 |
def.mul = |
| 300 |
function( left, right ) |
| 301 |
if type( right ) == 'number' |
| 302 |
then |
| 303 |
return tikz.p{ left.x * right, left.y * right } |
| 304 |
elseif type( left ) == 'number' |
| 305 |
then |
| 306 |
return tikz.p{ left * right.x, left * right.y } |
| 307 |
elseif type( right ) == 'table' |
| 308 |
then |
| 309 |
if right.id == 'point' |
| 310 |
then |
| 311 |
return tikz.p{ left.x * right.x, left.y * right.y } |
| 312 |
end |
| 313 |
end |
| 314 |
error( 'invalid operation' ) |
| 315 |
end |
| 316 |
|
| 317 |
def.sub = |
| 318 |
function( left, right ) |
| 319 |
return tikz.p{ left.x - right.x, left.y - right.y } |
| 320 |
end |
| 321 |
|
| 322 |
def.tostring = |
| 323 |
function( self ) |
| 324 |
return 'p{ '..self.x..', '..self.y..' }' |
| 325 |
end |
| 326 |
|
| 327 |
-- converts to a tikz string. |
| 328 |
def.zString = |
| 329 |
function( self ) |
| 330 |
return '('..self.x..','..self.y..')' |
| 331 |
end |
| 332 |
end ) |
| 333 |
|
| 334 |
------------------------------------------------------------------------------- |
| 335 |
-- A full circle. |
| 336 |
------------------------------------------------------------------------------- |
| 337 |
tikz.circle = |
| 338 |
immutable( function( def ) |
| 339 |
def.id = 'circle' |
| 340 |
|
| 341 |
-- constructor |
| 342 |
def.constructor = |
| 343 |
function( args ) |
| 344 |
return { |
| 345 |
at = args.at, |
| 346 |
radius = args.radius, |
| 347 |
} |
| 348 |
end |
| 349 |
|
| 350 |
-- converts to a tikz string. |
| 351 |
def.zString = |
| 352 |
function( self ) |
| 353 |
return zString( self.at )..' circle ('.. self.radius .. ')' |
| 354 |
end |
| 355 |
end ) |
| 356 |
|
| 357 |
------------------------------------------------------------------------------- |
| 358 |
-- A quadratic bezier curve. |
| 359 |
------------------------------------------------------------------------------- |
| 360 |
tikz.bezier2 = |
| 361 |
immutable( function( def ) |
| 362 |
def.id = 'bezier2' |
| 363 |
|
| 364 |
-- constructor |
| 365 |
def.constructor = |
| 366 |
function( args ) |
| 367 |
local p1 = args.p1 |
| 368 |
local pc = args.pc |
| 369 |
local p2 = args.p2 |
| 370 |
|
| 371 |
return { |
| 372 |
p1 = p1, |
| 373 |
pc = pc, |
| 374 |
p2 = p2, |
| 375 |
} |
| 376 |
end |
| 377 |
|
| 378 |
-- draws helping information |
| 379 |
def.proto.drawHelpers = |
| 380 |
function( self, prefix ) |
| 381 |
tikz.draw{ draw = 'red', tikz.line{ self.p1, self.pc } } |
| 382 |
tikz.draw{ draw = 'blue', tikz.line{ self.pc, self.p2 } } |
| 383 |
if prefix ~= nil |
| 384 |
then |
| 385 |
tikz.put{ tikz.node{ |
| 386 |
at = self.p1, |
| 387 |
anchor = 'north west', |
| 388 |
text = prefix .. '1', |
| 389 |
} } |
| 390 |
tikz.put{ tikz.node{ |
| 391 |
at = self.pc, |
| 392 |
anchor = 'north west', |
| 393 |
text = prefix .. 'c', |
| 394 |
} } |
| 395 |
tikz.put{ tikz.node{ |
| 396 |
at = self.p2, |
| 397 |
anchor = 'north west', |
| 398 |
text = prefix .. '2', |
| 399 |
} } |
| 400 |
end |
| 401 |
end |
| 402 |
|
| 403 |
-- converts to a tikz string. |
| 404 |
def.zString = |
| 405 |
function( self ) |
| 406 |
local s = { } |
| 407 |
table.insert( s, zString( self.p1 ) ) |
| 408 |
table.insert( s, '.. controls' ) |
| 409 |
table.insert( s, zString( self.pc ) ) |
| 410 |
table.insert( s, '..' ) |
| 411 |
table.insert( s, zString( self.p2 ) ) |
| 412 |
return table.concat( s, ' ' ) |
| 413 |
end |
| 414 |
end ) |
| 415 |
|
| 416 |
------------------------------------------------------------------------------- |
| 417 |
-- A cubic bezier curve. |
| 418 |
------------------------------------------------------------------------------- |
| 419 |
tikz.bezier3 = |
| 420 |
immutable( function( def ) |
| 421 |
def.id = 'bezier3' |
| 422 |
|
| 423 |
def.add = |
| 424 |
function( left, right ) |
| 425 |
if right.id == 'point' |
| 426 |
then |
| 427 |
return tikz.bezier3{ |
| 428 |
p1 = left.p1 + right, |
| 429 |
pc1 = left.pc1 + right, |
| 430 |
pc2 = left.pc2 + right, |
| 431 |
p2 = left.p2 + right, |
| 432 |
} |
| 433 |
end |
| 434 |
error( 'unknown operation' ) |
| 435 |
end |
| 436 |
|
| 437 |
-- constructor |
| 438 |
def.constructor = |
| 439 |
function( args ) |
| 440 |
local p1 = args.p1 |
| 441 |
local pc1 = args.pc1 |
| 442 |
local pc2 = args.pc2 |
| 443 |
local p2 = args.p2 |
| 444 |
|
| 445 |
return { |
| 446 |
p1 = p1, |
| 447 |
pc1 = pc1, |
| 448 |
pc2 = pc2, |
| 449 |
p2 = p2, |
| 450 |
} |
| 451 |
end |
| 452 |
|
| 453 |
-- draws helping information |
| 454 |
def.proto.drawHelpers = |
| 455 |
function( self, prefix ) |
| 456 |
tikz.draw{ draw = 'red', tikz.line{ self.p1, self.pc1 } } |
| 457 |
tikz.draw{ draw = 'yellow', tikz.line{ self.pc1, self.pc2 } } |
| 458 |
tikz.draw{ draw = 'blue', tikz.line{ self.pc2, self.p2 } } |
| 459 |
if prefix ~= nil |
| 460 |
then |
| 461 |
tikz.put{ tikz.node{ |
| 462 |
at = self.p1, |
| 463 |
anchor = 'north west', |
| 464 |
text = prefix .. '1', |
| 465 |
} } |
| 466 |
tikz.put{ tikz.node{ |
| 467 |
at = self.pc1, |
| 468 |
anchor = 'north west', |
| 469 |
text = prefix .. 'c1', |
| 470 |
} } |
| 471 |
tikz.put{ tikz.node{ |
| 472 |
at = self.pc2, |
| 473 |
anchor = 'north west', |
| 474 |
text = prefix .. 'c2', |
| 475 |
} } |
| 476 |
tikz.put{ tikz.node{ |
| 477 |
at = self.p2, |
| 478 |
anchor = 'north west', |
| 479 |
text = prefix .. '2', |
| 480 |
} } |
| 481 |
end |
| 482 |
end |
| 483 |
|
| 484 |
def.mul = |
| 485 |
function( left, right ) |
| 486 |
if type( right ) == 'number' |
| 487 |
then |
| 488 |
return tikz.bezier3 { |
| 489 |
p1 = left.p1 * right, |
| 490 |
pc1 = left.pc1 * right, |
| 491 |
pc2 = left.pc2 * right, |
| 492 |
p2 = left.p2 * right, |
| 493 |
} |
| 494 |
elseif type( left ) == 'number' |
| 495 |
then |
| 496 |
return tikz.bezier3 { |
| 497 |
p1 = right.p1 * left, |
| 498 |
pc1 = right.pc1 * left, |
| 499 |
pc2 = right.pc2 * left, |
| 500 |
p2 = right.p2 * left, |
| 501 |
} |
| 502 |
else |
| 503 |
error( 'invalid operation' ) |
| 504 |
end |
| 505 |
end |
| 506 |
|
| 507 |
-- point in center |
| 508 |
def.lazy.pc = |
| 509 |
function( self ) |
| 510 |
return self.pt( 0.5 ) |
| 511 |
end |
| 512 |
|
| 513 |
-- gets angle at t (0-1) |
| 514 |
def.proto.phit = |
| 515 |
function( self, t ) |
| 516 |
local p1 = self.p1 |
| 517 |
local pc1 = self.pc1 |
| 518 |
local pc2 = self.pc2 |
| 519 |
local p2 = self.p2 |
| 520 |
|
| 521 |
local u = 1 - t |
| 522 |
local uu3 = 3 * u * u |
| 523 |
local ut6 = 6 * u * t |
| 524 |
local tt3 = 3 * t * t |
| 525 |
|
| 526 |
return math.atan2( |
| 527 |
-uu3 * p1.y + uu3 * pc1.y - ut6 * pc1.y + ut6 * pc2.y - tt3 * pc2.y + tt3 * p2.y, |
| 528 |
-uu3 * p1.x + uu3 * pc1.x - ut6 * pc1.x + ut6 * pc2.x - tt3 * pc2.x + tt3 * p2.x |
| 529 |
) |
| 530 |
end |
| 531 |
|
| 532 |
-- reverts p1 and p2 and pc1 and pc2 respectively |
| 533 |
def.lazy.revert = |
| 534 |
function( self ) |
| 535 |
return tikz.bezier3{ |
| 536 |
p1 = self.p2, |
| 537 |
pc1 = self.pc2, |
| 538 |
pc2 = self.pc1, |
| 539 |
p2 = self.p1, |
| 540 |
} |
| 541 |
end |
| 542 |
|
| 543 |
-- gets point at t (0-1) |
| 544 |
def.proto.pt = |
| 545 |
function( self, t ) |
| 546 |
if type( t ) ~= 'number' or t < 0 or t > 1 |
| 547 |
then |
| 548 |
error( 'invalid pt' ) |
| 549 |
end |
| 550 |
|
| 551 |
local p1 = self.p1 |
| 552 |
local pc1 = self.pc1 |
| 553 |
local pc2 = self.pc2 |
| 554 |
local p2 = self.p2 |
| 555 |
local t1 = 1 - t |
| 556 |
|
| 557 |
return( |
| 558 |
tikz.p{ |
| 559 |
t1^3*p1.x + 3*t*t1^2*pc1.x + 3 *t^2*t1*pc2.x + t^3*p2.x, |
| 560 |
t1^3*p1.y + 3*t*t1^2*pc1.y + 3 *t^2*t1*pc2.y + t^3*p2.y |
| 561 |
} |
| 562 |
) |
| 563 |
end |
| 564 |
|
| 565 |
def.sub = |
| 566 |
function( left, right ) |
| 567 |
if right.id == 'point' |
| 568 |
then |
| 569 |
return tikz.bezier3{ |
| 570 |
p1 = left.p1 - right, |
| 571 |
pc1 = left.pc1 - right, |
| 572 |
pc2 = left.pc2 - right, |
| 573 |
p2 = left.p2 - right, |
| 574 |
} |
| 575 |
end |
| 576 |
error( 'unknown operation' ) |
| 577 |
end |
| 578 |
|
| 579 |
-- converts to a tikz string. |
| 580 |
def.zString = |
| 581 |
function( self ) |
| 582 |
local s = { } |
| 583 |
table.insert( s, zString( self.p1 ) ) |
| 584 |
table.insert( s, '.. controls' ) |
| 585 |
table.insert( s, zString( self.pc1 ) ) |
| 586 |
table.insert( s, 'and' ) |
| 587 |
table.insert( s, zString( self.pc2 ) ) |
| 588 |
table.insert( s, '..' ) |
| 589 |
table.insert( s, zString( self.p2 ) ) |
| 590 |
return table.concat( s, ' ' ) |
| 591 |
end |
| 592 |
end ) |
| 593 |
|
| 594 |
------------------------------------------------------------------------------- |
| 595 |
-- A bend line |
| 596 |
------------------------------------------------------------------------------- |
| 597 |
tikz.bline = |
| 598 |
immutable( function( def ) |
| 599 |
def.id = 'bline' |
| 600 |
|
| 601 |
-- moves the line. |
| 602 |
def.add = |
| 603 |
function( left, right ) |
| 604 |
if right.id == 'point' |
| 605 |
then |
| 606 |
return tikz.line{ p1 = left.p1 + right, p2 = left.p2 + right } |
| 607 |
end |
| 608 |
error( 'unknown operation' ) |
| 609 |
end |
| 610 |
|
| 611 |
-- constructor |
| 612 |
-- |
| 613 |
-- either |
| 614 |
-- { p1, p2 }, (a list ) |
| 615 |
-- { p1 = ..., p2 = ... }, |
| 616 |
-- { pc = ..., phi = ..., length = ... }, |
| 617 |
def.constructor = |
| 618 |
function( args ) |
| 619 |
local p1 |
| 620 |
local p2 |
| 621 |
|
| 622 |
if #args > 1 |
| 623 |
then |
| 624 |
-- list |
| 625 |
p1 = args[ 1 ] |
| 626 |
p2 = args[ 2 ] |
| 627 |
elseif type( args.p1 ) ~= 'nil' and type( args.p2 ) ~= 'nil' |
| 628 |
then |
| 629 |
p1 = args.p1 |
| 630 |
p2 = args.p2 |
| 631 |
elseif args.phi ~= nil |
| 632 |
then |
| 633 |
local phi = args.phi |
| 634 |
if args.pc ~= nil |
| 635 |
then |
| 636 |
if args.p1 ~= nil |
| 637 |
or args.p2 ~= nil |
| 638 |
then |
| 639 |
error( 'invalid line options', 2 ) |
| 640 |
end |
| 641 |
local pc = args.pc |
| 642 |
local len05 = args.length / 2 |
| 643 |
p1 = pc + tikz.p{ math.cos( phi ), math.sin( phi ) } * len05 |
| 644 |
p2 = pc - tikz.p{ math.cos( phi ), math.sin( phi ) } * len05 |
| 645 |
elseif type( args.p1 ) ~= 'nil' |
| 646 |
then |
| 647 |
if args.pc ~= nil |
| 648 |
or args.p2 ~= nil |
| 649 |
then |
| 650 |
error( 'invalid line options', 2 ) |
| 651 |
end |
| 652 |
p1 = args.p1 |
| 653 |
p2 = p1 + tikz.p{ math.cos( phi ), math.sin( phi ) } * args.length |
| 654 |
else |
| 655 |
error( 'invalid line options', 2 ) |
| 656 |
end |
| 657 |
else |
| 658 |
error( 'invalid line options', 2 ) |
| 659 |
end |
| 660 |
|
| 661 |
return { |
| 662 |
bend_left = args.bend_left, |
| 663 |
bend_right = args.bend_right, |
| 664 |
p1 = p1, |
| 665 |
p2 = p2, |
| 666 |
} |
| 667 |
end |
| 668 |
|
| 669 |
-- converts to a cubic bezier (that should look the same) |
| 670 |
def.lazy.bezier3 = |
| 671 |
function( self ) |
| 672 |
local phi |
| 673 |
if self.bend_right ~= nil then |
| 674 |
phi = self.bend_right * math.pi / 180 |
| 675 |
else |
| 676 |
phi = -self.bend_left * math.pi / 180 |
| 677 |
end |
| 678 |
local line = self.line |
| 679 |
local phil = line.phi |
| 680 |
local len = line.length * 0.3915 |
| 681 |
local lt1 = |
| 682 |
tikz.line{ |
| 683 |
p1 = line.p1, |
| 684 |
phi = phil-phi, |
| 685 |
length = len |
| 686 |
} |
| 687 |
local lt2 = |
| 688 |
tikz.line{ |
| 689 |
p1 = line.p2, |
| 690 |
phi = phil+phi+math.pi, |
| 691 |
length = len |
| 692 |
} |
| 693 |
|
| 694 |
return tikz.bezier3{ |
| 695 |
p1 = line.p1, |
| 696 |
pc1 = lt1.p2, |
| 697 |
pc2 = lt2.p2, |
| 698 |
p2 = line.p2, |
| 699 |
} |
| 700 |
end |
| 701 |
|
| 702 |
-- converts to a straight line. |
| 703 |
def.lazy.line = |
| 704 |
function( self ) |
| 705 |
return tikz.line{ |
| 706 |
p1 = self.p1, |
| 707 |
p2 = self.p2, |
| 708 |
} |
| 709 |
end |
| 710 |
|
| 711 |
-- point in center |
| 712 |
def.lazy.pc = |
| 713 |
function( self ) |
| 714 |
return self.bezier3.pc |
| 715 |
end |
| 716 |
|
| 717 |
-- gets point at t (0-1). |
| 718 |
def.proto.pt = |
| 719 |
function( self, t ) |
| 720 |
return self.bezier3.pt( t ) |
| 721 |
end |
| 722 |
|
| 723 |
-- converts to a tikz string. |
| 724 |
def.zString = |
| 725 |
function( self ) |
| 726 |
local s = { } |
| 727 |
table.insert( s, zString( self.p1 ) ) |
| 728 |
table.insert( s, 'to' ) |
| 729 |
if self.bend_left ~= nil |
| 730 |
then |
| 731 |
table.insert( s, '[bend left='..self.bend_left..']' ) |
| 732 |
end |
| 733 |
if self.bend_right ~= nil |
| 734 |
then |
| 735 |
table.insert( s, '[bend right='..self.bend_right..']' ) |
| 736 |
end |
| 737 |
table.insert( s, zString( self.p2 ) ) |
| 738 |
return table.concat( s, ' ' ) |
| 739 |
end |
| 740 |
end ) |
| 741 |
|
| 742 |
------------------------------------------------------------------------------- |
| 743 |
-- A curve is defined by a list of points. |
| 744 |
------------------------------------------------------------------------------- |
| 745 |
curve = |
| 746 |
immutable( function( def ) |
| 747 |
def.id = 'curve' |
| 748 |
|
| 749 |
-- constructor |
| 750 |
def.constructor = |
| 751 |
function( args ) |
| 752 |
return { |
| 753 |
points = args.points, |
| 754 |
tension = args.tension, |
| 755 |
cycle = args.cycle, |
| 756 |
} |
| 757 |
end |
| 758 |
|
| 759 |
-- draws helping information |
| 760 |
def.proto.drawHelpers = |
| 761 |
function( self, prefix ) |
| 762 |
local points = self.points |
| 763 |
for i, pi in ipairs( points ) |
| 764 |
do |
| 765 |
tikz.draw{ |
| 766 |
fill = 'black', |
| 767 |
tikz.circle{ |
| 768 |
at = pi, |
| 769 |
radius = 0.05, |
| 770 |
} |
| 771 |
} |
| 772 |
if prefix ~= nil |
| 773 |
then |
| 774 |
tikz.put{ tikz.node{ |
| 775 |
at = pi, |
| 776 |
anchor = 'north west', |
| 777 |
text = prefix .. i, |
| 778 |
} } |
| 779 |
end |
| 780 |
end |
| 781 |
end |
| 782 |
|
| 783 |
-- converts to a tikz string. |
| 784 |
def.zString = |
| 785 |
function( self ) |
| 786 |
local s = { 'plot' } |
| 787 |
table.insert( s, '[' ) |
| 788 |
if self.cycle |
| 789 |
then |
| 790 |
table.insert( s, 'smooth cycle' ) |
| 791 |
else |
| 792 |
table.insert( s, 'smooth' ) |
| 793 |
end |
| 794 |
if self.tension then |
| 795 |
table.insert( s, ',tension='..self.tension ) |
| 796 |
end |
| 797 |
table.insert( s, ']' ) |
| 798 |
local points = self.points |
| 799 |
table.insert( s, 'coordinates' ) |
| 800 |
table.insert( s, '{' ) |
| 801 |
for _, p in ipairs( points ) |
| 802 |
do |
| 803 |
table.insert( s, zString( p ) ) |
| 804 |
end |
| 805 |
table.insert( s, '}' ) |
| 806 |
return table.concat( s, ' ' ) |
| 807 |
end |
| 808 |
end ) |
| 809 |
|
| 810 |
------------------------------------------------------------------------------- |
| 811 |
-- A full ellipse. |
| 812 |
------------------------------------------------------------------------------- |
| 813 |
tikz.ellipse = |
| 814 |
immutable( function( def ) |
| 815 |
def.id = 'ellipse' |
| 816 |
|
| 817 |
-- constructor |
| 818 |
def.constructor = |
| 819 |
function( args ) |
| 820 |
local at = args.at |
| 821 |
local xradius = args.xradius |
| 822 |
local yradius = args.yradius |
| 823 |
|
| 824 |
return { |
| 825 |
at = at, |
| 826 |
xradius = xradius, |
| 827 |
yradius = yradius, |
| 828 |
} |
| 829 |
end |
| 830 |
|
| 831 |
-- intersect with a line. |
| 832 |
def.proto.intersectLine = |
| 833 |
function( self, line ) |
| 834 |
local at = self.at |
| 835 |
local rx = self.xradius |
| 836 |
local ry = self.yradius |
| 837 |
local p1 = line.p1 |
| 838 |
local p2 = line.p2 |
| 839 |
|
| 840 |
local x0 = p1.x |
| 841 |
local y0 = p1.y |
| 842 |
local x1 = p2.x |
| 843 |
local y1 = p2.y |
| 844 |
local cx = at.x |
| 845 |
local cy = at.y |
| 846 |
|
| 847 |
x0 = x0 - cx |
| 848 |
y0 = y0 - cy |
| 849 |
x1 = x1 - cx |
| 850 |
y1 = y1 - cy |
| 851 |
|
| 852 |
local A = ((x1 - x0) * (x1 - x0)) / rx / rx + ((y1 - y0) * (y1 - y0)) / ry / ry |
| 853 |
local B = (2 * x0 * (x1 - x0)) / rx / rx + (2 * y0 * (y1 - y0)) / ry / ry |
| 854 |
local C = (x0 * x0) / rx / rx + (y0 * y0) / ry / ry - 1 |
| 855 |
local D = B * B - 4 * A * C |
| 856 |
|
| 857 |
local tv = { } |
| 858 |
if D == 0 |
| 859 |
then |
| 860 |
table.insert( tv, -B / 2 / A ) |
| 861 |
else |
| 862 |
table.insert( tv, (-B + math.sqrt(D)) / 2 / A ) |
| 863 |
table.insert( tv, (-B - math.sqrt(D)) / 2 / A ) |
| 864 |
end |
| 865 |
|
| 866 |
local r = { } |
| 867 |
for _, t in ipairs( tv ) |
| 868 |
do |
| 869 |
if t >= 0 and t <= 1 |
| 870 |
then |
| 871 |
table.insert( |
| 872 |
r, |
| 873 |
p1 + tikz.p{ t * line.length * math.cos( line.phi ), t * line.length * math.sin( line.phi ) } |
| 874 |
) |
| 875 |
end |
| 876 |
end |
| 877 |
|
| 878 |
if #r == 0 |
| 879 |
then |
| 880 |
return |
| 881 |
elseif #r == 1 |
| 882 |
then |
| 883 |
return r[ 1 ] |
| 884 |
else |
| 885 |
return r |
| 886 |
end |
| 887 |
end |
| 888 |
|
| 889 |
-- converts to a tikz string. |
| 890 |
def.zString = |
| 891 |
function( self ) |
| 892 |
s = { } |
| 893 |
table.insert( s, zString( self.at ) ) |
| 894 |
table.insert( s, 'ellipse' ) |
| 895 |
table.insert( s, '[' ) |
| 896 |
table.insert( s, 'x radius =' ) |
| 897 |
table.insert( s, zString( self.xradius ) ) |
| 898 |
table.insert( s, ', y radius =' ) |
| 899 |
table.insert( s, zString( self.yradius ) ) |
| 900 |
table.insert( s, ']' ) |
| 901 |
return table.concat( s, ' ' ) |
| 902 |
end |
| 903 |
end ) |
| 904 |
|
| 905 |
------------------------------------------------------------------------------- |
| 906 |
-- An ellipse part. |
| 907 |
------------------------------------------------------------------------------- |
| 908 |
tikz.ellipseArc = |
| 909 |
immutable( function( def ) |
| 910 |
def.id = 'ellipseArc' |
| 911 |
|
| 912 |
-- constructor |
| 913 |
def.constructor = |
| 914 |
function( args ) |
| 915 |
return { |
| 916 |
at = args.at, |
| 917 |
from = args.from, |
| 918 |
to = args.to, |
| 919 |
xradius = args.xradius, |
| 920 |
yradius = args.yradius, |
| 921 |
} |
| 922 |
end |
| 923 |
|
| 924 |
-- converts to a tikz string. |
| 925 |
def.zString = |
| 926 |
function( self ) |
| 927 |
s = { } |
| 928 |
table.insert( s, 'plot' ) |
| 929 |
table.insert( s, '[domain =' ) |
| 930 |
table.insert( s, self.from ) |
| 931 |
table.insert( s, ':' ) |
| 932 |
table.insert( s, self.to ) |
| 933 |
table.insert( s, ',variable = \\phi,smooth]' ) |
| 934 |
table.insert( s, |
| 935 |
'({ '..zString(self.at.x)..'+'..zString(self.xradius)..'*cos(\\phi)},' |
| 936 |
..'{ '..zString(self.at.y)..'+'..zString(self.yradius)..'*sin(\\phi)})' |
| 937 |
) |
| 938 |
return table.concat( s, ' ' ) |
| 939 |
end |
| 940 |
end ) |
| 941 |
|
| 942 |
------------------------------------------------------------------------------- |
| 943 |
-- Single line (segment). |
| 944 |
------------------------------------------------------------------------------- |
| 945 |
tikz.line = |
| 946 |
immutable( function( def ) |
| 947 |
def.id = 'line' |
| 948 |
|
| 949 |
-- moves the line. |
| 950 |
def.add = |
| 951 |
function( left, right ) |
| 952 |
if right.id == 'point' |
| 953 |
then |
| 954 |
return tikz.line{ |
| 955 |
p1 = left.p1 + right, |
| 956 |
p2 = left.p2 + right, |
| 957 |
} |
| 958 |
end |
| 959 |
error( 'unknown operation' ) |
| 960 |
end |
| 961 |
|
| 962 |
-- multiplicator |
| 963 |
def.mul = |
| 964 |
function( left, right ) |
| 965 |
if type( left ) == 'number' |
| 966 |
then |
| 967 |
return tikz.line{ |
| 968 |
p1 = right.p1 * left, |
| 969 |
p2 = right.p2 * left, |
| 970 |
} |
| 971 |
elseif type( right ) == 'number' |
| 972 |
then |
| 973 |
return tikz.line{ |
| 974 |
p1 = left.p1 * right, |
| 975 |
p2 = left.p2 * right, |
| 976 |
} |
| 977 |
end |
| 978 |
error( 'unknown operation' ) |
| 979 |
end |
| 980 |
|
| 981 |
-- constructor |
| 982 |
-- |
| 983 |
-- either |
| 984 |
-- { p1, p2 }, (a list ) |
| 985 |
-- { p1 = ..., p2 = ... }, |
| 986 |
-- { pc = ..., phi = ..., length = ... }, |
| 987 |
def.constructor = |
| 988 |
function( args ) |
| 989 |
local p1 |
| 990 |
local p2 |
| 991 |
|
| 992 |
if #args > 1 |
| 993 |
then |
| 994 |
-- list |
| 995 |
p1 = args[ 1 ] |
| 996 |
p2 = args[ 2 ] |
| 997 |
elseif type( args.p1 ) ~= 'nil' and type( args.p2 ) ~= 'nil' |
| 998 |
then |
| 999 |
p1 = args.p1 |
| 1000 |
p2 = args.p2 |
| 1001 |
elseif args.phi ~= nil |
| 1002 |
then |
| 1003 |
local phi = args.phi |
| 1004 |
if args.pc ~= nil |
| 1005 |
then |
| 1006 |
if args.p1 ~= nil |
| 1007 |
or args.p2 ~= nil |
| 1008 |
then |
| 1009 |
error( 'invalid line options', 2 ) |
| 1010 |
end |
| 1011 |
local pc = args.pc |
| 1012 |
local len05 = args.length / 2 |
| 1013 |
p1 = pc + tikz.p{ math.cos( phi ), math.sin( phi ) } * len05 |
| 1014 |
p2 = pc - tikz.p{ math.cos( phi ), math.sin( phi ) } * len05 |
| 1015 |
elseif type( args.p1 ) ~= 'nil' |
| 1016 |
then |
| 1017 |
if args.pc ~= nil |
| 1018 |
or args.p2 ~= nil |
| 1019 |
then |
| 1020 |
error( 'invalid line options', 2 ) |
| 1021 |
end |
| 1022 |
p1 = args.p1 |
| 1023 |
p2 = p1 + tikz.p{ math.cos( phi ), math.sin( phi ) } * args.length |
| 1024 |
else |
| 1025 |
error( 'invalid line options', 2 ) |
| 1026 |
end |
| 1027 |
else |
| 1028 |
error( 'invalid line options', 2 ) |
| 1029 |
end |
| 1030 |
|
| 1031 |
return { |
| 1032 |
p1 = p1, |
| 1033 |
p2 = p2, |
| 1034 |
} |
| 1035 |
end |
| 1036 |
|
| 1037 |
-- divisor |
| 1038 |
def.div = |
| 1039 |
function( left, right ) |
| 1040 |
if type( right ) == 'number' |
| 1041 |
then |
| 1042 |
return tikz.line{ |
| 1043 |
p1 = left.p1 / right, |
| 1044 |
p2 = left.p2 / right, |
| 1045 |
} |
| 1046 |
end |
| 1047 |
error( 'unknown operation' ) |
| 1048 |
end |
| 1049 |
|
| 1050 |
-- length of line. |
| 1051 |
def.lazy.length = |
| 1052 |
function( self ) |
| 1053 |
local p1 = self.p1 |
| 1054 |
local p2 = self.p2 |
| 1055 |
local dx = p2.x - p1.x |
| 1056 |
local dy = p2.y - p1.y |
| 1057 |
local result = math.sqrt( dx*dx + dy*dy ) |
| 1058 |
return result |
| 1059 |
end |
| 1060 |
|
| 1061 |
-- center of line. |
| 1062 |
def.lazy.pc = |
| 1063 |
function( self ) |
| 1064 |
local p1 = self.p1 |
| 1065 |
local p2 = self.p2 |
| 1066 |
local result = tikz.p{ ( p1.x + p2.x ) / 2, ( p1.y + p2.y ) / 2 } |
| 1067 |
return result |
| 1068 |
end |
| 1069 |
|
| 1070 |
-- angle of line. |
| 1071 |
def.lazy.phi = |
| 1072 |
function( self ) |
| 1073 |
local p1 = self.p1 |
| 1074 |
local p2 = self.p2 |
| 1075 |
local result = math.atan2( p2.y - p1.y, p2.x - p1.x ) |
| 1076 |
return result |
| 1077 |
end |
| 1078 |
|
| 1079 |
-- gets point at t (0-1). |
| 1080 |
def.proto.pt = |
| 1081 |
function( self, t ) |
| 1082 |
if type( t ) ~= 'number' or t < 0 or t > 1 |
| 1083 |
then |
| 1084 |
error( 'invalid pt' ) |
| 1085 |
end |
| 1086 |
|
| 1087 |
local p1 = self.p1 |
| 1088 |
local p2 = self.p2 |
| 1089 |
local phi = self.phi |
| 1090 |
local lt = self.length * t |
| 1091 |
|
| 1092 |
return( |
| 1093 |
tikz.p{ |
| 1094 |
p1.x + lt * math.cos( phi ), |
| 1095 |
p1.y + lt * math.sin( phi ), |
| 1096 |
} |
| 1097 |
) |
| 1098 |
end |
| 1099 |
|
| 1100 |
-- intersects the line with another. |
| 1101 |
def.proto.intersectLine = |
| 1102 |
function( self, line ) |
| 1103 |
local p1 = self.p1 |
| 1104 |
local p2 = self.p2 |
| 1105 |
local p3 = line.p1 |
| 1106 |
local p4 = line.p2 |
| 1107 |
|
| 1108 |
if p1.x == p3.x and p1.y == p3.y then return p1 end |
| 1109 |
if p1.x == p4.x and p1.y == p4.y then return p1 end |
| 1110 |
if p2.x == p3.x and p2.y == p3.y then return p2 end |
| 1111 |
if p2.x == p4.x and p2.y == p4.y then return p2 end |
| 1112 |
|
| 1113 |
local den = ( p1.x - p2.x )*( p3.y - p4.y ) - ( p1.y - p2.y )*( p3.x - p4.x ); |
| 1114 |
if den == 0 |
| 1115 |
then |
| 1116 |
-- doesn't intersect |
| 1117 |
return |
| 1118 |
end |
| 1119 |
|
| 1120 |
local a = p1.x*p2.y - p1.y*p2.x; |
| 1121 |
local b = p3.x*p4.y - p3.y*p4.x; |
| 1122 |
local x = ( a*( p3.x - p4.x ) - ( p1.x - p2.x )*b ) / den; |
| 1123 |
local y = ( a*( p3.y - p4.y ) - ( p1.y - p2.y )*b ) / den; |
| 1124 |
|
| 1125 |
if |
| 1126 |
( |
| 1127 |
( x >= p1.x and x <= p2.x or x <= p1.x and x >= p2.x ) |
| 1128 |
and ( x >= p3.x and x <= p4.x or x <= p3.x and x >= p4.x ) |
| 1129 |
) |
| 1130 |
or |
| 1131 |
( |
| 1132 |
( y >= p1.y and y <= p2.y or y <= p1.y and y >= p2.y ) |
| 1133 |
and ( y >= p3.y and y <= p4.y or y <= p3.y and y >= p4.y ) |
| 1134 |
) |
| 1135 |
then |
| 1136 |
return tikz.p{ x, y } |
| 1137 |
end |
| 1138 |
end |
| 1139 |
|
| 1140 |
-- converts to a tikz string. |
| 1141 |
def.zString = |
| 1142 |
function( self ) |
| 1143 |
local s = { } |
| 1144 |
table.insert( s, zString( self.p1 ) ) |
| 1145 |
table.insert( s, '--' ) |
| 1146 |
table.insert( s, zString( self.p2 ) ) |
| 1147 |
return table.concat( s, ' ' ) |
| 1148 |
end |
| 1149 |
end ) |
| 1150 |
|
| 1151 |
------------------------------------------------------------------------------- |
| 1152 |
-- Function plot. |
| 1153 |
------------------------------------------------------------------------------- |
| 1154 |
tikz.plot = |
| 1155 |
immutable( function( def ) |
| 1156 |
def.id = 'plot' |
| 1157 |
|
| 1158 |
-- constructor |
| 1159 |
def.constructor = |
| 1160 |
function( args ) |
| 1161 |
return { |
| 1162 |
at = args.at, |
| 1163 |
from = args.from, |
| 1164 |
to = args.to, |
| 1165 |
step = args.step, |
| 1166 |
tension = args.tension, |
| 1167 |
func = args.func, |
| 1168 |
} |
| 1169 |
end |
| 1170 |
|
| 1171 |
def.lazy.points = |
| 1172 |
function( self ) |
| 1173 |
local from = self.from |
| 1174 |
local to = self.to |
| 1175 |
local step = self.step |
| 1176 |
if step == nil |
| 1177 |
then |
| 1178 |
step = ( to - from ) / 100 |
| 1179 |
end |
| 1180 |
local points = { } |
| 1181 |
local d |
| 1182 |
local at = self.at or tikz.p0 |
| 1183 |
for d = from, to, step |
| 1184 |
do |
| 1185 |
table.insert( points, at + self.func( d ) ) |
| 1186 |
end |
| 1187 |
return points |
| 1188 |
end |
| 1189 |
|
| 1190 |
-- converts to a tikz string. |
| 1191 |
def.zString = |
| 1192 |
function( self ) |
| 1193 |
local s = { 'plot' } |
| 1194 |
table.insert( s, '[' ) |
| 1195 |
table.insert( s, 'smooth' ) |
| 1196 |
if self.tension |
| 1197 |
then |
| 1198 |
table.insert( s, ',tension='..self.tension ) |
| 1199 |
end |
| 1200 |
table.insert( s, ']' ) |
| 1201 |
|
| 1202 |
local points = self.points |
| 1203 |
|
| 1204 |
table.insert( s, 'coordinates' ) |
| 1205 |
table.insert( s, '{' ) |
| 1206 |
for _, p in ipairs( points ) |
| 1207 |
do |
| 1208 |
table.insert( s, zString( p ) ) |
| 1209 |
end |
| 1210 |
table.insert( s, '}' ) |
| 1211 |
return table.concat( s, ' ' ) |
| 1212 |
end |
| 1213 |
end ) |
| 1214 |
|
| 1215 |
------------------------------------------------------------------------------- |
| 1216 |
-- A superEllipse |
| 1217 |
------------------------------------------------------------------------------- |
| 1218 |
tikz.superEllipse = |
| 1219 |
immutable( function( def ) |
| 1220 |
def.id = 'superEllipse' |
| 1221 |
|
| 1222 |
-- constructor |
| 1223 |
def.constructor = |
| 1224 |
function( args ) |
| 1225 |
local at = args.at |
| 1226 |
local n = args.n |
| 1227 |
local xradius = args.xradius |
| 1228 |
local yradius = args.yradius |
| 1229 |
|
| 1230 |
return { |
| 1231 |
at = at, |
| 1232 |
n = n, |
| 1233 |
xradius = xradius, |
| 1234 |
yradius = yradius, |
| 1235 |
} |
| 1236 |
end |
| 1237 |
|
| 1238 |
-- returns p on degree (0-360) |
| 1239 |
def.proto.pdeg = |
| 1240 |
function( self, t ) |
| 1241 |
if type( t ) ~= 'number' or t < 0 or t > 360 |
| 1242 |
then |
| 1243 |
error( 'invalid pdeg' ) |
| 1244 |
end |
| 1245 |
|
| 1246 |
local at = self.at |
| 1247 |
local n = self.n |
| 1248 |
local xo = self.xradius * math.abs( math.cos( t * math.pi / 180 ) )^(2/n) |
| 1249 |
local yo = self.yradius * math.abs( math.sin( t * math.pi / 180 ) )^(2/n) |
| 1250 |
|
| 1251 |
if t <= 90 |
| 1252 |
then |
| 1253 |
return tikz.p{ at.x + xo, at.y + yo } |
| 1254 |
elseif t <= 180 |
| 1255 |
then |
| 1256 |
return tikz.p{ at.x - xo, at.y + yo } |
| 1257 |
elseif t <= 270 |
| 1258 |
then |
| 1259 |
return tikz.p{ at.x - xo, at.y - yo } |
| 1260 |
else |
| 1261 |
return tikz.p{ at.x + xo, at.y - yo } |
| 1262 |
end |
| 1263 |
end |
| 1264 |
|
| 1265 |
-- converts to a tikz string. |
| 1266 |
def.zString = |
| 1267 |
function( self ) |
| 1268 |
local s = { } |
| 1269 |
local at = self.at |
| 1270 |
local xa = at.x |
| 1271 |
local ya = at.y |
| 1272 |
local n = self.n |
| 1273 |
local xr = self.xradius |
| 1274 |
local yr = self.yradius |
| 1275 |
table.insert( s, 'plot[domain=0:90,variable=\\t,smooth]' ) |
| 1276 |
table.insert( s, '({'..xr..'*cos(\\t)^(2/'..n..')+'..xa..'},{'..yr..'*sin(\\t)^(2/'..n..')+'..ya..'})' ) |
| 1277 |
table.insert( s, '--' ) |
| 1278 |
table.insert( s, 'plot[domain=90:0,variable=\\t,smooth]' ) |
| 1279 |
table.insert( s, '({-'..xr..'*cos(\\t)^(2/'..n..')+'..xa..'},{'..yr..'*sin(\\t)^(2/'..n..')+'..ya..'})' ) |
| 1280 |
table.insert( s, '--' ) |
| 1281 |
table.insert( s, 'plot[domain=0:90,variable=\\t,smooth]' ) |
| 1282 |
table.insert( s, '({-'..xr..'*cos(\\t)^(2/'..n..')+'..xa..'},{-'..yr..'*sin(\\t)^(2/'..n..')+'..ya..'})' ) |
| 1283 |
table.insert( s, '--' ) |
| 1284 |
table.insert( s, 'plot[domain=90:0,variable=\\t,smooth]' ) |
| 1285 |
table.insert( s, '({'..xr..'*cos(\\t)^(2/'..n..')+'..xa..'},{-'..yr..'*sin(\\t)^(2/'..n..')+'..ya..'})' ) |
| 1286 |
table.insert( s, '-- cycle' ) |
| 1287 |
return table.concat( s, ' ' ) |
| 1288 |
end |
| 1289 |
end ) |
| 1290 |
|
| 1291 |
------------------------------------------------------------------------------- |
| 1292 |
-- A reference to a named node. |
| 1293 |
------------------------------------------------------------------------------- |
| 1294 |
local optionsNamed = |
| 1295 |
{ |
| 1296 |
ref = false, |
| 1297 |
xshift = 'xshift', |
| 1298 |
yshift = 'yshift', |
| 1299 |
} |
| 1300 |
|
| 1301 |
tikz.named = |
| 1302 |
immutable( function( def ) |
| 1303 |
def.id = 'named' |
| 1304 |
|
| 1305 |
-- constructor |
| 1306 |
def.constructor = |
| 1307 |
function( args ) |
| 1308 |
local s = { } |
| 1309 |
_doOptions( s, args, optionsNamed, false, 2 ) |
| 1310 |
return { |
| 1311 |
opts = table.concat( s, ' ' ), |
| 1312 |
ref = args.ref, |
| 1313 |
} |
| 1314 |
end |
| 1315 |
|
| 1316 |
-- converts to a tikz string. |
| 1317 |
def.zString = |
| 1318 |
function( self ) |
| 1319 |
local s = { } |
| 1320 |
table.insert( s, '(' ) |
| 1321 |
if self.opts |
| 1322 |
then |
| 1323 |
table.insert( s, self.opts ) |
| 1324 |
end |
| 1325 |
table.insert( s, self.ref ) |
| 1326 |
table.insert( s, ')' ) |
| 1327 |
return table.concat( s ) |
| 1328 |
end |
| 1329 |
end ) |
| 1330 |
|
| 1331 |
------------------------------------------------------------------------------- |
| 1332 |
-- Multiple connected lines. |
| 1333 |
------------------------------------------------------------------------------- |
| 1334 |
tikz.polyline = |
| 1335 |
immutable( function( def ) |
| 1336 |
def.id = 'polyline' |
| 1337 |
|
| 1338 |
-- constructor |
| 1339 |
def.constructor = |
| 1340 |
function( args ) |
| 1341 |
local o = { } |
| 1342 |
for _, v in ipairs( args ) |
| 1343 |
do |
| 1344 |
table.insert( o, v ) |
| 1345 |
end |
| 1346 |
return o |
| 1347 |
end |
| 1348 |
|
| 1349 |
-- converts to a tikz string. |
| 1350 |
def.zString = |
| 1351 |
function( self ) |
| 1352 |
local s = { '' } |
| 1353 |
for k, v in ipairs( self ) |
| 1354 |
do |
| 1355 |
if k > 1 then table.insert( s, '--' ) end |
| 1356 |
table.insert( s, zString( v ) ) |
| 1357 |
end |
| 1358 |
return table.concat( s, ' ' ) |
| 1359 |
end |
| 1360 |
end ) |
| 1361 |
|
| 1362 |
------------------------------------------------------------------------------- |
| 1363 |
-- A rectangle |
| 1364 |
------------------------------------------------------------------------------- |
| 1365 |
tikz.rect = |
| 1366 |
immutable( function( def ) |
| 1367 |
def.id = 'rect' |
| 1368 |
|
| 1369 |
-- constructor |
| 1370 |
def.constructor = |
| 1371 |
function( args ) |
| 1372 |
local psw |
| 1373 |
local pne |
| 1374 |
|
| 1375 |
if args.pnw ~= nil |
| 1376 |
then |
| 1377 |
if args.pse ~= nil |
| 1378 |
then |
| 1379 |
psw = tikz.p{ args.pnw.x, args.pse.y } |
| 1380 |
pne = tikz.p{ args.pse.x, args.pnw.y } |
| 1381 |
elseif args.size ~= nil |
| 1382 |
then |
| 1383 |
psw = tikz.p{ args.pnw.x, args.pnw.y - args.size.y } |
| 1384 |
pne = tikz.p{ args.pnw.x + args.size.x, args.pnw.y } |
| 1385 |
else |
| 1386 |
error( 'invalid rect options', 2 ) |
| 1387 |
end |
| 1388 |
elseif args.psw ~= nil |
| 1389 |
then |
| 1390 |
psw = args.psw |
| 1391 |
if args.pne ~= nil |
| 1392 |
then |
| 1393 |
pne = args.pne |
| 1394 |
elseif args.size ~= nil |
| 1395 |
then |
| 1396 |
pne = psw + args.size |
| 1397 |
else |
| 1398 |
error( 'invalid rect options', 2 ) |
| 1399 |
end |
| 1400 |
elseif args.pse ~= nil |
| 1401 |
then |
| 1402 |
if args.size ~= nil |
| 1403 |
then |
| 1404 |
psw = tikz.p{ args.pse.x - args.size.x, args.pse.y } |
| 1405 |
pne = tikz.p{ args.pse.x, args.pse.y + args.size.y } |
| 1406 |
else |
| 1407 |
error( 'invalid rect options', 2 ) |
| 1408 |
end |
| 1409 |
elseif args.pc ~= nil |
| 1410 |
then |
| 1411 |
if args.size ~= nil |
| 1412 |
then |
| 1413 |
psw = tikz.p{ args.pc.x - args.size.x / 2, args.pc.y - args.size.y / 2 } |
| 1414 |
pne = tikz.p{ args.pc.x + args.size.x / 2, args.pc.y + args.size.y / 2 } |
| 1415 |
else |
| 1416 |
error( 'invalid rect options', 2 ) |
| 1417 |
end |
| 1418 |
else |
| 1419 |
error( 'invalid rect options', 2 ) |
| 1420 |
end |
| 1421 |
|
| 1422 |
return { |
| 1423 |
psw = psw, |
| 1424 |
pne = pne, |
| 1425 |
} |
| 1426 |
end |
| 1427 |
|
| 1428 |
def.zString = |
| 1429 |
function( self ) |
| 1430 |
local s = { } |
| 1431 |
table.insert( s, zString( self.psw ) ) |
| 1432 |
table.insert( s, 'rectangle' ) |
| 1433 |
table.insert( s, zString( self.pne ) ) |
| 1434 |
return table.concat( s, ' ' ) |
| 1435 |
end |
| 1436 |
|
| 1437 |
def.lazy.e = |
| 1438 |
function( self ) |
| 1439 |
return self.pne.x |
| 1440 |
end |
| 1441 |
|
| 1442 |
def.lazy.height = |
| 1443 |
function( self ) |
| 1444 |
return self.pne.y - self.psw.y |
| 1445 |
end |
| 1446 |
|
| 1447 |
def.lazy.n = |
| 1448 |
function( self ) |
| 1449 |
return self.pne.y |
| 1450 |
end |
| 1451 |
|
| 1452 |
def.lazy.pc = |
| 1453 |
function( self ) |
| 1454 |
return tikz.p{ self.psw.x + self.width / 2, self.psw.y + self.height / 2 } |
| 1455 |
end |
| 1456 |
|
| 1457 |
def.lazy.pe = |
| 1458 |
function( self ) |
| 1459 |
return tikz.p{ self.pne.x, self.psw.y + self.height / 2 } |
| 1460 |
end |
| 1461 |
|
| 1462 |
def.lazy.pn = |
| 1463 |
function( self ) |
| 1464 |
return tikz.p{ self.psw.x + self.width / 2, self.pne.y } |
| 1465 |
end |
| 1466 |
|
| 1467 |
def.lazy.pnw = |
| 1468 |
function( self ) |
| 1469 |
return tikz.p{ self.psw.x, self.pne.y } |
| 1470 |
end |
| 1471 |
|
| 1472 |
def.lazy.ps = |
| 1473 |
function( self ) |
| 1474 |
return tikz.p{ self.psw.x + self.width / 2, self.psw.y } |
| 1475 |
end |
| 1476 |
|
| 1477 |
def.lazy.pse = |
| 1478 |
function( self ) |
| 1479 |
return tikz.p{ self.pne.x, self.psw.y } |
| 1480 |
end |
| 1481 |
|
| 1482 |
def.lazy.pw = |
| 1483 |
function( self ) |
| 1484 |
return tikz.p{ self.psw.x, self.psw.y + self.height / 2 } |
| 1485 |
end |
| 1486 |
|
| 1487 |
def.lazy.s = |
| 1488 |
function( self ) |
| 1489 |
return self.psw.y |
| 1490 |
end |
| 1491 |
|
| 1492 |
def.lazy.size = |
| 1493 |
function( self ) |
| 1494 |
return tikz.p{ self.width, self.height } |
| 1495 |
end |
| 1496 |
|
| 1497 |
def.lazy.w = |
| 1498 |
function( self ) |
| 1499 |
return self.psw.x |
| 1500 |
end |
| 1501 |
|
| 1502 |
def.lazy.width = |
| 1503 |
function( self ) |
| 1504 |
return self.pne.x - self.psw.x |
| 1505 |
end |
| 1506 |
end ) |
| 1507 |
|
| 1508 |
|
| 1509 |
--==========================================================================-- |
| 1510 |
-- STYLES -- |
| 1511 |
--==========================================================================-- |
| 1512 |
tikz.style = |
| 1513 |
immutable( function( def ) |
| 1514 |
def.id = 'style' |
| 1515 |
|
| 1516 |
-- constructor |
| 1517 |
def.constructor = |
| 1518 |
function( name ) |
| 1519 |
return { name=name } |
| 1520 |
end |
| 1521 |
end ) |
| 1522 |
|
| 1523 |
tikz.above = tikz.style( 'above' ) |
| 1524 |
tikz.arrow = tikz.style( '-{Latex}' ) |
| 1525 |
tikz.below = tikz.style( 'below' ) |
| 1526 |
tikz.dashed = tikz.style( 'dashed' ) |
| 1527 |
tikz.dotted = tikz.style( 'dotted' ) |
| 1528 |
tikz.double_arrow = tikz.style( '{Latex}-{Latex}' ) |
| 1529 |
tikz.even_odd_rule = tikz.style( 'even odd rule' ) |
| 1530 |
tikz.midway = tikz.style( 'midway' ) |
| 1531 |
|
| 1532 |
--==========================================================================-- |
| 1533 |
-- OUTPUT --- |
| 1534 |
--==========================================================================-- |
| 1535 |
|
| 1536 |
-- |
| 1537 |
-- Writes out a named option. |
| 1538 |
-- |
| 1539 |
-- ~s: string to build |
| 1540 |
-- ~args: arguments |
| 1541 |
-- ~name: name of option |
| 1542 |
-- ~haveOpts: true if had options |
| 1543 |
-- ~rename: if defined rename options on output |
| 1544 |
-- |
| 1545 |
local function optionNamed( s, args, name, haveOpts, rename ) |
| 1546 |
|
| 1547 |
local v = args[ name ] |
| 1548 |
|
| 1549 |
if not v |
| 1550 |
then |
| 1551 |
return haveOpts |
| 1552 |
end |
| 1553 |
|
| 1554 |
if not rename |
| 1555 |
then |
| 1556 |
return haveOpts |
| 1557 |
end |
| 1558 |
|
| 1559 |
if not haveOpts |
| 1560 |
then |
| 1561 |
table.insert( s, '[' ) |
| 1562 |
else |
| 1563 |
table.insert( s, ',' ) |
| 1564 |
end |
| 1565 |
|
| 1566 |
if v == true |
| 1567 |
then |
| 1568 |
table.insert( s, rename ) |
| 1569 |
else |
| 1570 |
table.insert( s, rename..'='..v ) |
| 1571 |
end |
| 1572 |
|
| 1573 |
return true |
| 1574 |
end |
| 1575 |
|
| 1576 |
-- |
| 1577 |
-- Writes out an unnamed option. |
| 1578 |
-- |
| 1579 |
-- ~s: string to build |
| 1580 |
-- ~name: name of option |
| 1581 |
-- ~haveOpts: true if had options |
| 1582 |
-- |
| 1583 |
local function unnamedOption( s, name, haveOpts ) |
| 1584 |
if not haveOpts |
| 1585 |
then |
| 1586 |
table.insert( s, '[' ) |
| 1587 |
else |
| 1588 |
table.insert( s, ',' ) |
| 1589 |
end |
| 1590 |
table.insert( s, name ) |
| 1591 |
return true |
| 1592 |
end |
| 1593 |
|
| 1594 |
-- |
| 1595 |
-- Custom key compare. |
| 1596 |
-- |
| 1597 |
-- keys to put on top |
| 1598 |
local topKeys = |
| 1599 |
{ |
| 1600 |
node_distance = true, |
| 1601 |
} |
| 1602 |
|
| 1603 |
local function kcompare( ka, kb ) |
| 1604 |
local ta = topKeys[ ka ] |
| 1605 |
local tb = topKeys[ kb ] |
| 1606 |
if ta and not tb then return true end |
| 1607 |
if not ta and tb then return false end |
| 1608 |
return ka < kb |
| 1609 |
end |
| 1610 |
|
| 1611 |
-- |
| 1612 |
-- A helper to parse arguments to pass as drawing options to hand to tikz. |
| 1613 |
-- |
| 1614 |
-- s: string to build |
| 1615 |
-- args: the arguments to parse. |
| 1616 |
-- opts: named options to pass on |
| 1617 |
-- haveOpts: true [ has already been built |
| 1618 |
-- level: call level for error throwing |
| 1619 |
-- |
| 1620 |
local function _doOptions( s, args, opts, haveOpts, level ) |
| 1621 |
args = flatten( args ) |
| 1622 |
|
| 1623 |
for _, v in ipairs( args ) |
| 1624 |
do |
| 1625 |
if v.id == 'style' |
| 1626 |
then |
| 1627 |
haveOpts = unnamedOption( s, v.name, haveOpts ) |
| 1628 |
end |
| 1629 |
end |
| 1630 |
|
| 1631 |
local nkeys = { } |
| 1632 |
for key in pairs( args ) |
| 1633 |
do |
| 1634 |
if type( key ) ~= 'number' |
| 1635 |
then |
| 1636 |
table.insert( nkeys, key ) |
| 1637 |
end |
| 1638 |
end |
| 1639 |
table.sort( nkeys, kcompare ) |
| 1640 |
|
| 1641 |
for _, k in ipairs( nkeys ) |
| 1642 |
do |
| 1643 |
local ok = opts[ k ] |
| 1644 |
if ok |
| 1645 |
then |
| 1646 |
haveOpts = optionNamed( s, args, k, haveOpts, ok ) |
| 1647 |
elseif ok == nil |
| 1648 |
then |
| 1649 |
error( 'unknown option '..k, level + 1 ) |
| 1650 |
end |
| 1651 |
end |
| 1652 |
|
| 1653 |
haveOpts = _opts( s, args._opts, haveOpts ) |
| 1654 |
|
| 1655 |
if haveOpts |
| 1656 |
then |
| 1657 |
table.insert( s, ']' ) |
| 1658 |
end |
| 1659 |
end |
| 1660 |
|
| 1661 |
------------------------------------------------------------------------------- |
| 1662 |
-- Drawing shapes. |
| 1663 |
------------------------------------------------------------------------------- |
| 1664 |
local optionsDraw = |
| 1665 |
{ |
| 1666 |
decorate = 'decorate', |
| 1667 |
decoration = 'decoration', |
| 1668 |
dash_pattern = 'dash pattern', |
| 1669 |
color = 'color', |
| 1670 |
draw = 'draw', |
| 1671 |
fill = 'fill', |
| 1672 |
line_cap = 'line cap', |
| 1673 |
line_width = 'line width', |
| 1674 |
opacity = 'opacity', |
| 1675 |
pattern = 'pattern', |
| 1676 |
rotate = 'rotate', |
| 1677 |
transform_canvas = 'transform canvas', |
| 1678 |
xshift = 'xshift', |
| 1679 |
yshift = 'yshift', |
| 1680 |
_butt_cap = '-Butt Cap', |
| 1681 |
butt_cap_ = 'Butt Cap-', |
| 1682 |
_round_cap = '-{Round Cap[]}', |
| 1683 |
round_cap_ = '{Round Cap[]}-', |
| 1684 |
_round_cap_ = '{Round Cap[]}-{Round Cap[]}', |
| 1685 |
} |
| 1686 |
|
| 1687 |
-- |
| 1688 |
-- Puts out a bounding box command |
| 1689 |
-- |
| 1690 |
tikz.boundingbox = |
| 1691 |
function( args ) |
| 1692 |
tprint( '\\pgfresetboundingbox' ) |
| 1693 |
local s = { '\\path [use as bounding box] ' } |
| 1694 |
for _, v in ipairs( args ) |
| 1695 |
do |
| 1696 |
if v.id ~= 'style' |
| 1697 |
then |
| 1698 |
table.insert( s, zString( v ) ) |
| 1699 |
end |
| 1700 |
end |
| 1701 |
table.insert( s, ';' ) |
| 1702 |
tprint( table.unpack( s ) ) |
| 1703 |
end |
| 1704 |
|
| 1705 |
-- |
| 1706 |
-- Puts out a draw command. |
| 1707 |
-- |
| 1708 |
tikz.draw = |
| 1709 |
function( args ) |
| 1710 |
local s = { '\\draw ' } |
| 1711 |
_doOptions( s, args, optionsDraw, false, 2 ) |
| 1712 |
|
| 1713 |
for _, v in ipairs( args ) |
| 1714 |
do |
| 1715 |
if v.id ~= 'style' |
| 1716 |
then |
| 1717 |
table.insert( s, zString( v ) ) |
| 1718 |
end |
| 1719 |
end |
| 1720 |
|
| 1721 |
table.insert( s, ';' ) |
| 1722 |
tprint( table.unpack( s ) ) |
| 1723 |
end |
| 1724 |
|
| 1725 |
-- |
| 1726 |
-- Puts out a path command. |
| 1727 |
-- |
| 1728 |
tikz.path = |
| 1729 |
function( args ) |
| 1730 |
local s = { '\\path' } |
| 1731 |
_doOptions( s, args, optionsDraw, false, 2 ) |
| 1732 |
|
| 1733 |
for _, v in ipairs( args ) |
| 1734 |
do |
| 1735 |
if v.id ~= 'style' |
| 1736 |
then |
| 1737 |
table.insert( s, zString( v ) ) |
| 1738 |
end |
| 1739 |
end |
| 1740 |
table.insert( s, ';' ) |
| 1741 |
tprint( table.unpack( s ) ) |
| 1742 |
end |
| 1743 |
|
| 1744 |
-- |
| 1745 |
-- Puts out a bounding box. |
| 1746 |
-- |
| 1747 |
tikz.clip = |
| 1748 |
function( args ) |
| 1749 |
local s = { '\\begin{pgfinterruptboundingbox}', '\\clip' } |
| 1750 |
_doOptions( s, args, optionsDraw, false, 2 ) |
| 1751 |
|
| 1752 |
for _, v in ipairs( args ) |
| 1753 |
do |
| 1754 |
if v.id ~= 'style' then table.insert( s, zString( v ) ) end |
| 1755 |
end |
| 1756 |
table.insert( s, ';' ) |
| 1757 |
table.insert( s, '\\end{pgfinterruptboundingbox}' ) |
| 1758 |
tprint( table.unpack( s ) ) |
| 1759 |
end |
| 1760 |
|
| 1761 |
local shadeOptions = |
| 1762 |
{ |
| 1763 |
ball_color = 'ball color', |
| 1764 |
left_color = 'left color', |
| 1765 |
lower_left_color = 'lower left', |
| 1766 |
lower_right_color = 'lower right', |
| 1767 |
opacity = 'opacity', |
| 1768 |
right_color = 'right color', |
| 1769 |
shading = 'shading', |
| 1770 |
upper_left_color = 'upper left', |
| 1771 |
upper_right_color = 'upper right', |
| 1772 |
} |
| 1773 |
|
| 1774 |
-- |
| 1775 |
-- Puts out a shade command. |
| 1776 |
-- |
| 1777 |
tikz.shade = |
| 1778 |
function( args ) |
| 1779 |
local s = { '\\shade' } |
| 1780 |
_doOptions( s, args, shadeOptions, false, 2 ) |
| 1781 |
|
| 1782 |
for _, v in ipairs( args ) |
| 1783 |
do |
| 1784 |
if v.id ~= 'style' then table.insert( s, zString( v ) ) end |
| 1785 |
end |
| 1786 |
table.insert( s, ';' ) |
| 1787 |
tprint( table.unpack( s ) ) |
| 1788 |
end |
| 1789 |
|
| 1790 |
-- |
| 1791 |
-- Puts out z stuff. |
| 1792 |
-- |
| 1793 |
tikz.put = |
| 1794 |
function( args ) |
| 1795 |
for _, v in ipairs( args ) |
| 1796 |
do |
| 1797 |
local s = { } |
| 1798 |
if v.id == 'node' |
| 1799 |
then |
| 1800 |
table.insert( s, '\\' ) |
| 1801 |
table.insert( s, zString( v ) ) |
| 1802 |
table.insert( s, ';' ) |
| 1803 |
else |
| 1804 |
error( 'unknown: ' .. v.id, 1 ) |
| 1805 |
end |
| 1806 |
tprint( table.unpack( s ) ) |
| 1807 |
end |
| 1808 |
end |
| 1809 |
|
| 1810 |
tikz.declarehorizonalshading = |
| 1811 |
function( args ) |
| 1812 |
local steps = args.steps |
| 1813 |
local name = args.name |
| 1814 |
local s = { '\\pgfdeclarehorizontalshading{' .. name .. '}{100bp}{' } |
| 1815 |
|
| 1816 |
local keys = { } |
| 1817 |
for k, _ in pairs( steps ) |
| 1818 |
do |
| 1819 |
table.insert( keys, k ) |
| 1820 |
end |
| 1821 |
|
| 1822 |
table.sort( keys ) |
| 1823 |
for _, k in ipairs( keys ) |
| 1824 |
do |
| 1825 |
if _ > 1 then table.insert( s, ';' ) end |
| 1826 |
table.insert( s, 'color(' .. k .. 'bp)=(' .. steps[ k ] .. ')' ) |
| 1827 |
end |
| 1828 |
table.insert( s, '}' ) |
| 1829 |
|
| 1830 |
tprint( table.unpack( s ) ) |
| 1831 |
end |
| 1832 |
|
| 1833 |
------------------------------------------------------------------------------- |
| 1834 |
-- A node to be defined to tikZ |
| 1835 |
------------------------------------------------------------------------------- |
| 1836 |
local optionsNode = |
| 1837 |
{ |
| 1838 |
above = 'above', |
| 1839 |
anchor = 'anchor', |
| 1840 |
align = 'align', |
| 1841 |
at = false, |
| 1842 |
below = 'below', |
| 1843 |
color = 'color', |
| 1844 |
draw = 'draw', |
| 1845 |
left = 'left', |
| 1846 |
minimum_height = 'minimum height', |
| 1847 |
node_distance = 'node distance', |
| 1848 |
name = false, |
| 1849 |
right = 'right', |
| 1850 |
rotate = 'rotate', |
| 1851 |
text = false, |
| 1852 |
text_width = 'text width', |
| 1853 |
} |
| 1854 |
|
| 1855 |
tikz.node = |
| 1856 |
immutable( function( def ) |
| 1857 |
def.id = 'node'; |
| 1858 |
|
| 1859 |
def.zString = |
| 1860 |
function( self ) |
| 1861 |
local s = { 'node' } |
| 1862 |
|
| 1863 |
table.insert( s, self.opts ) |
| 1864 |
if self.name |
| 1865 |
then |
| 1866 |
table.insert( s, ' (' ) |
| 1867 |
table.insert( s, self.name ) |
| 1868 |
table.insert( s, ')' ) |
| 1869 |
end |
| 1870 |
|
| 1871 |
if self.at |
| 1872 |
then |
| 1873 |
table.insert( s, ' at ' ) |
| 1874 |
table.insert( s, zString( self.at ) ) |
| 1875 |
end |
| 1876 |
|
| 1877 |
if self.text |
| 1878 |
then |
| 1879 |
table.insert( s, ' {' ) |
| 1880 |
local text = string.gsub( self.text, '\n', '' ) |
| 1881 |
table.insert( s, text ) |
| 1882 |
table.insert( s, '}' ) |
| 1883 |
end |
| 1884 |
|
| 1885 |
return table.concat( s ) |
| 1886 |
end |
| 1887 |
|
| 1888 |
-- constructor |
| 1889 |
def.constructor = |
| 1890 |
function( args ) |
| 1891 |
local s = { } |
| 1892 |
_doOptions( s, args, optionsNode, false, 2 ) |
| 1893 |
return { |
| 1894 |
at = args.at, |
| 1895 |
name = args.name, |
| 1896 |
opts = table.concat( s, ' ' ), |
| 1897 |
text = args.text |
| 1898 |
} |
| 1899 |
end |
| 1900 |
end ) |
| 1901 |
|
| 1902 |
tikz.outfile = |
| 1903 |
function( filename ) |
| 1904 |
outfile = io.open( filename, 'w' ) |
| 1905 |
outfile:write( '%%% DO NOT EDIT THIS FILE %%%\n' ) |
| 1906 |
end |
| 1907 |
|
| 1908 |
-- |
| 1909 |
-- Hacking help. |
| 1910 |
-- |
| 1911 |
function _direct( args ) |
| 1912 |
tprint( table.unpack( args ) ) |
| 1913 |
end |
| 1914 |
|
| 1915 |
-- |
| 1916 |
-- Checks version metch. |
| 1917 |
-- |
| 1918 |
tikz.checkVersion = |
| 1919 |
function( v ) |
| 1920 |
if v ~= tikz.version |
| 1921 |
then |
| 1922 |
error( 'version mismatch', 1 ) |
| 1923 |
end |
| 1924 |
end |
| 1925 |
|
| 1926 |
-- |
| 1927 |
-- Creates a color. |
| 1928 |
-- |
| 1929 |
tikz.colorRGB = |
| 1930 |
function( red, green, blue ) |
| 1931 |
return '{rgb,255:red,'..red..'; green,'..green..'; blue,'..blue..'}' |
| 1932 |
end |
| 1933 |
|
| 1934 |
-- |
| 1935 |
-- Makes a tikz scope. |
| 1936 |
-- Argument must be a function to call. |
| 1937 |
-- |
| 1938 |
tikz.scope = |
| 1939 |
function( args ) |
| 1940 |
args = flatten( args ) |
| 1941 |
if #args ~= 1 or type( args[ 1 ] ) ~= 'function' |
| 1942 |
then |
| 1943 |
error( 'scope needs a function parameter' ) |
| 1944 |
end |
| 1945 |
_direct{ [[\begin{scope}]] } |
| 1946 |
args[ 1 ]( ) |
| 1947 |
_direct{ [[\end{scope}]] } |
| 1948 |
end |
| 1949 |
|
| 1950 |
--==========================================================================-- |
| 1951 |
-- SHORTCUTS |
| 1952 |
--==========================================================================-- |
| 1953 |
tikz.p0 = tikz.p{ 0, 0 } |
| 1954 |
|
| 1955 |
|
| 1956 |
--==========================================================================-- |
| 1957 |
-- Running code within tikz environment. |
| 1958 |
--==========================================================================-- |
| 1959 |
|
| 1960 |
local env_nesting = 0 |
| 1961 |
local org_env |
| 1962 |
tikz.within = |
| 1963 |
function( version ) |
| 1964 |
if version ~= '*' and version ~= tikz.version |
| 1965 |
then |
| 1966 |
error( 'version mismatch', 1 ) |
| 1967 |
end |
| 1968 |
|
| 1969 |
env_nesting = env_nesting + 1 |
| 1970 |
if env_nesting > 1 |
| 1971 |
then |
| 1972 |
return |
| 1973 |
end |
| 1974 |
|
| 1975 |
org_env = _ENV |
| 1976 |
local nenv = { } |
| 1977 |
local mt = { } |
| 1978 |
mt.__index = |
| 1979 |
function( _, key ) |
| 1980 |
local zk = tikz[ key ] |
| 1981 |
if zk ~= nil |
| 1982 |
then |
| 1983 |
return zk |
| 1984 |
else |
| 1985 |
return org_env[ key ] |
| 1986 |
end |
| 1987 |
end |
| 1988 |
setmetatable( nenv, mt ) |
| 1989 |
local func = debug.getinfo( 2 ).func |
| 1990 |
debug.setupvalue( func, 1, nenv ) |
| 1991 |
end |
| 1992 |
|
| 1993 |
tikz.without = |
| 1994 |
function( ) |
| 1995 |
env_nesting = env_nesting - 1 |
| 1996 |
if env_nesting > 0 then return end |
| 1997 |
local func = debug.getinfo( 2 ).func |
| 1998 |
debug.setupvalue( func, 1, org_env ) |
| 1999 |
end |