kicad-developers team mailing list archive
Mailing list archive
Guidelines for KiROUND-ing
Looking around for angles I noticed a thing... especially when computing
distance (like using hypot or a whole sqrt expression), sometimes the
result is KiROUND-ed while often a simple (int) truncation is done.
Simple example (from the plotting routines, where rounding *would* matter
start.x = centre.x + (int) ( radius * cos( -alpha ) );
start.y = centre.y + (int) ( radius * sin( -alpha ) );
IMHO these should be KiROUNDed, instead of casted. The same appears
often when computing, for example, a circle radius; since we have good
geometric functions we could go from (this is from the autorouter):
radius = (int) sqrt( (double) ( cx - ux0 ) * ( cx - ux0 )
+ (double) ( cy - uy0 ) * ( cy - uy0 ) );
radius = KiROUND( Distance( ux0, uy0, cx, cy ) );
which I hope everyone agree is better (if only for the expression itself).
Are there situations where some geometrical variable should *not* be rounded
and instead just cast to int? The standard defines 'truncation' as the
behaviour (http://en.cppreference.com/w/cpp/language/implicit_cast), so it
could be useful to have a 'rounding toward zero' behaviour; however I'd use an
explicit call to trunc if that was an important and wanted behaviour (just like
I often see and esplicit cast to double when an int is passed as a double
argument... I presume that's for highlighting the conversion).