yea, just determine which 2 points 'X' is between. If you don't know which points, then use 1 and numOfPts to recursively find it. Since the points are technically sorted by 'X', we can use a binary chop method; which is very fast, even with thousands of points to search. It's because the number of entries to check is cut in half at each pass. Probably the best method for when working with sorted data.
If you want the edges to meet, the I'd suggest making them the same height. Take the height of the 2 edges, average them, then set that as the height for each.
Move your mouse on the X-axis to see the circle trace over the line at the proper height.
REM =============================================
REM === 1D Midpoint Displacement example ===
REM === Author: Phaelax (phaelax@hotmail.com) ===
REM =============================================
set display mode 1024,768,16
randomize timer()
depth = 5
rem make this global so the smoothing function can read it
rem why? because array count() doesn't work on 2d arrays
numOfPts = 2^depth + 1
global PT_COUNT = numOfPts
dim points(numOfPts,2)
rem set begin and end points
points(1,1) = 1 : points(1,2) = 500
points(numOfPts,1) = 1000 : points(numOfPts,2) = 500
do
rem call the midpoint displacement function
midpoint(points(1,1),points(1,2),points(numOfPts,1),points(numOfPts,2),500,200,0.6,depth,1,numOfPts)
repeat
cls
rem draw the new line
for t = 1 to numOfPts-1
line points(t,1),points(t,2), points(t+1,1),points(t+1,2)
next t
x = mousex()
y = getHeight#(x,1,numOfPts)
circle x,y,5
if inkey$() = "s" and flag = 0 then flag=1:smooth()
if inkey$() <> "s" then flag = 0
until spacekey()
loop
rem recursive method
rem (x1,y1) - starting point of line
rem (x2,y2) - ending point of line
rem baseline - the base or ground of where the points start
rem height# - places midpoint Y-coord randomly between 0 and height#
rem h# - 0 < h# <= 1 : higher makes a very ridged line, lower makes smoother
rem depth - how many times should the line be split
rem arr1 - used for storing values in array
rem arr2 - used for storing values in array
function midpoint(x1,y1,x2,y2,baseline,height#,h#,depth, arr1,arr2)
x = x1 + (x2-x1)/2
y = baseline - rnd(height#)
arrIndex = arr1 + (arr2-arr1)/2
points(arrIndex,1) = x
points(arrIndex,2) = y
dec depth, 1
if depth > 0
midpoint(x1,y1,x,y,baseline,height#*h#,h#,depth,arr1,arrIndex)
midpoint(x,y,x2,y2,baseline,height#*h#,h#,depth,arrIndex,arr2)
endif
endfunction
function smooth()
rem define an array to temporarily store the points.
local dim temp(PT_COUNT)
for i = 2 to PT_COUNT - 1
y = points(i,2)
y1 = points(i-1,2)
y2 = points(i+1,2)
temp(i) = (y+y1+y2)/3
next i
for i = 2 to PT_COUNT - 1
points(i,2) = temp(i)
next i
endfunction
function getHeight#(x as float, arr1, arr2)
if (arr2 - arr1) = 1
t# = (x - points(arr1,1)) / (points(arr2,1)-points(arr1,1))
height# = points(arr1,2) + (points(arr2,2)-points(arr1,2))*t#
exitfunction height#
endif
arr = arr1 + (arr2-arr1)/2
if x = points(arr,1)
height# = points(arr, 1)
exitfunction height#
endif
if x > points(arr,1)
height# = getHeight#(x,arr,arr2)
else
height# = getHeight#(x,arr1,arr)
endif
endfunction height#
"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike