ios - Perfect alignment of triangles with Quartz2D drawing -
i want draw 2 general triangles in 2d space in such way share 1 edge, using quartz2d. i'm using code drawing:
- (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); triangle *t1 = [triangles objectatindex:0]; triangle *t2 = [triangles objectatindex:1]; [self filltriangle:t1 incontext:context]; [self filltriangle:t2 incontext:context]; } - (void) filltriangle:(triangle *)t incontext:(cgcontextref)ctx { cgcontextmovetopoint(ctx, t.p1.p.x, t.p1.p.y); cgcontextaddlinetopoint(ctx, t.p2.p.x, t.p2.p.y); cgcontextaddlinetopoint(ctx, t.p3.p.x, t.p3.p.y); cgcontextaddlinetopoint(ctx, t.p1.p.x, t.p1.p.y); cgcontextsetfillcolorwithcolor(ctx, t.color.cgcolor); cgcontextfillpath(ctx); }
where triangle
nsobject
subclass holding 3 points , color.
the problem when share 2 points, there's "space" between them when fill them code, shown in image:
question: knows how rid of space there's seamless transition 1 triangle no line or whatever between them? i'd maintain using quartz2d i'm glad help. thanks!
edit: there answers showing problem caused sub-pixel anti-aliasing, , adding line
cgcontextsetallowsantialiasing(ctx, no);
to filltriangle
method solved that. it's not result i'd see - don't beingness aliased, shown in sec picture.
question updated: know, how solve both of problems, is, keeping image clean no ugly aliasing no space between triangles?
edit2: so, pointed in answer, add together line stroke code, remove antialiasing flag , stroke fill space. here's lastly image show happened:
notice lines overlapping outside of box. wondering cause might have been, , disappeared when set width 0.5f (since retina, translates 1px , that's responder thought), , solved whole thing. help!
the problem drawing using antialiasing. disable context via
cgcontextsetshouldantialias(context, no);
and should fine.
edit: in case of antialiasing needed (your updated multicolor example), seek not fill triangles stroke border in same color line width of 1px:
cgcontextsetlinewidth(ctx, 0.5f); cgcontextdrawpath(ctx, kcgpathfillstroke);
of course of study should remove cgcontextsetshouldantialias
line in case. it's simple solution may not best performance wise, since path has stroked , filled.
ios drawing quartz-2d
No comments:
Post a Comment