Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bezier Path Generator
#2
After playing around with some other Bezier code it would seem this is normal. I streamlined the program above to be much faster and yield different numbers of points when using AND or OR in line 30 of the code.

When using AND the curve is very smooth when connected with lines. However, some detail is lost if successive X or Y coordinates are equal, not ideal for enemy ship path following but great when connecting the points with lines.

When using OR the data points are perfect for path following.

Code: (Select All)
TYPE POINT
    x AS INTEGER
    y AS INTEGER
END TYPE

DIM Curve(3) AS POINT
REDIM Path(1) AS POINT

DIM p AS POINT

Curve(0).x = 0
Curve(0).y = 479

Curve(1).x = 639
Curve(1).y = 239

Curve(2).x = 0
Curve(2).y = 239

Curve(3).x = 639
Curve(3).y = 0

SCREEN _NEWIMAGE(640, 480, 32)

Detail = .0001

t = 0
DO
    CurvePoint Curve(), p, t
    IF p.x <> Path(UBOUND(Path) - 1).x OR p.y <> Path(UBOUND(Path) - 1).y THEN ' OR can be changed to AND for different results
        Path(UBOUND(Path)) = p
        REDIM _PRESERVE Path(UBOUND(Path) + 1) AS POINT
        PSET (p.x, p.y)
    END IF
    t = t + Detail
LOOP UNTIL t > 1

'PSET (Path(1).x, Path(1).y) '        Draw lines instead when using AND above (very smooth)
'FOR i = 2 TO UBOUND(Path) - 1 '      OR yeilds 1102 points
'    LINE -(Path(i).x, Path(i).y) '   AND yields 368 points that need to be connected with lines
'NEXT i
PRINT UBOUND(Path) - 1
SLEEP


SUB CurvePoint (c() AS POINT, p AS POINT, t AS SINGLE)

    'Quick and dirty Bezier point calculator
    'Note: only works for 4 points

    DIM a AS SINGLE
    DIM b AS SINGLE
    DIM c AS SINGLE
    DIM d AS SINGLE

    a = (1 - t) * (1 - t) * (1 - t)
    b = 3 * (1 - t) * (1 - t) * t
    c = 3 * (1 - t) * t * t
    d = t * t * t

    p.x = a * c(0).x + b * c(1).x + c * c(2).x + d * c(3).x
    p.y = a * c(0).y + b * c(1).y + c * c(2).y + d * c(3).y

    'p.x = (1 - t) * (1 - t) * (1 - t) * c(0).x + 3 * (1 - t) * (1 - t) * t * c(1).x + 3 * (1 - t) * t * t * c(2).x + t * t * t * c(3).x
    'p.y = (1 - t) * (1 - t) * (1 - t) * c(0).y + 3 * (1 - t) * (1 - t) * t * c(1).y + 3 * (1 - t) * t * t * c(2).y + t * t * t * c(3).y

END SUB
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
Bezier Path Generator - by TerryRitchie - 10-06-2024, 03:06 AM
RE: Bezier Path Generator - by TerryRitchie - 10-06-2024, 05:08 PM
RE: Bezier Path Generator - by vince - 10-07-2024, 06:29 AM
RE: Bezier Path Generator - by TerryRitchie - 10-07-2024, 03:36 PM
RE: Bezier Path Generator - by Petr - 10-07-2024, 04:59 PM



Users browsing this thread: 3 Guest(s)