2

Trying to get my my head around this program we need to create What is needed is as per the notes: create a function named arbitraryMirror() that allows the user to place a mirror at an arbitrary angle, causing an intersect and therefore mirror the image.

This will need to be done on either a square or rectangle picture.

As per the pics below, this is the Output of what is required.

Output

I know how to mirror a pic (as shown below) with a square image, but i cannot work out if this can also be done with a rectangle image?

Cross

I had a look at a method of using y=mx+b but it seems overcomplicated? Maybe there is some coordinate geometry i need? Or algebra? Any help would be greatly appreciated!

Sam Sarzen
  • 21
  • 2

1 Answers1

1

The key formulas are (python):

# (x0, y0) and (x1, y1) are two points on the mirroring line
# dx, dy, L is the vector and lenght
dx, dy = x1 - x0, y1 - y0
L = (dx**2 + dy**2) ** 0.5

# Tangent (tx, ty) and normal (nx, ny) basis unit vectors
tx, ty = dx / L, dy / L
nx, ny = -dy / L, dx / L

# For each pixel
for y in range(h):
    for x in range(w):
        # Map to tangent/normal space
        n = (x+0.5 - x0)*nx + (y+0.5 - y0)*ny
        t = (x+0.5 - x0)*tx + (y+0.5 - y0)*ty

        # If we're in the positive half-space
        if n >= 0:
            # Compute mirrored point in XY space
            # (negate the normal component)
            xx = int(x0 + t*tx - n*nx + 0.5)
            yy = int(y0 + t*ty - n*ny + 0.5)

            # If valid copy to destination
            if 0 <= xx < w and 0 <= yy < h:
                img[y][x] = img[yy][xx]

Here you can see an example of the results example of mirroring

The top-left red corner are pixels that would be mirroring pixels outside of the original image and they're left untouched by the above code.

6502
  • 112,025
  • 15
  • 165
  • 265
  • Interesting, haven't come across these. May i ask what the 0.5 is for? – Sam Sarzen Oct 17 '16 at 09:18
  • @SamSarzentich: The top-left pixel on the screen is a box from (0, 0) to (1, 1) and the coordinates of its center are (0.5, 0.5). For the computation in these formulas I'm using just one sample per pixel (no antialias) with the sample being taken at the pixel center. – 6502 Oct 17 '16 at 09:41
  • Ok, the only other question i have, if i was going to take a user input would i be using the start/end coordinates from the x0,x1, y0,y1 ? – Sam Sarzen Oct 17 '16 at 10:13
  • @SamSarzentich: exactly... (x0, y0) and (x1, y1) are two points on the mirror line. The order decides which part of the image is preserved and which one is overwritten by the mirroring. – 6502 Oct 17 '16 at 12:16
  • Excellent! Thank you very much for your help! – Sam Sarzen Oct 17 '16 at 12:28
  • One last thing i forgot to ask. the lines – Sam Sarzen Oct 18 '16 at 05:21
  • On the code if 0 <= xx < w and 0 <= yy < h: img[y][x] = img[yy][xx] – Sam Sarzen Oct 18 '16 at 05:21
  • the line which mentions img[y][x] = img[yy][xx] i assume this the mirroring pseudocode or code? – Sam Sarzen Oct 18 '16 at 05:24
  • @SamSarzentich: the mirroring is done by copying one pixel at a time from one location to another, `(x, y)` is the destination and `(xx, yy)` is the source computed using the mirroring formula. Since for mirroring no pixel can be at the same time both a source and a destination there's no need to create a new image and the operation can be done in place. The check is to ensure that the computed source location is also inside the image as mirroring can reflect parts that would be outside (see example result)... – 6502 Oct 18 '16 at 06:17