StrengthLine.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include <stdexcept>
  2. #include "StrengthLine.hpp"
  3. StrengthLine::StrengthLine (QPoint * p1, QPoint * p2)
  4. {
  5. if (p1 == NULL || p2 == NULL)
  6. throw std::
  7. invalid_argument ("Points defining a strength line can't be NULL.");
  8. if (p1->x () == p2->x () && p1->y () == p2->y ())
  9. throw std::
  10. invalid_argument ("Points defining a strength line can't be equals.");
  11. this->p1 = p1;
  12. this->p2 = p2;
  13. }
  14. std::pair < float, float >
  15. StrengthLine::equation () const
  16. {
  17. // This method can be called only if a and b are
  18. // not aligned into a vertical or horizontal line
  19. float a = ((p1->y () - p2->y ()) * 1.0) / (p1->x () - p2->x ());
  20. float b = p1->y () - a * p1->x ();
  21. return std::make_pair (a, b);
  22. }
  23. std::pair < QPoint *, QPoint * >StrengthLine::toDraw (QImage * img) const
  24. {
  25. // Methode qui va calculer les points pour l'affichage des droites
  26. // sur l'image passées en paramètre. Calcul rapide donc
  27. // les points peuvent sortir de l'image.
  28. // ligne verticale
  29. if (p1->x () == p2->x ())
  30. return std::make_pair (new QPoint (p1->x (), 0),
  31. new QPoint (p1->x (), img->height ()));
  32. // ligne horizontale
  33. if (p1->y () == p2->y ())
  34. return std::make_pair (new QPoint (0, p1->y ()),
  35. new QPoint (img->width (), p1->y ()));
  36. // Equation de la droite et on prend les points gauche et droite de la ligne
  37. std::pair < float, float >
  38. eq = this->equation ();
  39. float
  40. a = std::get < 0 > (eq);
  41. float
  42. b = std::get < 1 > (eq);
  43. return std::make_pair (new QPoint (0, b),
  44. new QPoint (img->width (), a * img->width () + b));
  45. }
  46. std::pair < QPoint *, QPoint * >StrengthLine::interpolateToEdge (QImage * img) const
  47. {
  48. // Methode qui va calculer les points sur les bords des images
  49. // en fonction des deux points attributs qui définissent
  50. // la ligne.
  51. // ligne verticale
  52. if (p1->x () == p2->x ())
  53. return std::make_pair (new QPoint (p1->x (), 0),
  54. new QPoint (p1->x (), img->height ()));
  55. // ligne horizontale
  56. if (p1->y () == p2->y ())
  57. return std::make_pair (new QPoint (0, p1->y ()),
  58. new QPoint (img->width (), p1->y ()));
  59. // Equation de la droite et on prend les points gauche et droite de la ligne
  60. std::pair < float, float >
  61. eq = this->equation ();
  62. float
  63. a = std::get < 0 > (eq);
  64. float
  65. b = std::get < 1 > (eq);
  66. if(a>0)
  67. {
  68. if(b<0)
  69. {
  70. if((a*img->width()+b) > img->height())
  71. {
  72. // |------------------------------------------|
  73. // | * |
  74. // | * |
  75. // | * |
  76. // | * |
  77. // | * |
  78. // | * |
  79. // | * |
  80. // | * |
  81. // | * |
  82. // |------------------------------------------|
  83. return std::make_pair (new QPoint (-1*(b/a), 0),
  84. new QPoint ((img->height()-b)/a, img->height ()));
  85. }
  86. else
  87. {
  88. // |------------------------------------------|
  89. // | *** |
  90. // | *** |
  91. // | *** |
  92. // | *** |
  93. // | ***|
  94. // | |
  95. // | |
  96. // | |
  97. // | |
  98. // |------------------------------------------|
  99. return std::make_pair (new QPoint (-1*(b/a), 0),
  100. new QPoint (img->width (), a * img->width () + b));
  101. }
  102. }
  103. else // a>0 and b>0
  104. {
  105. if((a*img->width()+b) > img->height())
  106. {
  107. // |------------------------------------------|
  108. // | |
  109. // | |
  110. // |* |
  111. // | * |
  112. // | * |
  113. // | * |
  114. // | * |
  115. // | * |
  116. // | * |
  117. // |------------------------------------------|
  118. return std::make_pair (new QPoint (0, b),
  119. new QPoint ((img->height () -b )/a, img->height ()));
  120. }
  121. else
  122. {
  123. // |------------------------------------------|
  124. // | |
  125. // |******** |
  126. // | ******** |
  127. // | ******** |
  128. // | ******** |
  129. // | ******** |
  130. // | **|
  131. // | |
  132. // | |
  133. // |------------------------------------------|
  134. return std::make_pair (new QPoint (0, b),
  135. new QPoint (img->width(), a*img->width ()+b));
  136. }
  137. }
  138. }
  139. else // a<0
  140. {
  141. if((a*img->width()+b)>0)
  142. {
  143. if(b > img->height())
  144. {
  145. // |------------------------------------------|
  146. // | |
  147. // | |
  148. // | * |
  149. // | * |
  150. // | * |
  151. // | * |
  152. // | * |
  153. // | * |
  154. // | * |
  155. // |------------------------------------------|
  156. return std::make_pair (new QPoint ((img->height()-b)/a, img->height()),
  157. new QPoint (img->width(), a*img->width()+b));
  158. }
  159. else
  160. {
  161. // |------------------------------------------|
  162. // | |
  163. // | |
  164. // | **|
  165. // | ******** |
  166. // | ******** |
  167. // | ******** |
  168. // | ******** |
  169. // |******** |
  170. // | |
  171. // |------------------------------------------|
  172. return std::make_pair (new QPoint (0, b),
  173. new QPoint (img->width (), a * img->width () + b));
  174. }
  175. }
  176. else // a>0 and (a*img->width()+b) < 0
  177. {
  178. if(b > img->height())
  179. {
  180. // |------------------------------------------|
  181. // | * |
  182. // | * |
  183. // | * |
  184. // | * |
  185. // | * |
  186. // | * |
  187. // | * |
  188. // | * |
  189. // | * |
  190. // |------------------------------------------|
  191. return std::make_pair (new QPoint (-1*(b/a), 0),
  192. new QPoint ((img->height()-b)/a, img->height ()));
  193. }
  194. else
  195. {
  196. // |------------------------------------------|
  197. // | * |
  198. // | * |
  199. // | * |
  200. // | * |
  201. // |* |
  202. // | |
  203. // | |
  204. // | |
  205. // | |
  206. // |------------------------------------------|
  207. return std::make_pair (new QPoint (0, b),
  208. new QPoint (-1*(b/a), 0));
  209. }
  210. }
  211. }
  212. return std::make_pair (new QPoint (*p1),
  213. new QPoint (*p2));
  214. }