官术网_书友最值得收藏!

Dot product

The dot product is used to measure how similar two vectors are. Given two vectors, the dot product returns a scalar value. The result of the dot product has the following properties:

  • It is positive if the vectors point in the same direction.
  • It is negative if the vectors point in opposite directions.
  • It is 0 if the vectors are perpendicular.

If both input vectors have a unit length (you will learn about unit length vectors in the Normal vectors section of this chapter), the dot product will have a range of -1 to 1.

The dot product between two vectors, A and B, is equal to the length of A multiplied by the length of B multiplied by the cosine of the angle between the two vectors:

The easiest way to calculate the dot product is to sum the products of like components in the input vectors:

Implement the dot function in vec3.cpp. Don't forget to add the function definition to vec3.h:

float dot(const vec3 &l, const vec3 &r) {

    return l.x * r.x + l.y * r.y + l.z * r.z;

}

The dot product is one of the most used operations for video games. It's often used to check angles and in lighting calculations.

With the dot product, you have implemented the common component-wise operations of vectors. Next, you will learn about some of the non-component-wise operations that can be performed on vectors.

主站蜘蛛池模板: 吉林市| 峡江县| 自贡市| 大同市| 出国| 安义县| 滨海县| 正阳县| 汝南县| 巴林右旗| 文水县| 囊谦县| 永川市| 乐清市| 开阳县| 吉首市| 蕉岭县| 涿州市| 蒙山县| 海口市| 荣昌县| 麻城市| 乐陵市| 泸水县| 白沙| 祁门县| 依安县| 襄樊市| 中山市| 榆社县| 农安县| 孝昌县| 德保县| 舟山市| 广安市| 启东市| 普洱| 封开县| 定边县| 武川县| 台北市|