1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
Structure NewPoint
x.l
y.l
dx.l
dy.l
EndStructure
Declare Erreur(Message$)
Declare TracePolygone()
Declare AffPoints()
Declare TestPoint(x1, Y1, X2, Y2, d)
Declare PolygoneConvexe()
Declare Repartition()
Global ScreenHeight, ScreenWidth, ScreenDepth
If ExamineDesktops()
ScreenWidth = DesktopWidth(0)/2
ScreenHeight = DesktopHeight(0)/2
ScreenDepth = DesktopDepth(0)
Else
Erreur("Euh ?")
EndIf
#Nbpoints = 18
#Taille = 16
Global NewList ListPoint.NewPoint()
Global NewList Polygone.NewPoint()
DiametreSelection=6
OpenWindow(0,0,0,ScreenWidth,ScreenHeight,"Polygone")
OpenWindowedScreen(WindowID(0), 0,0,ScreenWidth,ScreenHeight)
Procedure RenderFrame()
Static x, y, x2, y2, Paused, previousElapsed
ClearScreen(0)
ExamineKeyboard()
AffPoints()
PolygoneConvexe()
TracePolygone()
ForEach ListPoint()
DisplayTransparentSprite(0,ListPoint()\x - #Taille, ListPoint()\y - #Taille)
Next
FlipBuffers() ; continue the rendering
EndProcedure
FlipBuffers() ; start the rendering
BindEvent(#PB_Event_RenderFrame, @RenderFrame())
Repartition()
LoadSprite(0, "Data/78-32x32x32.png")
CreateSprite(1,1,1)
StartDrawing(SpriteOutput(1))
Box(0,0,1,1, RGB(255,255,0))
StopDrawing()
;-Procedures
Procedure Erreur(Message$)
;MessageRequester("Erreur", Message$, 0)
Debug Message$
EndProcedure
Procedure Repartition()
;Répartition des boules sur l'écran
For i=1 To #Nbpoints
AddElement(ListPoint())
*MemPos.NewPoint = ListPoint()
MemIndex=ListIndex(ListPoint())
Repeat
Collision = #False
x = #Taille + Random(ScreenWidth - #Taille * 2)
y = #Taille + Random(ScreenHeight - #Taille * 2)
If ListSize(ListPoint()) > 1
ForEach ListPoint()
If ListIndex(ListPoint()) = MemIndex
Continue
EndIf
If Sqr(Pow(ListPoint()\x - x, 2.0) + Pow(ListPoint()\y - y, 2.0)) <= #Taille * 2
Collision = #True
Break
EndIf
Next
EndIf
Until Collision = #False
SelectElement(ListPoint(), MemIndex)
ListPoint()\x = x
ListPoint()\y = y
ListPoint()\dx = 2 + Random(2)
ListPoint()\dy = 2 + Random(2)
Next
EndProcedure
Procedure PolygoneConvexe()
If ListSize(ListPoint()) < 2
ProcedureReturn #False
EndIf
;Initialise
*Min.NewPoint = #Null
*p0.NewPoint = #Null
*pi.NewPoint = #Null
*pc.NewPoint = #Null
;Trouve le point le plus bas dans la liste des points
FirstElement(ListPoint())
*Min = ListPoint()
ForEach ListPoint()
*p0 = ListPoint()
;Mémorise le point le plus bas , ou le plus à gauche s'il y a égalité
If (*p0\y < *Min\y) Or ((*p0\y = *Min\y) And (*p0\x < *Min\x))
*Min = *p0
EndIf
Next
;Initialise la liste pour le contour convexe
ClearList(Polygone())
;Effectue la progression de Jarvis pour calculer le contour
*p0 = *Min
Repeat
;Insertion du nouveau p0 dans le contour convexe
If AddElement(Polygone()) = 0
Erreur("plus de mémoire pour ajouter un élément dans polygone")
Else
Polygone()\x = *p0\x
Polygone()\y = *p0\y
EndIf
;Trouve le point pc dans le sens des aiguilles d'une montre
*pc = #Null
ForEach ListPoint()
*pi = ListPoint()
;Saute p0
If *pi = *p0
Continue
EndIf
;Sélectionne le premier point
If *pc = #Null
*pc = ListPoint()
Continue
EndIf
;Teste si pi est dans le sens des aiguilles d'une montre par rapport à pc
z=(((*pi\x - *p0\x) * (*pc\y - *p0\y)) - ((*pi\y - *p0\y) * (*pc\x - *p0\x)))
If z > 0
;pi est dans le sens des aiguilles d'une montre par rapport à pc
*pc = *pi
ElseIf z = 0
;Si pi et pc sont colinéaires , on choisit le plus éloigné de p0
longueurpi = Pow(*pi\x - *p0\x, 2.0) + Pow(*pi\y - *p0\y, 2.0)
longueurpc = Pow(*pc\x - *p0\x, 2.0) + Pow(*pc\y - *p0\y, 2.0)
If longueurpi > longueurpc
*pc = *pi
EndIf
EndIf
Next
;Cherche le point suivant
*p0 = *pc
Until *p0 = *Min
EndProcedure
Procedure DoLine(xi,yi,xf,yf)
x = xi
y = yi
dx = xf - xi
dy = yf - yi
If dx > 0
xinc = 1
Else
xinc = -1
EndIf
If dy > 0
yinc = 1
Else
yinc = -1
EndIf
dx = Abs(dx)
dy = Abs(dy)
DisplaySprite(1,x,y)
If dx > dy
cumul = dx / 2
For i = 1 To dx
x + xinc
cumul + dy
If (cumul >= dx)
cumul - dx
y + yinc
EndIf
DisplaySprite(1,x,y)
Next
Else
cumul = dy / 2
For i = 1 To dy
y + yinc
cumul + dx
If cumul >= dy
cumul - dy
x + xinc
EndIf
DisplaySprite(1,x,y)
Next
EndIf
EndProcedure
Procedure TracePolygone()
SelectElement(Polygone(), 0)
*mem0.NewPoint = Polygone()
*mem.NewPoint = Polygone()
While NextElement(Polygone())
DoLine(*mem\x, *mem\y, Polygone()\x, Polygone()\y)
*mem = Polygone()
Wend
DoLine(*mem0\x, *mem0\y, *mem\x, *mem\y)
EndProcedure
Procedure.l LimiteX(*Valeur.NewPoint, Min.l, Max.l)
If *Valeur\x < Min
*Valeur\x = Min
ProcedureReturn #True
ElseIf *Valeur\x > Max
*Valeur\x = Max
ProcedureReturn #True
EndIf
EndProcedure
Procedure AffPoints()
CouleurPoint = RGB(200, 255, 0)
Taille2 = #Taille / 2
ForEach ListPoint()
ListPoint()\x + ListPoint()\dx
ListPoint()\y + ListPoint()\dy
If ListPoint()\x< #Taille
ListPoint()\x = #Taille
ListPoint()\dx * -1
ElseIf ListPoint()\x > ScreenWidth - #Taille
ListPoint()\x = ScreenWidth - #Taille
ListPoint()\dx * -1
EndIf
If ListPoint()\y< #Taille
ListPoint()\y = #Taille
ListPoint()\dy * -1
ElseIf ListPoint()\y > ScreenHeight - #Taille
ListPoint()\y = ScreenHeight - #Taille
ListPoint()\dy * -1
EndIf
*MemPos.NewPoint=ListPoint()
MemIndex=ListIndex(ListPoint())
ForEach ListPoint()
If ListIndex(ListPoint()) = MemIndex
Continue
EndIf
;Calcul la distance
Distance = Sqr(Pow(ListPoint()\x - *MemPos\x, 2.0) + Pow(ListPoint()\y - *MemPos\y, 2.0))
If Distance <= #Taille * 2
*MemPos\dx * -1
*MemPos\dy * -1
*MemPos\x + *MemPos\dx
*MemPos\y + *MemPos\dy
EndIf
Next
SelectElement(ListPoint(), MemIndex)
Next
EndProcedure
Procedure TestPoint(x1, Y1, X2, Y2, d)
If x1 > X2 - d And x1 < X2 + d And Y1 > Y2 - d And Y1 < Y2 + d
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure |
Partager