Search⌘ K

Correcting Unequal Scaling

Explore how to correct unequal scaling effects in Pycairo by using translation and scale transforms centered on specific points. Learn to draw ellipses precisely and apply save and restore functions to prevent stroke and fill distortions caused by scaling, improving your vector graphics drawing skills.

Before we learn how to correct unequal scaling, we will make an ellipse.

Placing an ellipse

If we want to scale a shape using a specific center of enlargement, we will use the same trick we used to set the center of rotation. We will use this to place an ellipse on the page. We will draw an ellipse with a width of 400 and a height of 200, centered on the point (300, 350). This is the code required to draw this ellipse:

Python 3.8
import cairo
import math
# Set up pycairo
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 800, 800)
ctx = cairo.Context(surface)
ctx.set_source_rgb(0.8, 0.8, 0.8)
ctx.paint()
# Ellipse
ctx.translate(300, 350)
ctx.scale(2, 1)
ctx.translate(-300, -350)
ctx.arc(300, 350, 100, 0, math.pi*2)
ctx.set_source_rgb(0.8, 0, 0)
ctx.fill_preserve()
ctx.set_source_rgb(0, 0, 0)
ctx.set_line_width(10)
ctx.stroke()

The first part of this code uses translate ...