Skip to content

Commit 62d61b4

Browse files
authored
Merge pull request #1450 from ryanbennitt/bugfix/line_bulge
Fix line thickness extending outside map
2 parents 9f76bc9 + 65e130c commit 62d61b4

File tree

2 files changed

+35
-51
lines changed

2 files changed

+35
-51
lines changed

changelog.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Template for new versions:
2020

2121
## Fixes
2222

23+
- `gui/design`: prevent line thickness from extending outside the map boundary
24+
2325
## Misc Improvements
2426

2527
## Removed

internal/design/shapes.lua

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -404,41 +404,46 @@ end
404404

405405
LineDrawer = defclass(LineDrawer, Shape)
406406

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+
407420
function LineDrawer:plot_bresenham(x0, y0, x1, y1, thickness)
408421
local dx = math.abs(x1 - x0)
409422
local dy = math.abs(y1 - y0)
410423
local sx = x0 < x1 and 1 or -1
411424
local sy = y0 < y1 and 1 or -1
412425
local e2, x, y
413426

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)
426432

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
430436

431-
e2 = 2 * err
437+
e2 = 2 * err
432438

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
437443

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
442447
end
443448
end
444449
end
@@ -490,15 +495,7 @@ function Line:cubic_bezier(x0, y0, x1, y1, bezier_point1, bezier_point2, thickne
490495
0.5)
491496
local y = math.floor(((1 - t) ^ 3 * y0 + 3 * (1 - t) ^ 2 * t * y2 + 3 * (1 - t) * t ^ 2 * y3 + t ^ 3 * y1) +
492497
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)
502499
t = t + granularity
503500
end
504501

@@ -507,15 +504,8 @@ function Line:cubic_bezier(x0, y0, x1, y1, bezier_point1, bezier_point2, thickne
507504
0.5)
508505
local y_end = math.floor(((1 - 1) ^ 3 * y0 + 3 * (1 - 1) ^ 2 * 1 * y2 + 3 * (1 - 1) * 1 ^ 2 * y3 + 1 ^ 3 * y1) +
509506
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)
519509
end
520510

521511
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)
525515
while t <= 1 do
526516
local x = math.floor(((1 - t) ^ 2 * x0 + 2 * (1 - t) * t * x2 + t ^ 2 * x1) + 0.5)
527517
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)
537519
t = t + granularity
538520
end
539521
end

0 commit comments

Comments
 (0)