Reputation: 5006
I'm trying to fill some triangles I have drawn, however they aren't filling. Below is the code which draws in the context, and as you can see in the picture the path strokes fine.
func draw(context:CGContextRef) {
CGContextBeginPath(context)
CGContextMoveToPoint(context, self.peak.x, self.peak.y)
CGContextAddLineToPoint(context, self.left.x,self.left.y)
CGContextMoveToPoint(context, self.left.x, self.left.y)
CGContextAddLineToPoint(context, self.right.x,self.right.y)
CGContextMoveToPoint(context, self.right.x, self.right.y)
CGContextAddLineToPoint(context, self.peak.x,self.peak.y)
CGContextSetStrokeColorWithColor(context,UIColor.blackColor().CGColor)
CGContextSetFillColorWithColor(context,UIColor.redColor().CGColor)
CGContextDrawPath(context, kCGPathFillStroke)
}
I call that function twice and the output is this.
I'm reading the Quartz2D programming guide but can't see why this wouldn't fill. This is just happening in a playground in a class that inherits from UIView, nothing fancy.
Upvotes: 1
Views: 655
Reputation: 540105
Each call to CGContextMoveToPoint()
starts a new subpath,
which is then filled as if you had closed it (and there is nothing
to see if you fill a single line segment). To stroke and fill
a triangle, use:
CGContextBeginPath(context)
CGContextMoveToPoint(context, self.peak.x, self.peak.y)
CGContextAddLineToPoint(context, self.left.x,self.left.y)
CGContextAddLineToPoint(context, self.right.x,self.right.y)
CGContextClosePath(context)
CGContextSetStrokeColorWithColor(context,UIColor.blackColor().CGColor)
CGContextSetFillColorWithColor(context,UIColor.redColor().CGColor)
CGContextDrawPath(context, kCGPathFillStroke)
Upvotes: 3