tags:

views:

120

answers:

5

I know how to do these mathematically but I don't actual know what they yield. I'v had formulas that required their use without actually knowing why they are required. How do they work in computer graphics? Thanks

+1  A: 

Scalar product is usually used to project vectors, cross product - to find normals.

Of course, there are tons of other different usage samples (of both dot and cross products) involving huge formulas and algorithms. Plain and simple math operations is always beneath something bigger.

Kotti
Would it be possible to get an example? Thanks
Milo
@user146780 See http://en.wikipedia.org/wiki/Dot_product and http://en.wikipedia.org/wiki/Cross_product *Basically the same ideas are often used in computer science and basic linear algebra problems.*
Kotti
+1  A: 

There are plenty of mathematical descriptions, but what you really need to know is "what are they good for?"...

A dot product of two vectors yields a scalar: the cosine of the angle between two vectors. Or, it can be used to get the projection (length) of vector A onto vector B. Both very handy.

A cross product of vectors A and B produces a vector that is perpendicular to both A and B. So if you have three distinct points (a,b,c) that lie in a plane you can use the vectors (a-b) and (a-c) to generate a perpendicular vector, which is the normal to the plane. (Normals are very useful when calculating things like lighting, because they allow you to use a dot product to calculate the angle between an incoming light ray and the normal of the surface - this angle can be used to calculate how much specular reflection bounces off the surface - which is where most of the "shinyness" comes from in computer graphics).

Another example of the use in graphics is if you know (e.g.) the direction a camera is looking (forwards, A) and you know the direction that is "up" (B) in the world, the cross product AxB yields the "sideways" direction C (This will be "left" or "right" depending on which way you do the cross product). i.e. when you only have the "look direction", you can determine which direction "right" is to allow you to move the camera sideways. (Note that in this case if A is pointing vertically upwards or downwards, you get a degernate case so the calculation fails)

Jason Williams
For a real world example, I often use the idea of a shadow when thinking about the dot-product. This is "projection" when the image of the object in one dimension (like a pole, the dimension is up and down) goes into another dimension (like the ground, left and right). The angle of the light determines how much shadow there is. If you think of a line extending to the light source, you can think of the shadow on the ground as how much "leftness or rightness" exists in that line compared to the straight up and down pole. This amount is the dot product of the line to the light source and the pole
codekaizen
The dot product is only directly the cosine if the vectors are of unit length. Also, if you ever deal with matrix multiplication -- and if you don't, you're not engaging with vector graphics in any meaningful way -- you should note that every element of the result matrix is a dot product of the constituent vectors. Other areas in which the dot product is highly useful are probability and multivariate statistics -- as in calculation of [covariance](http://en.wikipedia.org/wiki/Covariance#Relationship_to_inner_products), for example.
walkytalky
+3  A: 

To add a little to Kotti's answer...

The dot product of two vectors is the product of the absolute values of the lengths of the two vectors and the cosine of the angle between them. Thus, it can be used to project a vector to an arbitrary axis, given the unit vector for that axis. So, given three unit vectors aligned with your x, y, and z axes (i.e. your camera vectors, which may not be aligned with the x, y, and z coordinate axes you're using), you can determine the x, y, and z components of another vector (say, the vector from the camera to a point on an object).

It can also be used to find the angle between two vectors; you just divide out the vectors' lengths. This would be useful in, say, a flight simulator, where it allows you to compute pitch, roll, and heading given four vectors: forward and up unit vectors for the aircraft, and north and up unit vectors for the ground (which change depending on where you are relative to the planet).

A cross product gives you a vector that is perpendicular to both of the given vectors, and its length is equal to the product of the sine of the angle between the vectors and absolute values of the vectors' lengths. Thus, given two unit vectors that are already perpendicular, where the sine of the angle between them is either 1 or -1, you will get a vector perpendicular to both of them, and the three vectors can, say, describe a camera view.

Another thing cross products are good for is finding a vector perpendicular to a plane, given two vectors in a plane. Say you have a triangle you are rendering. Two edges of that triangle can be treated as two vectors, and the cross product between them gives you a vector perpendicular to the triangle. Then, given a vector from a light source to the triangle, you can take the dot product of the latter two vectors to compute the incident angle of the light on the triangle, which you can use to determine how bright it should be.

Mike DeSimone
+1  A: 

Another look at it is that scalar (i.e. dot) product measures the 'parallelness' of vectors, i.e. the closer the result is to the product of lengths of the two vectors, the more parallel they are to each other.

The vector (i.e. cross) product is very useful to determine 'handedness' of vectors: if the result is positive, the second vector is to the right of the first one, and other way round.

In computer graphics you usually use cross product to determine normal of the face and then do dot product of normal and light vector to see how much parallel they are (the more parallel, the better is the face lit).

martin
A: 

As a practical example I asked this question some times ago. The answer is basically the sign of the z coordinates of the cross product of two vectors initially in x, y plane. The reason is given by trigonometry as cross product is related to the sine of the angle between the two initial vectors.

kriss