Skip to content

Commit

Permalink
improvement to bresenham algoritm
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Quiceno committed Apr 19, 2016
1 parent 931c893 commit 3f89e4b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2016 @alvaropinot
Copyright (c) 2016 @nquicenob

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 7 additions & 4 deletions src/bresenham.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ function getInitValues(startPoint, finalPoint){
}
}

function inLoop(sign, currentP, finalP){
return sign < 0 ? currentP >= finalP : currentP <= finalP;
function getBreakFn(sign){
return sign < 0 ?
(current, final) => current >= final :
(current, final) => current <= final;
}

function calcMainCoordinates(absDiff){
return absDiff.x > absDiff.y ? ['x', 'y'] : ['y', 'x'];
}


export function* bresenhamLine(point, finalPoint) {
const { absDiff, sign } = getInitValues(point, finalPoint);
const [ mainCoordinate, coordinate ] = calcMainCoordinates(absDiff);
Expand All @@ -40,12 +41,14 @@ export function* bresenhamLine(point, finalPoint) {
const mainDiff = absDiff[mainCoordinate];
const secondDiff = absDiff[coordinate];

const breakFn = getBreakFn(mainSign);

let mainValue = point[mainCoordinate];
let secondValue = point[coordinate];

let eps = 0;

for( ; inLoop(mainSign, mainValue, final); mainValue += mainSign) {
for( ; breakFn(mainValue, final); mainValue += mainSign) {
yield {
[mainCoordinate]: mainValue,
[coordinate]: secondValue
Expand Down
1 change: 1 addition & 0 deletions test.bresenham.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function buildArray(fn, startPoint, finalPoint){
return arr;
}


test('Bresenham\'s line algoritm ', (assert) => {

[
Expand Down

0 comments on commit 3f89e4b

Please sign in to comment.