@@ -404,41 +404,46 @@ end
404
404
405
405
LineDrawer = defclass (LineDrawer , Shape )
406
406
407
+ function LineDrawer :plot_thickness (x , y , thickness )
408
+ local map_width , map_height = dfhack .maps .getTileSize ()
409
+ for i = math.max (y - math.floor (thickness / 2 ), 0 ), math.min (y + math.ceil (thickness / 2 ) - 1 , map_height - 1 ) do
410
+ for j = math.max (x - math.floor (thickness / 2 ), 0 ), math.min (x + math.ceil (thickness / 2 ) - 1 , map_width - 1 ) do
411
+ if not self .arr [j ] then self .arr [j ] = {} end
412
+ if not self .arr [j ][i ] then
413
+ self .arr [j ][i ] = true
414
+ self .num_tiles = self .num_tiles + 1
415
+ end
416
+ end
417
+ end
418
+ end
419
+
407
420
function LineDrawer :plot_bresenham (x0 , y0 , x1 , y1 , thickness )
408
421
local dx = math.abs (x1 - x0 )
409
422
local dy = math.abs (y1 - y0 )
410
423
local sx = x0 < x1 and 1 or - 1
411
424
local sy = y0 < y1 and 1 or - 1
412
425
local e2 , x , y
413
426
414
- for i = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
415
- x = x0
416
- y = y0 + i
417
- local err = dx - dy
418
- while true do
419
- for j = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
420
- if not self .arr [x + j ] then self .arr [x + j ] = {} end
421
- if not self .arr [x + j ][y ] then
422
- self .arr [x + j ][y ] = true
423
- self .num_tiles = self .num_tiles + 1
424
- end
425
- end
427
+ x = x0
428
+ y = y0
429
+ local err = dx - dy
430
+ while true do
431
+ self :plot_thickness (x , y , thickness )
426
432
427
- if sx * x >= sx * x1 and sy * y >= sy * ( y1 + i ) then
428
- break
429
- end
433
+ if sx * x >= sx * x1 and sy * y >= sy * y1 then
434
+ break
435
+ end
430
436
431
- e2 = 2 * err
437
+ e2 = 2 * err
432
438
433
- if e2 > - dy then
434
- err = err - dy
435
- x = x + sx
436
- end
439
+ if e2 > - dy then
440
+ err = err - dy
441
+ x = x + sx
442
+ end
437
443
438
- if e2 < dx then
439
- err = err + dx
440
- y = y + sy
441
- end
444
+ if e2 < dx then
445
+ err = err + dx
446
+ y = y + sy
442
447
end
443
448
end
444
449
end
@@ -490,15 +495,7 @@ function Line:cubic_bezier(x0, y0, x1, y1, bezier_point1, bezier_point2, thickne
490
495
0.5 )
491
496
local y = math.floor (((1 - t ) ^ 3 * y0 + 3 * (1 - t ) ^ 2 * t * y2 + 3 * (1 - t ) * t ^ 2 * y3 + t ^ 3 * y1 ) +
492
497
0.5 )
493
- for i = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
494
- for j = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
495
- if not self .arr [x + j ] then self .arr [x + j ] = {} end
496
- if not self .arr [x + j ][y + i ] then
497
- self .arr [x + j ][y + i ] = true
498
- self .num_tiles = self .num_tiles + 1
499
- end
500
- end
501
- end
498
+ self :plot_thickness (x , y , thickness )
502
499
t = t + granularity
503
500
end
504
501
@@ -507,15 +504,8 @@ function Line:cubic_bezier(x0, y0, x1, y1, bezier_point1, bezier_point2, thickne
507
504
0.5 )
508
505
local y_end = math.floor (((1 - 1 ) ^ 3 * y0 + 3 * (1 - 1 ) ^ 2 * 1 * y2 + 3 * (1 - 1 ) * 1 ^ 2 * y3 + 1 ^ 3 * y1 ) +
509
506
0.5 )
510
- for i = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
511
- for j = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
512
- if not self .arr [x_end + j ] then self .arr [x_end + j ] = {} end
513
- if not self .arr [x_end + j ][y_end + i ] then
514
- self .arr [x_end + j ][y_end + i ] = true
515
- self .num_tiles = self .num_tiles + 1
516
- end
517
- end
518
- end
507
+
508
+ self :plot_thickness (x_end , y_end , thickness )
519
509
end
520
510
521
511
function Line :quadratic_bezier (x0 , y0 , x1 , y1 , bezier_point1 , thickness )
@@ -525,15 +515,7 @@ function Line:quadratic_bezier(x0, y0, x1, y1, bezier_point1, thickness)
525
515
while t <= 1 do
526
516
local x = math.floor (((1 - t ) ^ 2 * x0 + 2 * (1 - t ) * t * x2 + t ^ 2 * x1 ) + 0.5 )
527
517
local y = math.floor (((1 - t ) ^ 2 * y0 + 2 * (1 - t ) * t * y2 + t ^ 2 * y1 ) + 0.5 )
528
- for i = 0 , thickness - 1 do
529
- for j = - math.floor (thickness / 2 ), math.ceil (thickness / 2 ) - 1 do
530
- if not self .arr [x + j ] then self .arr [x + j ] = {} end
531
- if not self .arr [x + j ][y + i ] then
532
- self .arr [x + j ][y + i ] = true
533
- self .num_tiles = self .num_tiles + 1
534
- end
535
- end
536
- end
518
+ self :plot_thickness (x , y , thickness )
537
519
t = t + granularity
538
520
end
539
521
end
0 commit comments