Libraryless. Click here for Pure Java version (9616L/53K).
1 | // Reconstructed off the top of my head (not checking any literature or other code)... because I'm a pro |
2 | srecord noeq BresenhamLineDrawer(int x1, int y1, int x2, int y2) extends Meta is Iterable<Pt> { |
3 | int mainStepX, mainStepY; |
4 | int sideStepX, sideStepY; |
5 | int i; |
6 | int x, y; |
7 | int fraction, increment, denominator; |
8 | // denominator is also number of pixels to return (minus 1) |
9 | |
10 | *(Pt start, Pt end) { |
11 | x1 = start.x; |
12 | y1 = start.y; |
13 | x2 = end.x; |
14 | y2 = end.y; |
15 | } |
16 | |
17 | simplyCached void init { |
18 | x = x1; y = y1; |
19 | if (abs(x2-x1) > abs(y2-y1)) { |
20 | // go mainly horizontal (right or left) |
21 | mainStepX = sideStepX = sign(x2-x1); |
22 | sideStepY = sign(y2-y1); |
23 | denominator = abs(x2-x1); |
24 | increment = abs(y2-y1); |
25 | } else { |
26 | // go mainly vertical (up or down) |
27 | mainStepY = sideStepY = sign(y2-y1); |
28 | sideStepX = sign(x2-x1); |
29 | denominator = abs(y2-y1); |
30 | increment = abs(x2-x1); |
31 | } |
32 | fraction = denominator/2; |
33 | if (scaffolding()) printVars BresenhamLineDrawer(+x1, +y1, +x2, +y2, +denominator); |
34 | } |
35 | |
36 | Pt next() { |
37 | init(); |
38 | |
39 | if (i > denominator) null; |
40 | |
41 | Pt p = pt(x, y); |
42 | if (i++ < denominator) { |
43 | fraction += increment; |
44 | if (fraction >= denominator) { |
45 | fraction -= denominator; |
46 | x += sideStepX; |
47 | y += sideStepY; |
48 | } else { |
49 | x += mainStepX; |
50 | y += mainStepY; |
51 | } |
52 | if (scaffolding()) |
53 | printVars BresenhamLineDrawer(+i, +fraction, +increment, +denominator, +x, +y, +p); |
54 | } |
55 | |
56 | ret p; |
57 | } |
58 | |
59 | public ItIt<Pt> iterator() { ret iff_null(-> next()); } |
60 | |
61 | public PtBuffer points() { ret new PtBuffer(iterator()); } |
62 | } |
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034441 |
Snippet name: | BresenhamLineDrawer - abstract line drawing algorithm |
Eternal ID of this version: | #1034441/25 |
Text MD5: | 1c7f9c084efddbd04a705325129ad455 |
Transpilation MD5: | b349ef3a1e00fd678afb143897b25b95 |
Author: | stefan |
Category: | javax / imaging |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-05-21 16:25:52 |
Source code size: | 1732 bytes / 62 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 191 / 374 |
Version history: | 24 change(s) |
Referenced in: | [show references] |