Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have to round value and store it, because the c++ does not has round function I use this:
C++
floor(x+0.5)


but when I use it in my code it does not work:

C#
void algo(float xx1, float yy1, float xx2, float yy2, float xx[], float yy[])
{
   double mm, m1, m2;
   m1 = yy2 - yy1;
   m2 = xx2 - xx1;
   mm = m1 / m2;
   yy[0] = yy1;
   xx[0] = xx1;

   if((mm>=-1)&&(mm<=1))
   {
      for(int i=0; i<=xx2; i++)
      {
         yy[i + 1] = floor(yy[i] + mm + 0.5);
         xx[i + 1] = xx[i] + 1;
      }
   }
   else
   {
      for(int i=0; i<=xx2; i++)
      {
         xx[i + 1] = floor(xx[i] + (1/mm) + 0.5);
         yy[i + 1] = yy[i] + 1;
      }
   }
}
Posted
Updated 13-Apr-12 9:14am
v2
Comments
[no name] 13-Apr-12 8:31am    
What do you mean by does not work? Can you give examples of input and output?
sami-syria 13-Apr-12 8:57am    
this function is part of lines drawing algorithms
when run this code without floor with this in put:xx1=2,yy1=2 xx2=7,yy2=5
I get this : xx=2 3 4 5 6 7
yy=2 2.6 3.2 3.8 4.4 5
but when I use floor I get this:
xx= 2 3 4 5 6 7
yy= 2 3 4 5 6 7

thank you

You'll find a pretty good answer here:
http://www.cplusplus.com/forum/articles/3638/[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
CPallini 13-Apr-12 8:22am    
Good one, 5.
Espen Harlinn 13-Apr-12 8:24am    
Thank you Pallini :-D
sami-syria 13-Apr-12 8:59am    
Thank you for your interest sir
i well try it
VJ Reddy 14-Apr-12 21:23pm    
Good reference. +5
Espen Harlinn 15-Apr-12 4:39am    
Thanks VJ :-D
If you put this in the debugger which is the FIRST thing to do when debugging a program you will find floor() is working properly.

The line

yy[i+1]=floor(yy[i]+mm+0.5);



does completely different things when you use floor() because yy[i+1] is dependent on yy[i].

You are generating a different series altogether.

Your algorithm is incorrect and needs work.
 
Share this answer
 
v2
Comments
Aescleal 14-Apr-12 5:33am    
All the talk of rounding and Bresenham's algorithm aside it's nice that someone spotted a potential code error. I was sitting here thinking "hang on, what he's trying should work."

A 5 to you Sir.
[no name] 14-Apr-12 8:47am    
Thanks for your kind remarks.
Espen Harlinn 15-Apr-12 18:37pm    
Oh, I'm nearly blushing - I feel it's a bit embarrassing that I didn't spot that one, because now that you've mentioned it - it kind of leaps of the screen :)
Nicely spotted!
[no name] 15-Apr-12 20:10pm    
Thanks - most bugs are like that.
Espen has already contributed a good link regarding the rounding. Thanks Espen, that got my 5.

The reason I post this solution is to make you aware that there is better algorithm for line drawing that you might not be aware of. It was invented by Jack E. Bresenham and is named after him.

Bresenham's Algorithm in Wikipedia

The nice thing about this algorithm is that it runs completely with integer operations. And that adds tremendously to its speed. Every conversion between floating-point format and integer is always a relatively slow operation. Or in your case you need a floating-point add (+0.5) a function call to floor, and probably later on when it comes to the real drawing a conversion to int. So for serious graphics work I would suggest you take a look at Bresenham's algorithm. It's not hard to convert the pseudo-code given on the Wiki page into C code.

Back to the subject of rounding: I have found the implicit conversion from double to int by the C runtime system sometimes a very slow operation. That was true VC++ V6; don't know if that is still so in newer versions. As a good work-around I found somewhere on CodeProject the following code (sorry that I cannot give credit to the author; might have been the following article: Floating-Point utilities):

inline long RoundInt (double a)
{
    long retval;
    __asm fld a
    __asm fistp retval
    return retval;
}


This little inline function translates into only two machine instructions and is according to my measurements way faster than floor (x + 0.5) and the following implicit conversion to int.

Hope that helped you a little further along the way.
 
Share this answer
 
Comments
Espen Harlinn 13-Apr-12 17:31pm    
Thanks for your note of confidence, now I just answered his immediate concern and this goes a bit further - so you've been 5'ed! :-D

OP can find a c# implmentation of Bresenham's Algorithm in XNAImageShape.cs which is part of the source code for http://www.codeproject.com/Articles/279718/Windows-Phone-Are-you-Game-Part-1

Converting the method
public void DrawLineBresenham(int x1, int y1, int x2, int y2, int color)
to c should take about one minute :-D
nv3 14-Apr-12 2:39am    
Thanks Espen! Good links.
Aescleal 14-Apr-12 5:32am    
Got a 5 from me, Bresenham's algorithm was the first bit of graphics programming I did years ago and it's so elegant that anyone recommending it is a gentleman and scholar in my book.

Cheers,

Ash
nv3 14-Apr-12 5:58am    
Thanks Ash! I fully agree about the elegance of Bresenham's algorithm and have used it on several occasions, even some not directly correlated to line drawing.
thank you all for your kindness the problem was solved...the program has 3 functions for 3 line drawing Algorithms: simple, digital differential analyzer and Bresenham's Algorithm to compare the execution time for each one.

thank you again
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900