Libraryless. Click here for Pure Java version (9616L/53K).
// Reconstructed off the top of my head (not checking any literature or other code)... because I'm a pro srecord noeq BresenhamLineDrawer(int x1, int y1, int x2, int y2) extends Meta is Iterable<Pt> { int mainStepX, mainStepY; int sideStepX, sideStepY; int i; int x, y; int fraction, increment, denominator; // denominator is also number of pixels to return (minus 1) *(Pt start, Pt end) { x1 = start.x; y1 = start.y; x2 = end.x; y2 = end.y; } simplyCached void init { x = x1; y = y1; if (abs(x2-x1) > abs(y2-y1)) { // go mainly horizontal (right or left) mainStepX = sideStepX = sign(x2-x1); sideStepY = sign(y2-y1); denominator = abs(x2-x1); increment = abs(y2-y1); } else { // go mainly vertical (up or down) mainStepY = sideStepY = sign(y2-y1); sideStepX = sign(x2-x1); denominator = abs(y2-y1); increment = abs(x2-x1); } fraction = denominator/2; if (scaffolding()) printVars BresenhamLineDrawer(+x1, +y1, +x2, +y2, +denominator); } Pt next() { init(); if (i > denominator) null; Pt p = pt(x, y); if (i++ < denominator) { fraction += increment; if (fraction >= denominator) { fraction -= denominator; x += sideStepX; y += sideStepY; } else { x += mainStepX; y += mainStepY; } if (scaffolding()) printVars BresenhamLineDrawer(+i, +fraction, +increment, +denominator, +x, +y, +p); } ret p; } public ItIt<Pt> iterator() { ret iff_null(-> next()); } public PtBuffer points() { ret new PtBuffer(iterator()); } }
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: | 190 / 373 |
Version history: | 24 change(s) |
Referenced in: | [show references] |