/*  Magnetimus By Peter Wirbelauer 28.o6.2o12   Germany  */
uses gdip

Screen 640, 480, 1
sys Font= LoadFont ("times",28)
SetSmoothMode 4

sys x, y, z, single x1, y1, x2, y2, winkel2 
dim xMagnet(768) as single
dim yMagnet(768) as single
dim wMagnet(768) as single

dim sinus(360) as single
dim cosin(360) as single

Function sgn (single a) as single 
single sg 
iF a < 0  
   sg= -1
Else  
   sg = 1
End iF
Return sg
End Function

Function Distance(single x1,y1,x2,y2) as single
single dx, dy
dx = x1-x2
dy = y1-y2
Return sqr(dx*dx+dy*dy)
End Function

Function xMove(single x,winkel,speed) as single
single x2
x2 = x + Cosin(winkel)*speed
return x2
End Function

Function yMove(single y,winkel,speed) as single
single y2
y2 = y + Sinus(winkel)*speed
return y2
End Function

Function winkel(single x1,y1,x2,y2) as single
single xhypo, yhypo, abstand, xwinkel
xhypo = x2 - x1
yhypo = y2 - y1
abstand = Distance x1,y1,x2,y2 
xwinkel = abs(acos(xhypo/abstand))
if sgn(xhypo) = -1 and sgn(yhypo) = -1 then xwinkel =180 + (180 - xwinkel)
if sgn(xhypo) =  1 and sgn(yhypo) = -1 then xwinkel =270 + ( 90 - xwinkel)
return xwinkel
End Function

for x=0 to 31
  for y=0 to 23
    z=y*32+x
    xMagnet(z) = x*20
    yMagnet(z) = y*20
  next
next   

for x=0 to 360
  sinus(x) = sin(x)
  cosin(x) = cos(x)
next

While Key(27)=0 
ClsColor 255,255,255,255
for x=1 to 31
  for y=1 to 23
    z=y*32+x
    wMagnet(z) = winkel(xMagnet(z),yMagnet(z),xMouse,yMouse)
    x1 = xMove(xMagnet(z), wMagnet(z), 8)
    y1 = yMove(yMagnet(z), wMagnet(z), 8)
    winkel2 = wMagnet(z) - 180
    if winkel2 < 0 then winkel2 =360 + winkel2
    x2 = xMove(xMagnet(z), winkel2, 8)
    y2 = yMove(yMagnet(z), winkel2, 8)
    Line x1, y1, xMagnet(z), yMagnet(z),2, ARGB(100,255,0,0)
    Line x2, y2, xMagnet(z), yMagnet(z),2, ARGB(100,0,0,255)
   next    
next   
Flip
SetFps 60
Wend
Ends