Hello,

A small magnet field.

After a while, I got a divison by zero.
Seems to me , is not intercepted.

Uses "ui","math"
#INCLUDE "abc.inc"

OpenWindow 640,480
SetHandleDC hdc,hwnd

Long ys, 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 Distance(x1,y1,x2,y2 As Single) As Single
    Local dx, dy As Single
    dx = x1-x2
    dy = y1-y2
    Return Sqr(dx*dx+dy*dy)
End Function

Function xMove(x,winkel,speed As Single) As Single
    Local x2 As Single
    If winkel=0 Then winkel=1
    x2 = x + cosin(winkel)*speed
    Return x2
End Function

Function yMove(y,winkel,speed As Single) As Single
    Local y2 As Single
    If winkel=0 Then winkel=1
    y2 = y + sinus(winkel)*speed
    Return y2
End Function

Function winkel(x1,y1,x2,y2 As Single) As Single
    Local xhypo, yhypo, abstand, xwinkel As Single
    xhypo = x2 - x1
    yhypo = y2 - y1
    abstand = Distance(x1,y1,x2,y2) 
    xwinkel = Abs(ArcCos(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=1 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 330
    sinus(x+1) = Sin(x)
    cosin(x+1) = Cos(x)
Next

While IsWindow(hwnd) And Key(27)=0 
Canvas_Clear Rgb(205,205,205)
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)
        DrawLine x1, y1, xMagnet(z), yMagnet(z),4, Rgb(255,0,0)
        DrawLine x2, y2, xMagnet(z), yMagnet(z),4, Rgb(0,0,255)
    Next    
Next   
Canvas_Redraw
Wend
Canvas_Window End