Circle and Curves
Explore how to draw arcs and complete circles with Pycairo's arc function, using radians for angle measurements. Learn to create versatile Bezier curves with control and anchor points, and combine curves and lines to form complex shapes.
We'll cover the following...
Arcs
An arc is a part of the circumference of a circle. The arc function is called like this: ctx.arc(cx,cy,r,a1,a2)
The following image shows how these parameters are used to draw an arc:
The arc is part of a circle, with radius r and centered at the point (cx, cy). It starts at angle a1 and ends at angle a2. Angles are measured from the x-axis and, by default, increase as we move clockwise. Here is an example of the code used to draw an arc:
Angles in Python, as in most computer languages, are measured in radians rather than degrees. The pi radians are exactly 180 degrees. That makes 1 radian equal to approximately 57.3 degrees.
The code given above draws an arc of a circle, with its center located at (300, 200): The middle of the image. The circle radius is 150, and the arc goes from angle 0 to angle pi/2 radians, which is a quarter of a turn. Run the code to see how the image looks.
Circles
There is no function for drawing a circle. Instead, we use the arc function again. An arc from 0 to radians (which is equivalent to 360 degrees) forms a complete circle.
Here is how to draw a circle, with center (cx, cy) and radius r:
ctx.arc(cx,cy,r,0,2*math.pi)
Bezier curves
A Bezier curve is a versatile curve, which is used to create many different shapes. It is based on four points. The anchors, A and D, are the endpoints of the curve. The control points, B and C, control the route the curve takes between the anchors. Here is an example of a Bezier curve:
We will cover the properties of this curve in an upcoming chapter. For now, we will just observe how it works.
Here is the code used to draw a single Bezier curve:
Let’s look at the code given above, in detail.
- In the code, the
move_tofunction sets the current point. Thecurve_to(xB, yB, xC, yC, xD, yD)function accepts six parameters. It treats the current point as the first anchor A. The six parameters represent the x, y values of the points B, C, and D. - After calling the
curve_tofunction, the current point is set to the location of D, which is the end of the curve.
We can also join curves and lines together to form poly curves. Here, we join a curve, a line, another curve, and then close the path (creating another line) to form a shape: