Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rshapes] Fix multisegment Bezier splines. #3744

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

spelufo
Copy link

@spelufo spelufo commented Jan 20, 2024

It seems to me that these functions are wrong, if you step the index by 1 you move to a control point instead of the next segment.

It seems to me that these functions are wrong, if you step the index by 1 you move to a control point instead of the next segment.
@raysan5
Copy link
Owner

raysan5 commented Jan 29, 2024

@spelufo Did you test it? Could you share some code example?

@spelufo
Copy link
Author

spelufo commented Jan 30, 2024

I did, through raylib-go:

package main

import rl "github.com/gen2brain/raylib-go/raylib"

func main() {
  rl.InitWindow(800, 600, "raylib [rshapes] example - bezier")
  defer rl.CloseWindow()

  rl.SetTargetFPS(60)

  for !rl.WindowShouldClose() {
    rl.BeginDrawing()
    rl.ClearBackground(rl.RayWhite)
    points := []rl.Vector2{
      {0.0, 0.0}, {50.0, 0.0}, {50.0, 50.0},
      {100.0, 50.0}, {100.0, 100.0}, {250.0, 250.0},
      {500.0, 500.0},
    }

    // Does the wrong thing.
    rl.DrawSplineBezierCubic(points, 5.0, rl.Green)

    // Proposed implementation:
    for i := 0; i < len(points) - 3; i += 3 {
      color := rl.Orange
      p := points[i]
      rl.DrawSplineSegmentBezierCubic(p, points[i + 1], points[i + 2], points[i + 3], 5.0, color)
    }

    rl.EndDrawing()
  }
}

Screenshot from 2024-01-30 15-05-41

@raysan5
Copy link
Owner

raysan5 commented Feb 3, 2024

@spelufo I'm trying this fix with example shapes_splines_drawing, replacing current DrawSplineSegmentBezierCubic() in a loop by the proposed solution but it doesn't seem to work as expected. Please could you review it?

Drawing code I'm using:

Vector2 fullPoints[14] = {
    points[0],
    control[0].end,
    control[1].start,
    points[1],
    control[1].end,
    control[2].start,
    points[2],
    control[2].end,
    control[3].start,
    points[3],
    control[3].end,
    control[4].start,
    points[4],
    control[4].end
};

DrawSplineBezierCubic(fullPoints, 14, splineThickness, RED);

image

The expected result is:

image

@spelufo
Copy link
Author

spelufo commented Feb 4, 2024

I've fixed the example code. I tested that the previous version of DrawSplineBezierCubic didn't work right. The new version works with this example code. The sequence of points passed to it must be points[0], control[0].start, control[0].end, points[1], ..., points[pointCount-1], and the length of control must always be pointCount - 1.

@spelufo
Copy link
Author

spelufo commented Feb 4, 2024

Regarding the "connections", the gaps left on the points between segments, one way to cover them up would be to have DrawSplineBezierCubic draw a circle at the points connecting segments of diameter equal to the thickness of the lines. Maybe not the most efficient way to do it but it would cover the kinks.

@spelufo
Copy link
Author

spelufo commented Feb 21, 2024

I've gone ahead and did that. It is ready to merge if you want.

@spelufo
Copy link
Author

spelufo commented May 17, 2024

@raysan5 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants