From 88b6101e9ae93d1477e9b4c87d17c64e95b290b4 Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Thu, 31 Oct 2013 12:25:49 -0400 Subject: [PATCH] DDA: reduce code duplication and simplify. There are three locations in the code that repeat a pattern of "If z=0 then use 2d-approx(dx,dy), else if x==0 && y==0 then use dz, else use 3d-approx". Teach approx_distance_3 to detect these conditions for us and apply the same logic. Replace the three call locations with a simple call to approx_distance_3. Binary size for the LOOKAHEAD case drops by almost 400 bytes: old: FLASH : 21242 bytes 149% 70% 34% 17% new: FLASH : 20844 bytes 146% 68% 33% 17% The size for non-LOOKAHEAD drops by 40 bytes: old: FLASH : 16592 bytes 116% 55% 27% 13% new: FLASH : 16552 bytes 116% 54% 27% 13% We can actually do a little better if we consider the zero-ness of all three axes, but this does make the code a little bit bigger. Another change will consider that option. This change simply tries to mimic the existing functionality. --- dda_lookahead.c | 30 ++++++++---------------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/dda_lookahead.c b/dda_lookahead.c index 840657b..e5d849e 100644 --- a/dda_lookahead.c +++ b/dda_lookahead.c @@ -183,29 +183,15 @@ void dda_find_crossing_speed(DDA *prev, DDA *current) { // Find out movement distances. // TODO: remember these from dda_start(); - if (prev->delta_um.Z == 0) - prev_distance = approx_distance( - prev->x_direction ? prev->delta_um.X : - prev->delta_um.X, - prev->y_direction ? prev->delta_um.Y : - prev->delta_um.Y); - else if (prev->delta_um.X == 0 && prev->delta_um.Y == 0) - prev_distance = prev->z_direction ? prev->delta_um.Z : - prev->delta_um.Z; - else - prev_distance = approx_distance_3( - prev->x_direction ? prev->delta_um.X : - prev->delta_um.X, - prev->y_direction ? prev->delta_um.Y : - prev->delta_um.Y, - prev->z_direction ? prev->delta_um.Z : - prev->delta_um.Z); + prev_distance = approx_distance_3( + prev->x_direction ? prev->delta_um.X : - prev->delta_um.X, + prev->y_direction ? prev->delta_um.Y : - prev->delta_um.Y, + prev->z_direction ? prev->delta_um.Z : - prev->delta_um.Z); - if (current->delta_um.Z == 0) - curr_distance = approx_distance( - current->x_direction ? current->delta_um.X : - current->delta_um.X, - current->y_direction ? current->delta_um.Y : - current->delta_um.Y); - else if (current->delta_um.X == 0 && current->delta_um.Y == 0) - curr_distance = current->z_direction ? current->delta_um.Z : - current->delta_um.Z; - else - curr_distance = approx_distance_3( - current->x_direction ? current->delta_um.X : - current->delta_um.X, - current->y_direction ? current->delta_um.Y : - current->delta_um.Y, - current->z_direction ? current->delta_um.Z : - current->delta_um.Z); + curr_distance = approx_distance_3( + current->x_direction ? current->delta_um.X : - current->delta_um.X, + current->y_direction ? current->delta_um.Y : - current->delta_um.Y, + current->z_direction ? current->delta_um.Z : - current->delta_um.Z); if (DEBUG_DDA && (debug_flags & DEBUG_DDA)) sersendf_P(PSTR("Distance: %lu, then %lu\n"), prev_distance, curr_distance);