From: Jean-Philippe Bruyère Date: Fri, 20 Apr 2018 03:41:09 +0000 (+0200) Subject: line joins in all direction, colinear case not cleared X-Git-Tag: v0.1-alpha~141 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=fc94ba0676dc623c5047970a0a22196a814dc839;p=jp%2Fvkvg.git line joins in all direction, colinear case not cleared --- diff --git a/src/vkvg_context_internal.c b/src/vkvg_context_internal.c index 714764a..66d8905 100644 --- a/src/vkvg_context_internal.c +++ b/src/vkvg_context_internal.c @@ -324,14 +324,16 @@ void add_line(vkvg_context* ctx, vec2 p1, vec2 p2, vec4 col){ } void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_t i, uint32_t iR){ - double alpha = 0; vec2 v0n = vec2_line_norm(ctx->points[iL], ctx->points[i]); vec2 v1n = vec2_line_norm(ctx->points[i], ctx->points[iR]); - vec2 bisec = vec2_add(v0n,v1n); + vec2 bisec = vec2_norm(vec2_add(v0n,v1n)); - bisec = vec2_norm(bisec); - alpha = acos(v0n.x*v1n.x+v0n.y*v1n.y)/2.0; + float alpha = acos(v0n.x * v1n.x + v0n.y * v1n.y)/2; + float cross = v0n.x * v1n.y - v0n.y * v1n.x; + + if (cross<0) + alpha = -alpha; float lh = hw / cos(alpha); bisec = vec2_perp(bisec); @@ -347,9 +349,15 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ _add_tri_indices_for_rect(ctx, idx); }else{ vec2 vp = vec2_perp(v0n); - v.pos = vec2_add (ctx->points[i], vec2_mult (vp, hw)); - _add_vertex(ctx, v); - v.pos = vec2_sub (ctx->points[i], bisec); + if (cross<0){ + v.pos = vec2_sub (ctx->points[i], vec2_mult (vp, hw)); + _add_vertex(ctx, v); + v.pos = vec2_add (ctx->points[i], bisec); + }else{ + v.pos = vec2_add (ctx->points[i], vec2_mult (vp, hw)); + _add_vertex(ctx, v); + v.pos = vec2_sub (ctx->points[i], bisec); + } _add_vertex(ctx, v); if (ctx->lineJoint == VKVG_LINE_JOIN_BEVEL){ @@ -358,13 +366,25 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ _add_triangle_indices(ctx, idx+1, idx+3, idx+4); }else if (ctx->lineJoint == VKVG_LINE_JOIN_ROUND){ float step = M_PI / hw; - float a = acos(-vp.x) + M_PI; - float a1 = a + alpha*2;//acos(v0n.x) + M_PI; - - a+=step; - while (a < a1){ - _add_vertexf(ctx, cos(a) * hw + ctx->points[i].x, sin(a) * hw + ctx->points[i].y); + float a = acos(vp.x); + if (vp.y < 0) + a = -a; + + if (cross<0){ + a+=M_PI; + float a1 = a + alpha*2; + a-=step; + while (a > a1){ + _add_vertexf(ctx, cos(a) * hw + ctx->points[i].x, sin(a) * hw + ctx->points[i].y); + a-=step; + } + }else{ + float a1 = a + alpha*2; a+=step; + while (a < a1){ + _add_vertexf(ctx, cos(a) * hw + ctx->points[i].x, sin(a) * hw + ctx->points[i].y); + a+=step; + } } uint32_t p0Idx = ctx->vertCount; _add_triangle_indices(ctx, idx, idx+2, idx+1); @@ -374,8 +394,12 @@ void _build_vb_step (vkvg_context* ctx, Vertex v, float hw, uint32_t iL, uint32_ _add_triangle_indices(ctx, p0Idx, p0Idx+1, idx+1); _add_triangle_indices(ctx, idx+1, p0Idx+1, p0Idx+2); } + vp = vec2_mult (vec2_perp(v1n), hw); - v.pos = vec2_add (ctx->points[i], vp); + if (cross<0) + v.pos = vec2_sub (ctx->points[i], vp); + else + v.pos = vec2_add (ctx->points[i], vp); _add_vertex(ctx, v); } diff --git a/tests/test1.c b/tests/test1.c index 5966a5d..9ee7767 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -658,7 +658,6 @@ void test_img_surface (VkvgContext ctx) { void test_line_caps (VkvgContext ctx) { float x = 20, y = 20, dx = 30, dy = 60; - vkvg_scale(ctx,2,2); vkvg_set_line_width(ctx,26); vkvg_set_rgba(ctx,0,0,0,1); @@ -702,27 +701,53 @@ void test_line_caps (VkvgContext ctx) { } void test_line_join (VkvgContext ctx){ - float x = 50, y = 200, dx = 100, dy = 30; + float x = 50, y = 150, dx = 150, dy = 140; - //vkvg_scale(ctx,4,4); + vkvg_scale(ctx,2,2); - vkvg_set_line_width(ctx,30); + vkvg_set_line_width(ctx,40); vkvg_set_rgba(ctx,0,0,0,1); + + + vkvg_set_line_join(ctx,VKVG_LINE_JOIN_ROUND); + //vkvg_rectangle(ctx,x,y,dx,dy); + vkvg_move_to(ctx,x,y); - vkvg_rel_line_to(ctx,50,-40); - vkvg_rel_line_to(ctx,50,40); + vkvg_rel_line_to(ctx,50,-30); + vkvg_rel_line_to(ctx,50,0); + vkvg_rel_line_to(ctx,50,30); + vkvg_rel_line_to(ctx,0,60); + vkvg_rel_line_to(ctx,-50,70); + vkvg_rel_line_to(ctx,-50,0); + vkvg_rel_line_to(ctx,-50,-70); + vkvg_close_path(ctx); + vkvg_stroke(ctx); + + vkvg_move_to(ctx,x+200,y); + vkvg_rel_line_to(ctx,50,70); + vkvg_rel_line_to(ctx,50,0); + vkvg_rel_line_to(ctx,50,-70); + vkvg_rel_line_to(ctx,0,-60); + vkvg_rel_line_to(ctx,-50,-30); + vkvg_rel_line_to(ctx,-50,0); + vkvg_rel_line_to(ctx,-50,30); + vkvg_close_path(ctx); + vkvg_stroke(ctx); + + /*vkvg_rel_line_to(ctx,dx,-dy); + vkvg_rel_line_to(ctx,dx,dy); vkvg_stroke(ctx); vkvg_set_line_join(ctx,VKVG_LINE_JOIN_BEVEL); - vkvg_rel_move_to(ctx,-100,60); - vkvg_rel_line_to(ctx,50,-40); - vkvg_rel_line_to(ctx,50,40); + vkvg_rel_move_to(ctx,-dx*2,abs(dy*1.5)); + vkvg_rel_line_to(ctx,dx,-dy); + vkvg_rel_line_to(ctx,dx,dy); vkvg_stroke(ctx); vkvg_set_line_join(ctx,VKVG_LINE_JOIN_ROUND); - vkvg_rel_move_to(ctx,-100,60); - vkvg_rel_line_to(ctx,50,-50); - vkvg_rel_line_to(ctx,50,50); + vkvg_rel_move_to(ctx,-dx*2,abs(dy*1.5)); + vkvg_rel_line_to(ctx,dx,-dy); + vkvg_rel_line_to(ctx,dx,dy); vkvg_stroke(ctx); - vkvg_set_line_join(ctx,VKVG_LINE_JOIN_MITER); + vkvg_set_line_join(ctx,VKVG_LINE_JOIN_MITER);*/ } int main(int argc, char *argv[]) {