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

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.

主站蜘蛛池模板: 叙永县| 张家界市| 德令哈市| 韩城市| 临澧县| 恭城| 安宁市| 穆棱市| 特克斯县| 巨野县| 呼伦贝尔市| 花莲县| 长春市| 大竹县| 武邑县| 盐山县| 沙河市| 龙井市| 荔浦县| 阳春市| 富顺县| 县级市| 青铜峡市| 浦城县| 沂水县| 紫金县| 大丰市| 正镶白旗| 昌图县| 襄垣县| 望城县| 上饶县| 桐庐县| 霍城县| 海南省| 长兴县| 通海县| 夏津县| 岳普湖县| 云浮市| 宜都市|