Reputation: 5364
I need algorithm to get shift trace of given polygon by given 2d-vector.
Given valid polygon with no holes, but possibly concave. Operation performs on plane, so the result might be a polygon, possibly with holes. If it simplifies the task, outer polygon is enough.
It looks simple to describe, but I find it complex to realize, so I look for some ready solutions, preferably in c#.
Upvotes: 3
Views: 174
Reputation: 3331
Suppose you have a polygon P given by the points A1, A2, ... , An.
Now you decide to shift it by X on the x-axis and Y on the y-axis.
You can do this to each point individually to get the ending location of the polygon.
Let's call the shifted polygon Q givng by points B1, B2, ... , Bn.
Then all you need to do is draw the following parallelograms:
(A1 A2 B2 B1), (A2 A3 B3 B2), (A3 A4 B4 B3), ... , (An-1 An Bn Bn-1) , (An A1 B1 Bn)
At this point you will have filled in the shape you want.
Some of the parallelograms will overlap, but that is okay, since you are just filling them all in with the same red color.
By doing it this way, you also get your second example to turn out correctly, (the bottom right corner of the hole in the middle should be diagonal, because of the lip sliding into position).
Upvotes: 1