04-01-2024, 11:44 AM
If you define the ground as a couple of connected lines, you can quite easily calculate the height at any x:
I still haven't read through this thread yet, sorry. Maybe this is similar to what kevin did?
Code:
set window "test", 640, 480
set redraw off
' Create an array of connected lines that cover the width of the window.
lines = []
x0 = 0
y0 = 400
while x0 < 640
x1 = x1 + 16 + rnd(64); y1 = 400 + rnd(80) - 40
lines[sizeof(lines)] = [x0, y0, x1, y1]
x0 = x1; y0 = y1
wend
' Loop until excape is pressed.
while not keydown(KEY_ESCAPE, true)
set color 0, 0, 0
cls
' Draw the lines.
set color 255, 255, 255
for i = 0 to sizeof(lines) - 1
draw line lines[i][0], lines[i][1], lines[i][2], lines[i][3]
next
' Mark the y coordinate of the ground at mouse x.
x = mousex()
y = GetY(lines, x)
set color 0, 255, 0
draw line x, y - 16, x, y
redraw
fwait 60
wend
function GetY(lines, x)
y = 480
for i = 0 to sizeof(lines) - 1
if x >= lines[i][0] and x < lines[i][2]
k = (x - lines[i][0])/(lines[i][2] - lines[i][0])
y = lines[i][1] + k*(lines[i][3] - lines[i][1])
break
endif
next
return y
endfunc
I still haven't read through this thread yet, sorry. Maybe this is similar to what kevin did?