Jump to content

Ramer-Douglas-Peucker line simplification: Difference between revisions

Added BASIC256
m (→‎{{header|Wren}}: Minor tidy)
(Added BASIC256)
Line 127:
[ ( 0, 0 ) ( 2, -0.1 ) ( 3, 5 ) ( 7, 9 ) ( 8, 9 ) ( 9, 9 ) ]
</pre>
 
=={{header|BASIC256}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">arraybase 1
global matriz
dim matriz(10, 3)
matriz[ 1, 1] = 0 : matriz[ 1, 2] = 0 : matriz[ 1, 3] = 0
matriz[ 2, 1] = 1 : matriz[ 2, 2] = 0.1 : matriz[ 2, 3] = 0
matriz[ 3, 1] = 2 : matriz[ 3, 2] = -0.1 : matriz[ 3, 3] = 0
matriz[ 4, 1] = 3 : matriz[ 4, 2] = 5 : matriz[ 4, 3] = 0
matriz[ 5, 1] = 4 : matriz[ 5, 2] = 6 : matriz[ 5, 3] = 0
matriz[ 6, 1] = 5 : matriz[ 6, 2] = 7 : matriz[ 6, 3] = 0
matriz[ 7, 1] = 6 : matriz[ 7, 2] = 8.1 : matriz[ 7, 3] = 0
matriz[ 8, 1] = 7 : matriz[ 8, 2] = 9 : matriz[ 8, 3] = 0
matriz[ 9, 1] = 8 : matriz[ 9, 2] = 9 : matriz[ 9, 3] = 0
matriz[10, 1] = 9 : matriz[10, 2] = 9 : matriz[10, 3] = 0
 
call DRDP(matriz, 1, 10, 1)
 
print "Remaining points after simplifying:"
matriz[1, 3] = true
matriz[10, 3] = true
for i = 1 to matriz[?][]
if matriz[i, 3] = true then print "(";matriz[i, 1];", "; matriz[i, 2];") ";
next i
end
 
function DistanciaPerpendicular(tabla, i, ini, fin)
dx = tabla[fin, 1] - tabla[ini, 1]
dy = tabla[fin, 2] - tabla[ini, 2]
 
#Normalise
mag = (dx^2 + dy^2)^0.5
if mag > 0 then dx /= mag : dy /= mag
 
pvx = tabla[i, 1] - tabla[ini, 1]
pvy = tabla[i, 2] - tabla[ini, 2]
 
#Get dot product (project pv onto normalized direction)
pvdot = dx * pvx + dy * pvy
 
#Scale line direction vector
dsx = pvdot * dx
dsy = pvdot * dy
 
#Subtract this from pv
ax = pvx - dsx
ay = pvy - dsy
return (ax^2 + ay^2)^0.5
end function
 
subroutine DRDP(matriz, ini, fin, epsilon)
dmax = 0
 
# Find the point with the maximum distance
if matriz[?][] < 2 then print "Not enough points to simplify": end
 
for i = ini + 1 to fin
d = DistanciaPerpendicular(matriz, i, ini, fin)
if d > dmax then indice = i : dmax = d
next
 
# If max distance is greater than epsilon, recursively simplify
if dmax > epsilon then
matriz[indice, 3] = true
# Recursive call
call DRDP(matriz, ini, indice, epsilon)
call DRDP(matriz, indice, fin, epsilon)
end if
end subroutine</syntaxhighlight>
{{out}}
<pre>Remaining points after simplifying:
(0, 0) (2, -0.1) (3, 5) (7, 9) (9, 9) </pre>
 
=={{header|C}}==
2,169

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.