SHOW:
|
|
- or go back to the newest paste.
1 | - | -- drafter.lua -- stateless approach |
1 | + | -- drafter.lua -- factory approach |
2 | ||
3 | local drafter = {} | |
4 | ||
5 | - | function drafter.line(startX, startY, endX, endY, maxLength) |
5 | + | function drafter.line(maxLength) |
6 | - | local deltaX, deltaY = endX - startX, endY - startY |
6 | + | return function(startX, startY, endX, endY) |
7 | - | local requestedLength = math.sqrt(deltaX * deltaX + deltaY * deltaY) |
7 | + | local deltaX, deltaY = endX - startX, endY - startY |
8 | - | if requestedLength > maxLength then |
8 | + | local requestedLength = math.sqrt(deltaX * deltaX + deltaY * deltaY) |
9 | - | local ratio = maxLength / requestedLength |
9 | + | if requestedLength > maxLength then |
10 | - | endX, endY = startX + deltaX * ratio, startY + deltaY * ratio |
10 | + | local ratio = maxLength / requestedLength |
11 | endX, endY = startX + deltaX * ratio, startY + deltaY * ratio | |
12 | - | display.newLine(startX, startY, endX, endY) |
12 | + | end |
13 | display.newLine(startX, startY, endX, endY) | |
14 | end | |
15 | end | |
16 | ||
17 | return drafter |