# How does this bezier curve rendering shader determine if a curve is concave or convex?

The algorithm is explained like this:

• For each quadratic Bézier curve we use Bézier control points of the curve to create triangles. Triangles we get contain curves that are either convex or concave.

• Then we need to assign [u v] coordinates to the vertices of the triangles (article says that we assign values [0 0], [½ 0], and [1 1]; I'm not sure why these values are used).

• Then it say that instead of using [u v] coordinate to look up a color value as in texture mapping, we use the [u v] coordinate to evaluate a procedural texture. The pixel shader computes the expression u^2 - v using the sign of the result to determine pixel inclusion. For convex curves, a positive result means the pixel is outside the curve; otherwise it is inside. For concave curves, this test is reversed.

At the end of the article an example implementation of that algorithm is provided (reproduced below). It says it is a "quadratic curve pixel shader" so as I understand it, it should be able to render both convex and concave curves, but I am only able to get it to render convex curves.

Is it an incomplete example? If not, how / where it is specified if the curve is convex or concave?

float4 QuadraticPS(float2 p : TEXCOORD0, float4 color : COLOR0) : COLOR
{
float2 px = ddx(p);
float2 py = ddy(p);
// Chain rule
float fx = (2*p.x)*px.x - px.y;
float fy = (2*p.x)*py.x - py.y;
// Signed distance
float sd = (p.x*p.x - p.y)/sqrt(fx*fx + fy*fy);
// Linear alpha
float alpha = 0.5 - sd;
if (alpha > 1)       // Inside
color.a = 1;
else if (alpha < 0)  // Outside
clip(-1);
else
// Near boundary
color.a = alpha;
return color;
}


• ...because for concave curve formula should be changed from p.x*p.x - p.y to p.y - p.x*p.x. I am passing in [0, 0] [1/2, 0] [1, 1] as [u, v] texture coordinates for vertices of my test triangle and it seems to me mathematically impossible to pass in uv coordinates that would cause concave result. Unless I am wrong in some way. – zigzag Jan 17 '14 at 14:44