Sam Sarzen
Sam Sarzen

Reputation: 21

Setting diagonal mirroring via Jython (user to set points)

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!

Upvotes: 2

Views: 239

Answers (1)

6502
6502

Reputation: 114539

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.

Upvotes: 1

Related Questions