Advertisement
Guest User

Untitled

a guest
May 29th, 2015
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. void Clip1Left(point &A, point &B, point Pmin, point Pmax)
  2. {
  3. A.y = B.y - (B.x - Pmin.x) * (B.y - A.y) / (B.x - A.x);
  4. A.x = Pmin.x;
  5. }
  6.  
  7. void Clip1Top(point &A, point &B, point Pmin, point Pmax)
  8. {
  9. A.x = B.x - (B.y - Pmax.y) * (B.x - A.x) / (B.y - A.y);
  10. A.y = Pmax.y;
  11. }
  12.  
  13. void Clip1Bottom(point &A, point &B, point Pmin, point Pmax)
  14. {
  15. A.x = B.x - (B.y - Pmin.y) * (B.x - A.x) / (B.y - A.y);
  16. A.y = Pmin.y;
  17. }
  18.  
  19. void Clip2Right(point A, point &B, point Pmin, point Pmax)
  20. {
  21. B.y = A.y + (Pmax.x - A.x) * (B.y - A.y) / (B.x - A.x);
  22. B.x = Pmax.x;
  23. }
  24.  
  25. void Clip2Top(point A, point &B, point Pmin, point Pmax)
  26. {
  27. B.x = A.x + (Pmax.y - A.y) * (B.x - A.x) / (B.y - A.y);
  28. B.y = Pmax.y;
  29. }
  30.  
  31. void Clip2Bottom(point A, point &B, point Pmin, point Pmax)
  32. {
  33. B.x = A.x + (Pmin.y - A.y) * (B.x - A.x) / (B.y - A.y);
  34. B.y = Pmin.y;
  35. }
  36.  
  37. // FC-Algorithm
  38. bool clip(point &A, point &B, point Pmin, point Pmax)
  39. {
  40. // 1
  41. if (A.x > B.x) std::swap(A, B);
  42.  
  43. // 2
  44. int C1 = 0;
  45. if (A.x < Pmin.x) C1 += 1;
  46. if (A.x > Pmax.x) C1 += 2;
  47. if (A.y < Pmin.y) C1 += 4;
  48. if (A.y > Pmax.y) C1 += 8;
  49.  
  50. int C2 = 0;
  51. if (B.x < Pmin.x) C2 += 1;
  52. if (B.x > Pmax.x) C2 += 2;
  53. if (B.y < Pmin.y) C2 += 4;
  54. if (B.y > Pmax.y) C2 += 8;
  55.  
  56. // 3
  57. if (C1 & C2) return false;
  58.  
  59. // 4
  60. int C = C1 * 16 + C2;
  61.  
  62. switch (C) {
  63. // 5
  64. case 0x00: return true;
  65.  
  66. // 6
  67. case 0x02: {
  68. Clip2Right(A, B, Pmin, Pmax);
  69. return true;
  70. }
  71.  
  72. // 7
  73. case 0x04: {
  74. Clip2Bottom(A, B, Pmin, Pmax);
  75. return true;
  76. }
  77.  
  78. // 8
  79. case 0x06: {
  80. Clip2Right(A, B, Pmin, Pmax);
  81.  
  82. if (B.y < Pmin.y) Clip2Bottom(A, B, Pmin, Pmax);
  83. return true;
  84. }
  85.  
  86. // 9
  87. case 0x08: {
  88. Clip2Top(A, B, Pmin, Pmax);
  89. return true;
  90. }
  91.  
  92. // 10
  93. case 0x0A: {
  94. Clip2Right(A, B, Pmin, Pmax);
  95.  
  96. if (B.y > Pmax.y) Clip2Top(A, B, Pmin, Pmax);
  97. return true;
  98. }
  99.  
  100. // 11
  101. case 0x10: {
  102. Clip1Left(A, B, Pmin, Pmax);
  103. return true;
  104. }
  105.  
  106. // 12
  107. case 0x12: {
  108. Clip1Left(A, B, Pmin, Pmax);
  109. Clip2Right(A, B, Pmin, Pmax);
  110. return true;
  111. }
  112.  
  113. // 13
  114. case 0x14: {
  115. Clip1Left(A, B, Pmin, Pmax);
  116.  
  117. if (A.y < Pmin.y) return false;
  118. else Clip2Bottom(A, B, Pmin, Pmax);
  119. return true;
  120. }
  121.  
  122. // 14
  123. case 0x16: {
  124. Clip1Left(A, B, Pmin, Pmax);
  125.  
  126. if (A.y < Pmin.y) return false;
  127. else Clip2Bottom(A, B, Pmin, Pmax);
  128.  
  129. if (B.x > Pmax.x) Clip2Right(A, B, Pmin, Pmax);
  130. return true;
  131. }
  132.  
  133. // 15
  134. case 0x18: {
  135. Clip1Left(A, B, Pmin, Pmax);
  136.  
  137. if (A.y > Pmax.y) return false;
  138. else Clip2Top(A, B, Pmin, Pmax);
  139. return true;
  140. }
  141.  
  142. // 16
  143. case 0x1A: {
  144. Clip1Left(A, B, Pmin, Pmax);
  145.  
  146. if (A.y > Pmax.y) return false;
  147. else Clip2Top(A, B, Pmin, Pmax);
  148.  
  149. if (B.x > Pmax.x) Clip2Right(A, B, Pmin, Pmax);
  150. return true;
  151. }
  152.  
  153. // 17
  154. case 0x40: {
  155. Clip1Bottom(A, B, Pmin, Pmax);
  156. return true;
  157. }
  158.  
  159. // 18
  160. case 0x42: {
  161. Clip1Bottom(A, B, Pmin, Pmax);
  162.  
  163. if (A.x > Pmax.x) return false;
  164. else Clip2Right(A, B, Pmin, Pmax);
  165. return true;
  166. }
  167.  
  168. // 19
  169. case 0x48: {
  170. Clip1Bottom(A, B, Pmin, Pmax);
  171. Clip2Top(A, B, Pmin, Pmax);
  172. return true;
  173. }
  174.  
  175. // 20
  176. case 0x4A: {
  177. Clip1Bottom(A, B, Pmin, Pmax);
  178.  
  179. if (A.x > Pmax.x) return false;
  180. else Clip2Right(A, B, Pmin, Pmax);
  181.  
  182. if (B.y > Pmax.y) Clip2Top(A, B, Pmin, Pmax);
  183.  
  184. return true;
  185. }
  186.  
  187. // 21
  188. case 0x50: {
  189. Clip1Left(A, B, Pmin, Pmax);
  190.  
  191. if (A.y < Pmin.y) Clip1Bottom(A, B, Pmin, Pmax);
  192.  
  193. return true;
  194. }
  195.  
  196. // 22
  197. case 0x52: {
  198. Clip1Bottom(A, B, Pmin, Pmax);
  199.  
  200. if (A.x > Pmax.x) return false;
  201. else if (A.x < Pmin.x) Clip1Left(A, B, Pmin, Pmax);
  202.  
  203. Clip2Right(A, B, Pmin, Pmax);
  204. return true;
  205. }
  206.  
  207. // 23
  208. case 0x58: {
  209. Clip1Left(A, B, Pmin, Pmax);
  210.  
  211. if (A.y > Pmax.y) return false;
  212. else if (A.y < Pmin.y) Clip1Bottom(A, B, Pmin, Pmax);
  213.  
  214. Clip2Top(A, B, Pmin, Pmax);
  215. return true;
  216. }
  217.  
  218. // 24
  219. case 0x5A: {
  220. Clip1Left(A, B, Pmin, Pmax);
  221.  
  222. if (A.y > Pmax.y) return false;
  223. else if (A.y < Pmin.y) {
  224. Clip1Bottom(A, B, Pmin, Pmax);
  225. if (A.x > Pmax.x) return false;
  226. }
  227.  
  228. Clip2Top(A, B, Pmin, Pmax);
  229.  
  230. if (B.x > Pmax.x) Clip2Right(A, B, Pmin, Pmax);
  231. return true;
  232. }
  233.  
  234. // 25
  235. case 0x80: {
  236. Clip1Top(A, B, Pmin, Pmax);
  237. return true;
  238. }
  239.  
  240. // 26
  241. case 0x82: {
  242. Clip1Top(A, B, Pmin, Pmax);
  243.  
  244. if (A.x > Pmax.x) return false;
  245. else Clip2Right(A, B, Pmin, Pmax);
  246. return true;
  247. }
  248.  
  249. // 27
  250. case 0x84: {
  251. Clip1Top(A, B, Pmin, Pmax);
  252. Clip2Bottom(A, B, Pmin, Pmax);
  253. return true;
  254. }
  255.  
  256. // 28
  257. case 0x86: {
  258. Clip1Top(A, B, Pmin, Pmax);
  259.  
  260. if (A.x > Pmax.x) return false;
  261. else Clip2Right(A, B, Pmin, Pmax);
  262.  
  263. if (B.y < Pmin.y) Clip2Bottom(A, B, Pmin, Pmax);
  264. return true;
  265. }
  266.  
  267. // 29
  268. case 0x90: {
  269. Clip1Left(A, B, Pmin, Pmax);
  270.  
  271. if (A.y > Pmax.y) Clip1Top(A, B, Pmin, Pmax);
  272. return true;
  273. }
  274.  
  275. // 30
  276. case 0x92: {
  277. Clip1Top(A, B, Pmin, Pmax);
  278.  
  279. if (A.x > Pmax.x) return false;
  280. else if (A.x < Pmin.x) Clip1Left(A, B, Pmin, Pmax);
  281.  
  282. Clip2Right(A, B, Pmin, Pmax);
  283. return true;
  284. }
  285.  
  286. // 31
  287. case 0x94: {
  288. Clip1Left(A, B, Pmin, Pmax);
  289.  
  290. if (A.y < Pmin.y) return false;
  291. else if (A.y > Pmax.y) Clip1Top(A, B, Pmin, Pmax);
  292.  
  293. Clip2Bottom(A, B, Pmin, Pmax);
  294. return true;
  295. }
  296.  
  297. // 32
  298. case 0x96: {
  299. Clip1Left(A, B, Pmin, Pmax);
  300.  
  301. if (A.y < Pmin.y) return false;
  302. else if (A.y > Pmax.y) {
  303. Clip1Top(A, B, Pmin, Pmax);
  304. if (A.x > Pmax.x) return false;
  305. }
  306.  
  307. Clip2Bottom(A, B, Pmin, Pmax);
  308.  
  309. if (B.x > Pmax.x) Clip2Right(A, B, Pmin, Pmax);
  310. return true;
  311. }
  312. }
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement