user interface - C# Math conversions for pixels and frames -


so i'm working on c# tool need calculate user clicks in timeline , current number within range. end result should 'integer' since based on frames.

enter image description here

is correct way writing code this? not sure if there simpler way of writing it. 'float' tags seem bit much. how convert integer @ end? seems bit inaccurate...

        int ctrlx = e.x; // cursors x pos relative control         float perct = ((float)e.x / (float)this.width); // control pixel width         float normalizedwidth = this.maxvalue - this.minvalue;         float val = this.minvalue + (perct * normalizedwidth);         label1.text = val.tostring(); 

just this:

label1.text = ((int)val).tostring(); 

if need round or down, use math.ceiling or math.floor first!

or simplify whole thing to:

var result = this.minvalue + (int)( ( e.x / (double)this.width ) * ( this.maxvalue - this.minvalue ); label1.text = result.tostring(); 

also note might x values outside control (seeing how keeps mouse capture while mouse down), might want care that. (edit: if allow user drag value, is)


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -