_TITLE "Smooth SUB test code" ' B+ started 2020-01-24 adapted and modified from ' Curve smoother by STxAxTIC https://www.qb64.org/forum/index.php?topic=184.msg963#msg963
' I want a Toolbox Sub that will take an array of points for a curve and smooth them out.
' 2020-01-25 add getClick, fcirc and modify thic to test single click points data.
'reinitialize everything for next curve test of Smooth
clean
numPoints = 0
'get bunch of clicks until enter keypress
x = -1
getClick x, y, k
IF x
<> -1 THEN AppendXY pts
(), x
, y: numPoints
= numPoints
+ 1:
CIRCLE (x
, y
), 1, &HFFFF00FF
drawcurve pts(), 10, &HFFFFFF00
'======================= Feature SUB =======================================================================
' This code takes a dynamic points array and adds and modifies points to smooth out the data,
' to be used as Toolbox SUB. b+ 2020-01-24 adapted and modified from:
' Curve smoother by STxAxTIC https://www.qb64.org/forum/index.php?topic=184.msg963#msg963
'TYPE XY
' x AS SINGLE
' y AS SINGLE
'END TYPE
' targetPoints is the number of points to be in finished smoothed out array
' smoothIterations is number of times to try and round out corners
DIM rad2Max
, kmax
, k
, numPoints
, xfac
, yfac
, rad2
, j
'
' Determine the pair of neighboring points that have the greatest separation of all pairs.
'
rad2Max = -1
kmax = -1
FOR k
= 1 TO numPoints
- 1 xfac = arr(k).x - arr(k + 1).x
yfac = arr(k).y - arr(k + 1).y
rad2 = xfac ^ 2 + yfac ^ 2
kmax = k
rad2Max = rad2
'
' Starting next to kmax, create a `gap' by shifting all other points by one index.
'
arr(j + 1).x = arr(j).x
arr(j + 1).y = arr(j).y
'
' Fill the gap with a new point whose position is determined by the average of its neighbors.
'
arr(kmax + 1).x = .5 * (arr(kmax).x + arr(kmax + 2).x)
arr(kmax + 1).y = .5 * (arr(kmax).y + arr(kmax + 2).y)
numPoints = numPoints + 1
'
' At this stage, the curve still has all of its sharp edges. Use a `relaxation method' to smooth.
' The new position of a point is equal to the average position of its neighboring points.
'
FOR j
= 1 TO smoothIterations
FOR k
= 2 TO numPoints
- 1 temp(k).x = .5 * (arr(k - 1).x + arr(k + 1).x)
temp(k).y = .5 * (arr(k - 1).y + arr(k + 1).y)
FOR k
= 2 TO numPoints
- 1 arr(k).x = temp(k).x
arr(k).y = temp(k).y
yCP 10, "Click mouse to hearts content or 500 times. Escape quits testing."
yCP 30, "Press enter to test curve drawing..."
yCP 50, "b+ Smooth SUB that uses STxAxTIC's Curve Smoother code."
yCP 600 - 30, "Curve will disappear in 5 secs or keypress then Click your way to another curve."
thic arr(k).x, arr(k).y, arr(k + 1).x, arr(k + 1).y, thk, c
'update 2020-01-24 to include PD2 inside the sub
PD2 = 1.570796326794897
t2 = thick / 2
x3
= x1
+ t2
* COS(a
+ PD2
) y3
= y1
+ t2
* SIN(a
+ PD2
) x4
= x1
+ t2
* COS(a
- PD2
) y4
= y1
+ t2
* SIN(a
- PD2
) x5
= x2
+ t2
* COS(a
+ PD2
) y5
= y2
+ t2
* SIN(a
+ PD2
) x6
= x2
+ t2
* COS(a
- PD2
) y6
= y2
+ t2
* SIN(a
- PD2
) ftri x6, y6, x4, y4, x3, y3, K
ftri x3, y3, x5, y5, x6, y6, K
fcirc x1, y1, t2, K 'added on for this app
fcirc x2, y2, t2, K ' ditto
'2019-12-16 fix by Steve saves some time with STATIC and saves and restores last dest
_BLEND a&
'<<<< new 2019-12-16 fix
'update 2020-01-24 now with _printwidth so can use any FONT
SUB yCP
(y
, s$
) 'for xmax pixel wide graphics screen Center Print at pixel y row
' modified for XY type
SUB AppendXY
(arr
() AS XY
, addx
, addy
)
'getClick returns the mouse x, y position WHEN THE MOUSE WAS RELEASED! or keypress ASC 27 or 32 to 125
'2019-08-06 Test now with new mBox and inputBox procedures
'found mBox needed a _KEYCLEAR, how about inputBox? OK had _KEYCLEAR already
mx = -1: my = -1: q = 0
'IF mb THEN
'from Steve Gold standard
Radius
= ABS(R
): RadiusError
= -Radius: X
= Radius: Y
= 0 LINE (CX
- X
, CY
)-(CX
+ X
, CY
), C
, BF
RadiusError = RadiusError + Y * 2 + 1
LINE (CX
- Y
, CY
- X
)-(CX
+ Y
, CY
- X
), C
, BF
LINE (CX
- Y
, CY
+ X
)-(CX
+ Y
, CY
+ X
), C
, BF
X = X - 1
RadiusError = RadiusError - X * 2
Y = Y + 1
LINE (CX
- X
, CY
- Y
)-(CX
+ X
, CY
- Y
), C
, BF
LINE (CX
- X
, CY
+ Y
)-(CX
+ X
, CY
+ Y
), C
, BF