Let's say, for example, you were trying to photo-manipulate this image, and you wanted to change the picture on the wall to the left of the picture. This picture came out of a book, so I have no idea about the exact position of the camera or the wall. How is it possible to map a picture, with correct perspective onto the wall?

**V _{1}**, can be found by intersecting the lines

Now that you have all these new points, you can use them to construct four new small rectangles.

Rectangle 1 is (A, i1, O, i4) Rectangle 2 is (i1, B, i2, O) Rectangle 3 is (O, i2, C, i3) Rectangle 4 is (i4, O, i3, D)

Keep subdividingthe rectangles until the size of a rectangle is less than a pixel on the photo.
Alternatively, you can subdivide until the size of the rectangles is quite small, then linearly
texturemap.

structure: POINT FLOAT x, y end structure structure: LINE POINT: p1, p2 end structure global variables: POINT: v1, v2 end global variables procedure main Variables: LINE edge1, edge2 POINT a, b, c, d, ta, tb, tc, td ask user to specify POINTs a,b,c,d edge1 = d-a // calculate a pair of opposite edges edge2 = c-b v1 = intersection(edge1, edge2) edge1 = b-a // calculate the other pair of opposite edges edge2 = c-d v2 = intersection(edge1, edge2) ta = top left of texture tb = bottom left of texture tc = bottom right of texture td = top right of texture recurse(a, b, c, d, ta, tb, tc, td) end procedure main procedure recurse(POINT a, POINT b, POINT c, POINT d, POINT ta, POINT tb, POINT tc, POINT td) // POINTs a,b,c,d are the vertices that make up this rectangle on the photo. // POINTs ta, tb, tc, td are the equivalent coordinates on the texture map. Variables: LINE line1, line2 // lines used for calculations POINT o, i1, i2, i3, i4, // midpoints of the rectangle on the photo POINT to, ti1, ti2, ti3, ti4 // midpoints of the rectangle on the texture if area(a, b, c, d) > 1 then // if the rectangle is still bigger than a pixel then line1 = c-a // calculate the two lines between opposite corners line2 = d-b o = intersection(line1, line2) // calculate the central point of the rectangle line1 = v1-o // line from center to vanishing point 1 line2 = b-a i1 = intersection(line1, line2) // calculate the point i1 line1 = v2-o // line from center to vanishing point 2 line2 = c-b i2 = intersection(line1, line2) // calculate the point i2 line1 = v1-o // line from center to vanishing point 1 line2 = d-c i3 = intersection(line1, line2) // calculate the point i3 line1 = v2-o // line from center to vanishing point 2 line2 = d-a i4 = intersection(line1, line2) // calculate the point i4 to = mid(ta,tb,tc,td) ti1 = mid(ta,tb) ti2 = mid(tb,tc) ti3 = mid(tc,td) ti4 = mid(td,ta) recurse(a, i1, o, i4, ta, ti1, to, ti4) // subdivide rectangle 1 recurse(i1, b, i2, o, ti1, tb, ti2, to) // subdivide rectangle 2 recurse(o, i2, c, i3, to, ti2, tc, ti3) // subdivide rectangle 3 recurse(i4, o, i3, d, ti4, to, ti3, td) // subdivide rectangle 4 else // otherwise this rectangle is smaller than a pixel, so a // pixel should be drawn line1 = c-a // calculate the two lines between opposite corners line2 = d-b o = intersection(line1, line2) // find the middle of the rectangle on the photo to = mid(ta,tb,tc,td) // find the middle of the rectangle on the texture copy pixel from texturemap at pointtoto photo at pointoendif end procedure recurse