Created
March 27, 2020 02:55
-
-
Save arce/4046efeafed3717563203e10a761010e to your computer and use it in GitHub Desktop.
bezier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static float getApproximationFactor(float angleStart, float angleEnd) { | |
int arc = angleEnd - angleStart; | |
if (abs(arc) > M_PI) { | |
arc -= M_PI * 2; | |
arc /= M_PI * 2; | |
} | |
return (4 / 3) * tan(arc / 4); | |
} | |
static void getRelativeControlPoints(float points[], float angleStart, float angleEnd, float radius) { | |
float factor = getApproximationFactor(angleStart, angleEnd); | |
float distToCtrPoint = sqrt(radius * radius * (1 + factor * factor)); | |
float angle1 = angleStart + atan(factor); | |
float angle2 = angleEnd - atan(factor); | |
points[0] = cos(angle1) * distToCtrPoint; | |
points[1] = sin(angle1) * distToCtrPoint; | |
points[2] = cos(angle2) * distToCtrPoint; | |
points[3] = sin(angle2) * distToCtrPoint; | |
} | |
static void getPointAtAngle(float points[], float angle, float x, float y, float radius) { | |
points[0] = x + radius * cos(angle); | |
points[1] = y + radius * sin(angle); | |
} | |
static int P5_Arc(lua_State *L) { | |
float x = luaL_checknumber(L, 1); | |
float y = luaL_checknumber(L, 2); | |
float radius = luaL_checknumber(L, 3); | |
float angleStartRadians = luaL_checknumber(L, 4); | |
float angleEndRadians = luaL_checknumber(L, 5); | |
float relControlPoints[4]; | |
getRelativeControlPoints(relControlPoints, angleStartRadians, angleEndRadians, radius); | |
float pointStart[2]; | |
getPointAtAngle(pointStart, angleStartRadians, x, y, radius); | |
float pointEnd[2]; | |
getPointAtAngle(pointEnd, angleEndRadians, x, y, radius); | |
float controlPoint1[] = {x+relControlPoints[0], y+relControlPoints[1]}; | |
float controlPoint2[] = {x+relControlPoints[2], y+relControlPoints[3]}; | |
const VGfloat coords[8] = { | |
pointStart[0], pointStart[1], | |
controlPoint1[0],controlPoint1[1], | |
controlPoint2[0],controlPoint2[1], | |
pointEnd[0],pointEnd[1] | |
}; | |
vgModifyPathCoords(arc_path, 0, 2, coords); | |
if (strokeEnable) | |
vgDrawPath(arc_path, VG_STROKE_PATH); | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment