Hi all,

I'm trying to find out the cause of a bug in an open source project (LibreCAD). I am at my wit's end.
The problem is (from the perspective of a user) that some arcs are drawn in the wrong direction.
Here is an example:


The left half of the image is the way how it should look like. On the right side you can see the bug.
It's an image of a 0,0/200,100 rectangle which should be filled with a hatch (the hatch is called "arcs")

What some other guys and I found out so far:
#this bug seems to be present in release version only
#may!!! only effect 32bit os

My recent debugging orgy led to these results:
The order of the start point and end point of the arc to be drawn are transposed in the QList "is2":
Qt Code:
  1. QList<std::shared_ptr<RS_Vector> > is2;
To copy to clipboard, switch view to plain text mode 

As "is2" is created/filled in the sort section I think the bug have to be somewhere there.
Here is the code snippet of the sorting:

Qt Code:
  1. ...
  2. // sort the intersection points into is2:
  3. RS_Vector sp = startPoint;
  4. double sa = center.angleTo(sp);
  5. if(ellipse != NULL) sa=ellipse->getEllipseAngle(sp);
  6. QList<std::shared_ptr<RS_Vector> > is2;
  7. bool done;
  8. double minDist;
  9. double dist = 0.0;
  10. std::shared_ptr<RS_Vector> av;
  11. std::shared_ptr<RS_Vector> v;
  12. RS_Vector last = RS_Vector(false);
  13. do {
  14. done = true;
  15. minDist = RS_MAXDOUBLE;
  16. av.reset();
  17. for (int i = 0; i < is.size(); ++i) {
  18. v = is.at(i);
  19. double a;
  20. switch(e->rtti()){
  21. case RS2::EntityLine:
  22. dist = sp.distanceTo(*v);
  23. break;
  24. case RS2::EntityArc:
  25. case RS2::EntityCircle:
  26. a = center.angleTo(*v);
  27. dist = reversed?
  28. fmod(sa - a + 2.*M_PI,2.*M_PI):
  29. fmod(a - sa + 2.*M_PI,2.*M_PI);
  30. break;
  31. case RS2::EntityEllipse:
  32. a = ellipse->getEllipseAngle(*v);
  33. dist = reversed?
  34. fmod(sa - a + 2.*M_PI,2.*M_PI):
  35. fmod(a - sa + 2.*M_PI,2.*M_PI);
  36. break;
  37. default:
  38. break;
  39.  
  40. }
  41.  
  42. if (dist<minDist) {
  43. minDist = dist;
  44. done = false;
  45. av = v;
  46. }
  47. }
  48.  
  49. // copy to sorted list, removing double points
  50. if (!done && av.get()!=NULL) {
  51. if (last.valid==false || last.distanceTo(*av)>RS_TOLERANCE) {
  52. is2.append(std::shared_ptr<RS_Vector>(new RS_Vector(*av)));
  53. last = *av;
  54. }
  55. #if QT_VERSION < 0x040400
  56. emu_qt44_removeOne(is, av);
  57. #else
  58. is.removeOne(av);
  59. #endif
  60.  
  61. av.reset();
  62. }
  63. } while(!done);
  64. ...
To copy to clipboard, switch view to plain text mode 
The sort process should sort the points/elements in the list "is" and add them in the correct order* to "is2".
*the order should be: start point, the point next to the start point, ..., the end point

The funny thing (at least for me) is, when I add qDebug("angle centerToNextIntersection: %f",a);
after these lines :
Qt Code:
  1. case RS2::EntityArc:
  2. case RS2::EntityCircle:
  3. a = center.angleTo(*v);
  4. dist = reversed?
  5. fmod(sa - a + 2.*M_PI,2.*M_PI):
  6. fmod(a - sa + 2.*M_PI,2.*M_PI);
  7. ->here->here->
To copy to clipboard, switch view to plain text mode 
the weird behaviour seems to be gone!

If you would like to have a look at the discussion of this bug in the LibreCAD forum (there are more pictures of the bug):
http://forum.librecad.org/bugs-probl...tp5708894.html

If you would like to have a look at the whole file where the bug is present:
https://github.com/LibreCAD/LibreCAD...e/rs_hatch.cpp
(the sort process begins at about the line 335)

Are the elements of QList added/removed in the wrong way?
What could also lead to a behaviour where the order of QList may become upside down/incorrect?

ANY help is appreciated!
(also hints about things I could try)

using:
ubuntu 12.04 32bit
Compiler: GNU GCC 4.6.3
Qt Version: 4.8.1