C++ Programming for Games Module I phần 9 pps

39 285 0
C++ Programming for Games Module I phần 9 pps

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Write a program that does the following: 1. Ask the user to enter up to a line of text and store it in a string s. 2. Transform each alphabetical character in s into its lowercase form. If a character is not an alphabetical character—do not modify it. 3. Output the lowercase string to the console window. Your program output should look like this: Enter a string: Hello, World! Lowercase string = hello, world! Press any key to continue 6.10.4 Palindrome Dictionary.com defines a palindrome as follows: “A word, phrase, verse, or sentence that reads the same backward or forward. For example: A man, a plan, a canal, Panama!” For our purposes, we will generalize and say that a palindrome can be any string that reads the same backwards or forwards and does not have to form a real word or sentence. Thus, some simpler examples may be: “abcdedcba” “C++C” “ProgrammingnimmargorP” Write a program that does the following: 1. Asks the user to enter up to a line of text and store it in a string s. 2. Tests if the string s is a palindrome. 3. If s is a palindrome then output “s is a palindrome” else output “s is not a palindrome.” Your program output should look like this: Enter a string: Hello, World! Hello, World! is not a palindrome Press any key to continue Another sample out Enter a string: abcdedcba abcdedcba is a palindrome Press any key to continue put: 221 Chapter 7 Operator Overloading 222 Introduction In the previous chapter, we saw that we could access a character in a std::string object using the bracket operator ([]). Moreover, we also saw that we could add two std::strings together using the addition operator (+) and that we could use the relational operators ( ==, !=, <, etc) with std::string objects as well. So it appears that we can use (some) C++ operators with std::string. We may assume that perhaps these operators are defined for every class. However, a quick test verifies that this is not the case: class Fraction { public: Fraction(); Fraction(float num, float den); float mNumerator; float mDenominator; }; Fraction::Fraction() { mNumerator = 0.0f; mDenominator = 1.0f; } Fraction::Fraction(float num, float den) { mNumerator = num; mDenominator = den; } int main() { Fraction f(1.0f, 2.0f); Fraction g(3.0f, 4.0f); Fraction p = f * g; bool b = f > g; } The above code yields the errors: C2676: binary '*' : 'Fraction' does not define this operator or a conversion to a type acceptable to the predefined operator C2676: binary '>' : 'Fraction' does not define this operator or a conversion to a type acceptable to the predefined operator. Why can std::string use the C++ operators but we cannot? We actually can, but the functionality is not available by default. We have to define (or overload) these C++ operators in our class definitions. These overloaded operators are defined similarly to regular class methods, and they specify what the operator does in the context of the particular class in which it is being overloaded. For example, what 223 does it mean to multiply two Fractions? We know from basic math that to multiply two fractions we multiply the numerators and the denominators. Thus, we would overload the multiplication operator for Fraction and give an implementation like so: Fraction Fraction::operator *(const Fraction& rhs) { Fraction P; P.mNumerator = mNumerator * rhs.mNumerator; P.mDenominator = mDenominator * rhs.mDenominator; return P; // return the fraction product. } With operator overloading we can make our user-defined types behave very similarly to the C++ built-in types (e.g., float, int). Indeed, one of the primary design goals of C++ was for user-defined types to behave similarly to built-in C++ types. The rest of this chapter describes operator overloading in detail by looking at two different class examples. The first class which we build will model a mathematical vector, which is an essential tool for 3D computer graphics and 3D game programming. However, before proceeding, note that operator overloading is not recommended for every class; you should only use operator overloading if it makes the class easier and more natural to work with. Do not overload operators and implement them with non-intuitive and confusing behavior. To illustrate an extreme case: You should not overload the ‘+’ operator such that it performs a subtraction operation, as this kind of behavior would be very confusing. Chapter Objectives • Learn how to overload the arithmetic operators. • Discover how to overload the relational operators. • Overload the conversion operators. • Understand the difference between deep copies and shallow copies. • Find out how to overload the assignment operator and copy constructor to perform deep copies. 7.1 Vector Mathematics In this section we discuss the mathematics of vectors, in order to understand what they are (the data) and what we can do with them (the methods). Clearly we need to know this information if we are to model a vector in C++ with a class. In 3D computer graphics programming (and many other fields for that matter), you will use a vector to model quantities that consist of a magnitude and a direction. Examples of such quantities are physical 224 forces (forces are applied in a certain direction and have a strength or magnitude associated with them), and velocities (speed and direction). Geometrically, we represent a vector as a directed line segment—Figure 7.1. The direction of the line segment describes the vector direction and the length of the line segment describes the magnitude of the vector. Figure7.1: Geometric interpretation of a vector. Note that vectors describe a direction and magnitude, but they say nothing about location. Therefore, we are free to choose a convenient location from which they originate. In particular, for solving problems, it is convenient to define all vectors such that their “tails” originate from the origin of the working coordinate system, as seen in Figure 7.2. Figure 7.2: A vector with its tail fixed at the origin. Observe that by specifying the coordinates of the vector’s tail we can control its magnitude and direction. 225 Thus we can describe a vector analytically by merely specifying the coordinates of its “head.” This motivates the following structural representation of a 3D vector: class Vector3 { // Methods // Data float mX; float mY; float mZ; }; At first glance, it is easy to confuse a vector with a point, as they both are specified via three coordinate components. However, recall that vector coordinates are interpreted differently than point coordinates; specifically, vector coordinates indicate the end point to which a directed line segment (originating from the origin) connects. Conversely, point coordinates specify a location in space and say nothing about directions and/or magnitudes. What follows is a description of important vector operations. For now, we do not need to worry about the detailed understanding of this math or why it is this way; rather, our goal is simply to understand the vector operation descriptions well enough to implement C++ methods which perform these operations. Note: The Game Mathematics and Graphics Programming with DirectX 9 Part I courses at Game Institute explain vectors in detail. Throughout this discussion we restrict ourselves to 3D vectors. Let zyx uuuu ,,= r and zyx vvvv ,,= r be any vectors in 3-space, and let 3,2,1=p r and 1,3,5 −=q r . Vector Equality: Two vectors are equal if and only if their corresponding components are equal. That is, vu r r = if and only if and Vector Addition ,, yyxx vuvu == zz vu = . : The sum of two vectors is found by adding corresponding components: zzyyxxzyxzyx vuvuvuvvvuuuvu +++=+=+ ,,,,,, rr . Example: 4,1,613,32,511,3,53,2,1 −=+−+=−+=+ qp rr . 226 Geometrically, we add two vectors vu r r + by parallel translating v r so that its tail is coincident with the head of u r and then the sum vu r r + is the vector that originates at the tail of u r and terminates at the head of v r . Figure 7.3 illustrates. Figure 7.3: Vector addition. We translate v r so that its tail coincides with u r . Then the sum vu r r + is the vector from the tail of u r to the head of translated v r . A key observation to note regarding parallel translation of a vector is that the length and direction of the vector is preserved; that is, translating a vector parallel to itself does not change its properties and therefore it is legal to parallel transport them around for visualization. Vector Subtraction: The difference between two vectors is found by subtracting corresponding components: zzyyxxzyxzyx vuvuvuvvvuuuvu −−−=−=− ,,,,,, rr . Example: ( ) 2,5,413,32,511,3,53,2,1 −=−−−−=−−=− qp rr . Geometrically, we can view the difference vu r r − as the vector that originates from the head of v r and terminates at the head of u r . Figure 7.4 illustrates. 227 Figure 7.4: Vector subtraction. We view vector subtraction as the sum ( ) vuvu r r r r − + = − . So first we negate v r and then translate v r − so that its tail coincides with u r . Then ( ) vu r r − + is the vector originating from the tail of u r and terminating at the head of v r − . Observe that subtraction can be viewed as an addition; that is, ( ) vuvu r r r r − + = − , where negating a vector flips its direction. Scalar Multiplication: A vector can be multiplied by a scalar, which modifies the magnitude of the vector but not its direction. To multiply a vector by a scalar we multiply each vector component by the scalar: 321321 ,,,, kvkvkvvvvkvk == r Example: () ( ) () 9,6,333,23,133,2,133 ===p r As the name implies, scalar multiplication scales the length of a vector. Figure 7.5 shows some examples. 228 Figure 7.5: Scalar multiplication. Multiplying a vector by a scalar changes the magnitude of the vector. A negative scalar flips the direction. Vector Magnitude: We use a double vertical bar notation to denote the magnitude of a vector; for example, the magnitude of the vector v r is denoted as v r . The magnitude of a vector is found by computing the distance from the tail of a vector to its head: 2 3 2 2 2 1 vvvv ++= r Example: 14321 222 =++=p r Geometrically, the magnitude of a vector is its length—see Figure 7.6. Figure 7.6: Vector magnitude. The magnitude of a vector is its length. Normalizing a Vector: 229 Normalizing a vector makes its length equal to 1.0. We call this a unit vector and denote it by putting a “hat” on it (e.g., ). We normalize a vector by scalar multiplying the vector by the reciprocal of its magnitude: v ˆ r vvv rrr = ˆ Example: 143142,1413,2,1141 ˆ === ppp rrr . The Dot Product: The dot product of two vectors is the sum of the products of corresponding components: zzyyxxzyxzyx vuvuvuvvvuuuvu ++=⋅=⋅ ,,,, rr It can be proved that θ cosvuvu ⋅=⋅ rr , where θ is the angle between u r and v r . Consequently, a dot product can be useful for finding the angle between two vectors. Example: () ( ) ( ) 23651332511,3,53,2,1 =+−=+−+=−⋅=⋅ qp rr Observe that this vector product returns a scalar—not a vector. The dot product of a vector v r with a unit vector n ˆ r evaluates to the magnitude of the projection of v r onto , as Figure 7.7 shows. n ˆ r 230 [...]... represent magnitudes and directions Examples of such quantities are physical forces (forces are applied in a certain direction and have a strength and magnitude associated with them), and velocities (speed and direction) Geometrically, we represent a vector as a directed line segment The direction of the line segment describes the vector direction, and the length of the line segment describes the magnitude... dest using a c-string C, and we let dest.mData point to C What happens if C changes? If C changes then dest changes as well, since internally, dest’s data representation is a pointer to C What happens if C is destroyed? If C is destroyed then dest becomes invalid automatically, since dest’s internal data representation is a pointer to C (which was destroyed) This kind of behavior might be surprising to... behave similarly to intrinsic C++ types (e.g., float, int) To facilitate this goal, operator overloading was added to C++ Operator overloading enables programmers to overload the C++ operators (e.g., ‘+’, ‘*’, ‘ . “tails” originate from the origin of the working coordinate system, as seen in Figure 7.2. Figure 7.2: A vector with its tail fixed at the origin. Observe that by specifying the coordinates. operation descriptions well enough to implement C++ methods which perform these operations. Note: The Game Mathematics and Graphics Programming with DirectX 9 Part I courses at Game Institute. location. Therefore, we are free to choose a convenient location from which they originate. In particular, for solving problems, it is convenient to define all vectors such that their “tails”

Ngày đăng: 05/08/2014, 09:45

Từ khóa liên quan

Mục lục

  • Background Information

    • Exercise

      • The Dot Product:

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan