Lecture5_Transformations_Pycairo_Detailed.pptx

kamalsmail1 0 views 11 slides Oct 08, 2025
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Lecture 5


Slide Content

Computer Graphics with Pycairo Lecture 5: Transformations and Coordinate Systems

Objectives - Understand coordinate system in Pycairo - Apply translation, rotation, and scaling - Use ctx.save() and ctx.restore() - Combine multiple transformations - Create complex drawings using transformations

Coordinate System in Pycairo - Origin (0,0) is at top-left corner - X-axis: rightwards, Y-axis: downwards - Units in pixels - Example: drawing a rectangle at (50,50)

Translation # Move shapes using translate ctx.translate(100, 50) # move origin by (100,50) ctx.rectangle(0, 0, 100, 50) ctx.set_source_rgb(0, 0, 1) # blue ctx.fill()

Rotation # Rotate shapes using rotate import math ctx.translate(200, 200) # move origin to rotation center ctx.rotate(math.radians(45)) # rotate 45 degrees ctx.rectangle(-50, -25, 100, 50) ctx.set_source_rgb(1, 0, 0) # red ctx.fill()

Scaling # Scale shapes using scale ctx.save() ctx.translate(50, 300) ctx.scale(2, 0.5) # scale x by 2, y by 0.5 ctx.rectangle(0, 0, 50, 50) ctx.set_source_rgb(0, 1, 0) # green ctx.fill() ctx.restore()

Saving and Restoring Context - ctx.save() stores current transformation and state - ctx.restore() restores saved state - Useful to apply temporary transformations without affecting other drawings

Combining Transformations # Combine translate, rotate, scale ctx.save() ctx.translate(300, 300) ctx.rotate(math.radians(30)) ctx.scale(1.5, 1) ctx.rectangle(-50, -25, 100, 50) ctx.set_source_rgb(0.5, 0, 0.5) # purple ctx.fill() ctx.restore()

Practical Example Program import cairo import math WIDTH, HEIGHT = 400, 400 surface = cairo.ImageSurface (cairo.FORMAT_ARGB32, WIDTH, HEIGHT) ctx = cairo.Context (surface) # Blue rectangle translated ctx.translate (100, 50) ctx.rectangle (0,0,100,50); ctx.set_source_rgb (0,0,1); ctx.fill () ctx.identity_matrix () # reset transformations # Red rotated rectangle ctx.translate (200,200); ctx.rotate ( math.radians (45)) ctx.rectangle (-50,-25,100,50); ctx.set_source_rgb (1,0,0); ctx.fill () ctx.identity_matrix () # Green scaled rectangle ctx.save (); ctx.translate (50,300); ctx.scale (2,0.5) ctx.rectangle (0,0,50,50); ctx.set_source_rgb (0,1,0); ctx.fill (); ctx.restore () # Purple combined transform rectangle ctx.save (); ctx.translate (300,300); ctx.rotate ( math.radians (30)); ctx.scale (1.5,1) ctx.rectangle (-50,-25,100,50); ctx.set_source_rgb (0.5,0,0.5); ctx.fill (); ctx.restore () surface.write_to_png ('transformations.png')

Example Output - Blue rectangle moved with translation - Red rectangle rotated 45 degrees - Green rectangle scaled - Purple rectangle combined translation, rotation, scaling - Saved as 'transformations.png'

Summary - Pycairo uses top-left origin coordinate system - Translation, rotation, scaling allow flexible drawing - Use ctx.save() and ctx.restore() to manage transformations - Combine transformations for complex drawings - Code ready for Anaconda environment
Tags