Ich kann nur folgendes Beispiel hier aus dem Forum beisteuren weiß aber nicht mehr genau ob es aus dem Forum oder Code Archiv ist.
Code: Alles auswählen
Structure XY
x.f
y.f
EndStructure
#FACTOR = 300.0
#TRANSL = 0.5 ; >0.5=Täler, <0.5=Berge
#PIXELS = 100 ; hier bis 500!
Global Dim ALine.f(#PIXELS)
Global mv.f, d.l, max.l
Procedure Split(low.l, high.l)
Protected m.l
m = (high + low) / 2
d = high - low
If d <= 1
ProcedureReturn 0
EndIf
mv = (ALine(high) + ALine(low)) / 2.0
ALine(m) = mv - (Random(10000) / 10000.0 - #TRANSL) * #FACTOR * d / (max)
Split(m, high)
Split(low, m)
EndProcedure
Procedure.f LineLineCol(*P.XY, *r.XY, *A.XY, *B.XY, *OUT.XY)
Protected t.f, s.f
Protected AB.XY
s = *A\x * *r\y - *A\y * *r\x
t = (s - *P\x * *r\y + *P\y * *r\x) / (s - *B\x * *r\y + *B\y * *r\x)
AB\x = *B\x - *A\x
AB\y = *B\y - *A\y
If t >= -0.0001 And t <= 1.0001
*OUT\x = *A\x + t * AB\x
*OUT\y = *A\y + t * AB\y
ProcedureReturn Pow(*OUT\x - *P\x, 2) + Pow(*OUT\y - *P\y, 2)
EndIf
ProcedureReturn 99999999.0
EndProcedure
Procedure.l LineCollideArray(*Start.XY, *Dir.XY, *Out.XY)
Protected i.l, shortestDis.f, t.f
Protected A.XY, B.XY, Out2.XY
shortestDis = 99999999.0
For i = 0 To #PIXELS - 1
A\y = 1.0 + ALine(i) + 250.0
A\x = 1.0 * i / #PIXELS * 500
B\y = 1.0 + ALine(i + 1) + 250.0
B\x = 1.0 * (i + 1) / #PIXELS * 500
t = LineLineCol(*Start, *Dir, @A, @B, @Out2)
If t < shortestDis
shortestDis = t
*Out\x = Out2\x
*Out\y = Out2\y
EndIf
Next
EndProcedure
max = #PIXELS
Split(0, #PIXELS)
CreateImage(0, 500, 500)
OpenWindow(0, 200, 200, 500, 500, "Drawing", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CreateGadgetList(WindowID(0))
ImageGadget(1, 0, 0, 500, 500, ImageID(0))
SetGadgetState(1, ImageID(0))
Repeat
hdc = StartDrawing(ImageOutput(0))
Box(0,0, 500,500, 0)
For z = 1 To #PIXELS
LineXY(1.0 * (z - 1) / #PIXELS * 500, ALine(z - 1) + 250, 1.0 * z / #PIXELS * 500, ALine(z) + 250, $FF)
Next
P.XY
P\x = 250
P\y = 0
Dir.XY
Dir\x = WindowMouseX(0) - 250
Dir\y = WindowMouseY(0)
Out.XY
LineCollideArray(@P, @Dir, @Out)
Circle(Out\x, Out\y, 10, $FF00)
Line(P\x, P\y, Dir\x, Dir\y, $FF0000)
StopDrawing()
SetGadgetState(1, ImageID(0))
Delay(10)
Until WindowEvent() = #PB_Event_CloseWindow