c++. Convert drawing bezier curve calls with 2 points defined to calls with 3 points defined?

StackOverflow https://stackoverflow.com/questions/18464945

  •  26-06-2022
  •  | 
  •  

Question

link to theory

C and c mean curves with 3 points defined.

S and s mean curves with 2 points defined.

The relevant functions are (monkSVG + monkVG/openVG):

    void OpenVG_SVGHandler::onPathCubic( float x1, float y1, float x2, float y2, float x3, float y3 ) { 
        VGubyte seg = VG_CUBIC_TO | openVGRelative();
        VGfloat data[6];

        data[0] = x1; data[1] = y1;
        data[2] = x2; data[3] = y2;
        data[4] = x3; data[5] = y3;
        vgAppendPathData( _current_group->current_path->path, 1, &seg, data);

    }

    void OpenVG_SVGHandler::onPathSCubic( float x2, float y2, float x3, float y3 ) {
        VGubyte seg = VG_SCUBIC_TO | openVGRelative();
        VGfloat data[4];

        data[0] = x2; data[1] = y2;
        data[2] = x3; data[3] = y3;
        vgAppendPathData( _current_group->current_path->path, 1, &seg, data);

    }

The problem is the second function (with 2 points) result image look likes incorrect. So I want to try to replace it with a function which look likes workable.

I tried to store a point from a previous step but it gives an incorrect result:

float x2 = d_string_to_float( c, &c );
float y2 = d_string_to_float( c, &c );
float x3 = d_string_to_float( c, &c );
float y3 = d_string_to_float( c, &c );
float x1 = 2 * prevX - x2;
float y1 = 2 * prevY - x2;
_handler->onPathCubic(x1, y1, x2, y2, x3, y3);
prevX = x3;
prevY = y3;
Was it helpful?

Solution 2

My solution is to write code similar to the same in another library:

link

OTHER TIPS

Let's name the variables slightly differently. You have two cubic Bezier curves, one with four points A, B, C, D, and the other with points E, F, G, H.

The SCubic function tries to ensure that D=E and (C+F)/2 = D.

The first time you call onPathCubic, x1 = B, x2 = C, x3 = D. A is previously set. (I only discuss x for conciseness, but you would need x and y).

The second time you call onPathCubic, you need to compute F, and G and H are specified. How can you compute F? Well, F = 2 * D - C.

Let's look at how our letters map to variables in the second call:

D = E = prevX
F = x1
G = x2
H = x3

This is a problem, because you're not storing C, so you don't have enough information to compute F.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top