0

I'm trying to convert Bezier.js implementation of calculating normals to a Shadertoy program, and the code appears to not use any of the calculated values. It needs to be for quadratic rationals as well.

I found the Javascript code a slight bit hard to follow, so I simplified it for my Shadertoy program:

vec2[3] derive(vec2[3] p)
{
    vec2[3] dpoints;
    int l_length = 0, j;
    for (int i = 2; i > 0; --i) {
      vec2[3] l;
      for (j = 0; j < i; j++) {
        vec2 dpt = vec2(
          float(i) * (p[j + 1].x - p[j].x),
          float(i) * (p[j + 1].y - p[j].y));
        dpoints[l_length] = dpt;
        l[l_length] = dpt; ++l_length;
      }
      p = l;
    }
    return dpoints;
}

The Bezier.js program continues to add functionality for 3d beziers, in case that has anything to do with rational beziers.

I need to make sense of the rest of the program, since I don't know the theory for calculating the normals.

Illari
  • 43
  • 5
  • 1
    Then maybe it's time to read [The Primer on Bezier Curves](https://pomax.github.io/bezierinfo). In 2D the normal is just the derivative, rotated 90 degrees and normalized (but in 3D, things are more complicated and if you want good looking normals [just calculus won't get you there](https://math.stackexchange.com/questions/2843307/getting-consistent-normals-along-a-3d-bezier-curve)). And getting the derivative function of a Bezier curve is _really_ easy as we can see at the end of the [section on derivatives](https://pomax.github.io/bezierinfo/#derivatives) in the primer. – Mike 'Pomax' Kamermans May 15 '22 at 15:44
  • 1
    What type do you mean by `vec2[3]`? To me it sounds like an array of three 2-dimensional vectors but that does not make much sense with the rest of the code. – Dominik Mokriš May 15 '22 at 16:09
  • if you throw away your slow iterative way of computing Bezier point an use [polynomials directly](https://stackoverflow.com/a/22582447/2521214) you would have no problem of derivate it which will give you tangent which in 3D you cross product with reference align direction (like camera up) to obtain normal ... in 2D is enough to cross product the tangent (do not be fouled by bad math books which say cross product is only in 3D as its well defined in 2D,3D,4D... ND) so in 2D just swap `(x,y)` into `(y,-x)` of the tangent ... – Spektre May 18 '22 at 07:28

1 Answers1

0

To spell Pomax's answer out loud:

Only the last calculated value is used, to make a "curve" (line) from origin.

The weights are calculated as w'0 = 2(w1-w0), w'1 = 2(w2-w1).

The resulting bezier at t gives the tangent of the original bezier at t.

I hope I got this right I haven't tried this yet.

Illari
  • 43
  • 5