views:

82

answers:

2

I know how to move, rotate, and scale, but how does skewing work? what would I have to do to a set of verticies to skew them?

Thanks

+2  A: 

Offset X values by an amount that varies linearly with the Y value (or vice versa).

Edit: Doing this with a rectangle:

Let's say you start with a rectangle (0, 0), (4, 0), (4, 4), (0, 4). Let's assume you want to skew it with a slope of 2, so as it goes two units up, it'll move one to the right, something like this (hand drawn, so the angle's undoubtedly a bit wrong, but I hope it gives the general idea):

alt text

To get this, each X value is adjusted like:

X = X + Y * S

where S is the inverse of the slope of the skew. In this case, the slope is 2, so S = 1/2. Working that for our four corners, we get:

(0, 0) => 0 + 0 / 2 = 0 => (0, 0)
(4, 0) => 4 + 0 / 2 = 4 => (4, 0)
(4, 4) => 4 + 4 / 2 = 6 => (6, 4)
(0, 4) => 0 + 4 / 2 = 2 => (2, 4)
Jerry Coffin
Could you show an example with a rectangle? Thanks
Milo
`tan(theta/2) != tan(theta)/2`, but good demo otherwise.
Ben Voigt
@Ben: Oops, quite right. I think I've fixed it (the easy way, by only discussing slope -- angles in degrees are pretty useless anyway, but I suspect a lot of people would be scratching their heads if I said anything about "Pi/8 radians" or anything like that -- though if they care much about graphics, they really *do* need to learn radians).
Jerry Coffin
+1  A: 

Skewing / shearing is described in detail at http://en.wikipedia.org/wiki/Shear_mapping and http://mathworld.wolfram.com/ShearMatrix.html

andand