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

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.

主站蜘蛛池模板: 岫岩| 根河市| 岳西县| 永平县| 内江市| 昭平县| 高淳县| 深泽县| 金秀| 峨眉山市| 瑞昌市| 新绛县| 天柱县| 绥德县| 陆河县| 宝应县| 若尔盖县| 乐平市| 屏边| 台湾省| 驻马店市| 高碑店市| 龙泉市| 桂林市| 昭苏县| 高雄县| 饶阳县| 仁怀市| 梅河口市| 湘潭市| 安宁市| 搜索| 平利县| 通渭县| 申扎县| 西华县| 榆社县| 剑河县| 宜黄县| 随州市| 民勤县|