Re: Polygon coordinates transformation

new topic     » goto parent     » topic index » view thread      » older message » newer message

Tommy wrote:

> Derek wrote:
>> Tommy wrote:
>>> I want to add a routine to Win32Dib, that can draw a part of a bitmap on
>>> another bitmap, but stretched to a polygon. To do this, I need to find
>>> an
>>> algorithm that transforms coordinates from 1 polygon to another one.
>>>
>>> I have 2 polygons with each 4 points:
>>> polygon_source = {{u1, v1}, {u2, v2}, {u3, v3}, {u4, v4}}
>>> polygon_destination = {{x1, y1}, {x2, y2}, {x3, y3}, {x4, y4}}
>>>
>>> I need to find a formula that transforms a coordinate {x, y} inside
>>> polygon_destination to a coordinate {u, v} inside polygon_source.
>>>
>>> I've looked for tutorials on texture mapping, but didn't find any non-3D
>>> transformation formula. I've looked for methods to solve linear
>>> equations, but didn't find the right method.
>>>
>>> Can anyone solve the following set of equations?
>>>
>>> x1 * a + y1 * b + c = u1
>>> x2 * a + y2 * b + c = u2
>>> x3 * a + y3 * b + c = u3
>>> x4 * a + y4 * b + c = u4
>>>
>>> a, b and c are the unknown variables. u?, x? and y? are known values. If
>>> these equations can be solved, I can transform coordinates with the 2
>>> simple functions:
>>>
>>> u = x * a + y * b + c
>>> v = x * d + y * e + f   -- d, e and f can be found with the same set of
>>> equations, but with v? instead of u?
>>>
>>> This finds the coordinate {u, v} in polygon_source that represent the
>>> coordinate {x, y} in polygon_destination.
>>
>> I'm very tired so this is probably wrong...
>>
>> b = (x1u2 - x1u3 + x2u3 - x2u1 + x3u1 - x3u2) / (x1y2 - x1y3 + x3y3 -
>> x2y1 + x3y1 - x3y2)
>> a = (u1 - u2 - b(y1 -y2)) / (x1 - x2)
>> c = u1 - ax1 - by1
>
> Thanks for the effort, Derek. I've also tried it manually, and your
> solution is one of the many solutions I came up with. The problem is:
> there are 3 unknowns, but 4 equations.

I don't think that this is the case. I think in your equations:
   x1 * a + y1 * b + c = u1
   x2 * a + y2 * b + c = u2
   x3 * a + y3 * b + c = u3
   x4 * a + y4 * b + c = u4

the variables u? (belonging to the source polygon) are known, but the
variables x? and y? (belonging to the destination polygon) are *not*
known. If you knew the values of the variables x? and y?, you were
already done, and it wouldn't be necessary to do any calculation ... smile
And what about the variables a, b, and c? I'm not sure whether or not
you know them, because I don't know their meaning.

Maybe, because of my limited knowledge of the English language, I didn't
understand at all, what exactly the problem is.

Assumption:
You want to be able to change the *size* of any polygon with 4 corners,
without changing its shape, i.e. without altering any of its *angles*.
Is this true?

If so, then I think a different set of equations must be solved, in
order to get the appropriate formula. I don't want to make calculations
based on a guess, so please tell me, whether this assumption is correct.

> Your solution doesn't match the 4th
> equation (no x4, y4 or u4 are present). I tried to solve this by making 4
> unknowns to match the 4 equations like this: 'x * a + y * b + c + d = u'
> (extra unknown 'd'), but it didn't work.
>
> I just came up with something: couldn't I split the 4-cornered polygon
> into 2 triangles...
> Triangle 1: {{x1, y1}, {x2, y2}, {x3, y3}}
> Triangle 2: {{x1, y1}, {x3, y3}, {x4, y4}}
> ...and find a, b and c for each triangle with Derek's solution?

Regards,
   Juergen

PS: Tomorrow I'll leave the town for some days, so there might be a
    delay until I can write my next post.

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu