Computer vision: Move points with an affine matrix

Before warping an image, check what a transform does to a few points.

What to build

A program that applies a 2 × 3 affine matrix to points.

Requirements

  • Read the matrix rows a b tx and c d ty, then the points.
  • Print each transformed point as x y with two decimal places; print 0.00, never -0.00.

Input

Line 1: a, b, tx. Line 2: c, d, ty. Line 3: N. Then N lines x y.

Output

N lines.

Constraints

N ≤ 100.

Example

Input:

1 0 5
0 1 -2
2
0 0
3 4

Output:

5.00 -2.00
8.00 2.00

Why it works

Each output coordinate is a weighted sum of x and y plus a shift, which is exactly one row of the matrix multiplied by (x, y, 1).

Examples

Example 1 · Input
1 0 5
0 1 -2
2
0 0
3 4
Expected output
5.00 -2.00
8.00 2.00
Example 2 · Input
2 0 0
0 0.5 0
2
1 1
-3 4
Expected output
2.00 0.50
-6.00 2.00

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

x' = a x + b y + tx and y' = c x + d y + ty.

Hint 2

Add 0.0 to each result before formatting so that a negative zero prints as 0.00.

Solution

Show one correct solution (try it yourself first)
a, b, tx = map(float, input().split())
c, d, ty = map(float, input().split())
for _ in range(int(input())):
    x, y = map(float, input().split())
    nx = a * x + b * y + tx + 0.0
    ny = c * x + d * y + ty + 0.0
    print(f"{nx:.2f} {ny:.2f}")

Lesson: Affine transforms: shift, scale, rotate and warp →