Converting double to int correctly in drawing



I'm doing a program in C that does some drawing.
Depending on the values of 'start' and 'stop' I want to draw a
rectangle of
appropriate height.

'start' and 'stop' are integers and has values in the interval [0,
MAX]
'height' is the maxium height of the rectangle, it will have this
height if
start = 0 and stop = MAX.

x = 0;
width = 100;
draw_rectangle(x, (double)start/MAX * height, width, (double)(stop -
start)/MAX * height);

But draw_rectangle takes ints, so if the expressions have decimals
they will be
truncated, and the resultning rectangle will not be right. For example
if start = 10 and
stop = MAX it will not (depending on the actual value of MAX) draw the
rectangle all
the way down, it misses the last pixel.

So my question is how to fix this, how to convert correctly to ints? I
can't just add
one pixel to the end, since my code works for values where the
expressions does not
contain decimals.

Thanks!
.



Relevant Pages

  • Smooth scaling of System.Drawing.Pen object?
    ... The code below draws with a Graphics object that is ... If I draw a rectangle with a pen wider than 1 ... the resultant sides are just 1 pixel thick. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Create transparent action buttons
    ... What I normally do is click and draw a small rectangle or oval (depending on ... Microsoft PowerPoint MVP ...
    (microsoft.public.powerpoint)
  • Re: Calculate the interior of a rectangle
    ... Apart from a slightly esoteric case which really doesn't have a bearing on this and which I won't therefore mention, a pixel is the smallest unit that can be addressed on the display. ... You also need to take into account which method you are using to draw your stuff, and in some cases you need to do that for all lines, even single pixel lines. ... For example, if you are using a single pixel line thickness and you draw a rectangle using the VB Line method with coordinates - you will not get exactly the same sized rectangle as you would get if you instead drew it using the API Rectangle method with exactly the same - coordinates. ... For example, if you draw a rectangle using such a pen then the rectangle coordinates you specify will effectively describe the outer edge of the lines of drawn rectangle, not the centre point, no matter how thick the line is. ...
    (microsoft.public.vb.general.discussion)
  • Re: Rectangle odd behavior
    ... The rectangle draws the line 1 pixel wide and draws it to the top-left edge ... DrawRectangle which means that Pen.Alignment does not work for 1 pixel width ... pens. ... > If you draw this rectangle on the screen you will end up with a rectangle ...
    (microsoft.public.dotnet.framework.drawing)
  • Rectangle odd behavior
    ... I am having a hard time understanding the logic behind the Rectangle object. ... If you draw this rectangle on the screen you will end up with a rectangle ... like the one shown below (The character "X" represents a pixel used to draw ...
    (microsoft.public.dotnet.languages.csharp)