Ooops
January 23, 2021, 04:39:54 AM

Author Topic: [bb] Filled Quad by starfox [ 1+ years ago ]  (Read 445 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Filled Quad by starfox [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Filled Quad
Author : starfox
Posted : 1+ years ago

Description : Takes four points (any position) and fills it with a quadrilateral, useful for pseudo 3d stuff. Also comes with an optional parameter gridprecise to determine how precise to make the quad (smaller the more precise, but slower). This function is pretty fast.

Code :
Code: BlitzBasic
  1. Function FilledQuad(p1x,p1y,p2x,p2y,p3x,p3y,p4x,p4y,gridprecise=2)
  2. Local farleftx,farlefty ;Find the point farthest to the left
  3. Local farrightx,farrighty ;Find the point farthest to the right
  4. Local mid1x,mid1y ;Find the point second farthest to the left
  5. Local mid2x,mid2y ;Find the point second farthest to the right
  6. Local farleftid,farrightid,mid1id,mid2id
  7.  
  8. ;Find parallel points
  9. par1x = p1x
  10. par1y = p1y
  11. par2x = p4x
  12. par2y = p4y
  13.  
  14. If Abs(par1x-p1x) < 2
  15. count = count + 1
  16. EndIf
  17. If Abs(par1x-p2x) < 2
  18. count = count + 1
  19. EndIf
  20. If Abs(par1x-p3x) < 2
  21. count = count + 1
  22. EndIf
  23. If Abs(par1x-p4x) < 2
  24. count = count + 1
  25. EndIf
  26. If count >= 3 Then Return ;Can't do anything to that!
  27. count=0
  28. If Abs(par2x-p1x) < 2
  29. count = count + 1
  30. EndIf
  31. If Abs(par2x-p2x) < 2
  32. count = count + 1
  33. EndIf
  34. If Abs(par2x-p3x) < 2
  35. count = count + 1
  36. EndIf
  37. If Abs(par2x-p4x) < 2
  38. count = count + 1
  39. EndIf
  40. If count >= 3 Then Return ;Can't do anything to that!
  41. count=0
  42. If Abs(par1y-p1y) < 2
  43. count = count + 1
  44. EndIf
  45. If Abs(par1y-p2y) < 2
  46. count = count + 1
  47. EndIf
  48. If Abs(par1y-p3y) < 2
  49. count = count + 1
  50. EndIf
  51. If Abs(par1y-p4y) < 2
  52. count = count + 1
  53. EndIf
  54. If count >= 3 Then Return ;Can't do anything to that!
  55. count=0
  56. If Abs(par2y-p1y) < 2
  57. count = count + 1
  58. EndIf
  59. If Abs(par2y-p2y) < 2
  60. count = count + 1
  61. EndIf
  62. If Abs(par2y-p3y) < 2
  63. count = count + 1
  64. EndIf
  65. If Abs(par2y-p4y) < 2
  66. count = count + 1
  67. EndIf
  68. If count >= 3 Then Return ;Can't do anything to that!
  69.  
  70. ;Long unneccesary function to detect locations:p
  71.  
  72. farleftx = p1x
  73. farlefty = p1y
  74. farleftid=1
  75.  
  76. If farleftx > p2x
  77. farleftx = p2x
  78. farlefty = p2y
  79. farleftid=2
  80. EndIf
  81.  
  82. If farleftx > p3x
  83. farleftx = p3x
  84. farlefty = p3y
  85. farleftid=3
  86. EndIf
  87.  
  88. If farleftx > p4x
  89. farleftx = p4x
  90. farlefty = p4y
  91. farleftid=4
  92. EndIf
  93.  
  94. farrightx = p1x
  95. farrighty = p1y
  96. farrightid=1
  97.  
  98. If farrightx < p2x
  99. farrightx = p2x
  100. farrighty = p2y
  101. farrightid=2
  102. EndIf
  103.  
  104. If farrightx < p3x
  105. farrightx = p3x
  106. farrighty = p3y
  107. farrightid=3
  108. EndIf
  109.  
  110. If farrightx < p4x
  111. farrightx = p4x
  112. farrighty = p4y
  113. farrightid=4
  114. EndIf
  115.  
  116. If farleftid <> 1 And farrightid <> 1
  117. mid1x = p1x
  118. mid1y = p1y
  119. mid1id=1
  120. EndIf
  121.  
  122. If farleftid <> 2 And farrightid <> 2
  123. If mid1id <> 0
  124. mid2x = p2x
  125. mid2y = p2y
  126. mid2id=2
  127. Else
  128. mid1x = p2x
  129. mid1y = p2y
  130. Mid1id = 2
  131. EndIf
  132. EndIf
  133.  
  134. If farleftid <> 3 And farrightid <> 3
  135. If mid1id <> 0
  136. mid2x = p3x
  137. mid2y = p3y
  138. mid2id=3
  139. Else
  140. mid1x = p3x
  141. mid1y = p3y
  142. Mid1id = 3
  143. EndIf
  144. EndIf
  145.  
  146. If farleftid <> 4 And farrightid <> 4
  147. If mid1id <> 0
  148. mid2x = p4x
  149. mid2y = p4y
  150. mid2id=4
  151. Else
  152. mid1x = p4x
  153. mid1y = p4y
  154. Mid1id = 4
  155. EndIf
  156. EndIf
  157.  
  158. col = ray2dintersect(farleftx,farlefty,mid1x,mid1y,mid2x,mid2y,farrightx,farrighty)
  159. If col = 1 Then changdir=1
  160. col = ray2dintersect(farleftx,farlefty,mid2x,mid2y,mid1x,mid1y,farrightx,farrighty)
  161. If col = 1 Then changdir=2
  162.  
  163. ;2D vectors
  164. Local vec1x#,vec1y#,vec1step#
  165. Local vec2x#,vec2y#,vec2step#
  166.  
  167. ;Quad Picture
  168. ;   O---------O
  169. ;  /         /
  170. ; /         /
  171. ;O---------O
  172.  
  173. vec1x = farleftx:vec1y = farlefty
  174. vec2x = farleftx:vec2y = farlefty
  175.  
  176.  
  177. dist1# = Abs(mid1x-farleftx)
  178. If dist1 = 0 Then dist1 = 1
  179. dist2# = Abs(mid2x-farleftx)
  180. If dist2 = 0 Then dist2 = 1
  181. alldist = Abs(farrightx-farleftx)
  182.  
  183. vec1step = (mid1y-farlefty)/(dist1)
  184. vec2step = (mid2y-farlefty)/(dist2)
  185.  
  186. If mid1x-farleftx = 0
  187. vec1y = vec1y + vec1step
  188. EndIf
  189.  
  190. If mid2x-farleftx = 0
  191. vec2y = vec2y + vec2step
  192. EndIf
  193.  
  194. If changdir = 2
  195. dist2# = Abs(farrightx-farleftx)
  196. If dist2 = 0 Then dist2 = 1
  197. vec2step = (farrighty-farlefty)/(dist2)
  198.  
  199. If farrightx-farleftx = 0
  200. vec2y = vec2y + vec2step
  201. EndIf
  202.  
  203. ElseIf changdir = 1
  204. dist1# = Abs(farrightx-farleftx)
  205. If dist1 = 0 Then dist1 = 1
  206. vec1step = (farrighty-farlefty)/(dist1)
  207.  
  208. If farrightx-farleftx = 0
  209. vec1y = vec1y + vec1step
  210. EndIf
  211.  
  212. EndIf
  213.  
  214. starttea = farleftx
  215. endtea = farrightx
  216.  
  217. For tea =  starttea To endtea
  218.  
  219. If tea Mod gridprecise = 0
  220. If vec1y < vec2y
  221.  
  222.         If tea >= 0 And tea <= 640
  223.         If vec1y >= 0 And vec1y <= 480
  224.         Rect(tea,vec1y,gridprecise,vec2y-vec1y+1)
  225.         EndIf
  226.         EndIf
  227. Else
  228.         If tea >= 0 And tea <= 640
  229.         If vec2y >= 0 And vec2y <= 480
  230.         Rect(tea,vec2y,gridprecise,vec1y-vec2y+1)
  231.         EndIf
  232.         EndIf
  233. EndIf
  234. EndIf
  235.  
  236.         If tea >= mid1x And gotomid1=0
  237.         If changdir=0
  238.         dist1# = (farrightx-mid1x)
  239.         If dist1 = 0 Then dist1 = 1
  240.         vec1step = (farrighty-mid1y)/(dist1)
  241.         ElseIf changdir = 2
  242.                 dist1# = (mid2x-mid1x)
  243.                 If dist1 = 0 Then dist1 = 1
  244.                 vec1step = (mid2y-mid1y)/(dist1)
  245.         ElseIf changdir = 1
  246.                 dist2# = (farrightx-mid1x)
  247.                 If dist2 = 0 Then dist2 = 1
  248.                 vec2step = (farrighty-mid1y)/(dist2)
  249.         EndIf
  250.         gotomid1=1
  251.         EndIf
  252.        
  253.         If tea >= mid2x And gotomid2=0
  254.         If changdir=0
  255.         dist2# = (farrightx-mid2x)
  256.         If dist2 = 0 Then dist2 = 1
  257.         vec2step = (farrighty-mid2y)/(dist2)
  258.         ElseIf changdir=2
  259.                 dist1# = (farrightx-mid2x)
  260.                 If dist1 = 0 Then dist1 = 1
  261.                 vec1step = (farrighty-mid2y)/(dist1)
  262.         ElseIf changdir=1
  263.                 dist2# = (mid1x-mid2x)
  264.                 If dist2 = 0 Then dist2 = 1
  265.                 vec2step = (mid1y-mid2y)/(dist2)
  266.         EndIf
  267.         gotomid2=1
  268.         EndIf
  269.        
  270.         vec1y = vec1y + vec1step
  271.         vec2y = vec2y + vec2step
  272.        
  273. Next
  274.  
  275.  
  276. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal