From a45a3beb47663e9036b926442e205e81abbae986 Mon Sep 17 00:00:00 2001 From: timb Date: Sun, 24 May 2015 17:03:57 -0500 Subject: circle brush algo --- js/blit.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'js/blit.js') diff --git a/js/blit.js b/js/blit.js index 367d67b..16c62a5 100644 --- a/js/blit.js +++ b/js/blit.js @@ -47,11 +47,26 @@ var blit = (function(){ } }) } + var distance_rect = function(x, y, ratio){ + return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x, 2)) + } + var distance_square = function(x, y, ratio){ + return Math.sqrt((Math.pow(y * ratio, 2)) + Math.pow(x * ratio, 2)) + } blit.circle = function(A, lex){ - var hw = brush.w/2|0, hh = brush.h/2|0 + var hw = brush.w/2, hh = brush.h/2 + var ratio, distance + + if (brush.w === brush.h){ + distance = distance_square + ratio = hw / hh * (brush.w === 3 || brush.w === 5 ? 1.2 : 1.05) + } else { + distance = distance_rect + ratio = hw / hh + } + A.forEach(function(lex,x,y) { - var len = Math.sqrt(Math.pow(x-hw,2)+Math.pow(y-hh,2)) - if (len > Math.abs(hw)) { + if (distance(x - hw + 0.5, y - hh + 0.5, ratio) > hw){ lex.clear() } }) -- cgit v1.2.3-70-g09d2