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

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.

主站蜘蛛池模板: 双流县| 新郑市| 吉隆县| 潮州市| 鹤庆县| 淳安县| 介休市| 安泽县| 金华市| 东兰县| 罗定市| 修文县| 湛江市| 五指山市| 石景山区| 新干县| 樟树市| 克什克腾旗| 旺苍县| 富宁县| 赤水市| 武夷山市| 宕昌县| 罗田县| 乾安县| 尉犁县| 浦县| 德化县| 邢台市| 黔江区| 海淀区| 微山县| 黔西县| 石柱| 英德市| 舟曲县| 辽中县| 黄浦区| 龙门县| 旬阳县| 巴林右旗|