Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.

Similar presentations


Presentation on theme: "Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4."— Presentation transcript:

1 Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4

2 Today’s Lecture 1.Numbers 2.Numeric Functions 3.Math Functions 4.Vectors & Vector Operations

3 Mathematics distinguishes between several types of number: –Whole numbers (called the natural numbers) 0,1,2…, only positive numbers –Integers 0,1,-1,2,-2,3,-3…, +ve or -ve whole numbers –Real numbers 0.02, -3.455, 124.0, 0.33333, any number written with a decimal point (possibly infinitely long) –As well as others… A number (in any form) is called a scalar Numbers.

4 Numbers in C++ Equivalent types of number in C++: –int - integers (+ve or –ve) –unsigned int - whole numbers (+ve only) –float or double - real numbers Each type has finite storage, e.g. 4-bytes for int –Exact size depends on compiler So they have limitations not present in maths: –Maximum and minimum values E.g. typical int (4-bytes) has max/min of approximately ±2 billion –float can’t represent all possible real numbers E.g. float can’t hold 0.1, value may be 0.0999999977648258

5 More Numbers in C++ There are different sizes of these types: –long int (or long) / short int (or short) More / less precision than int (maybe…) unsigned versions of these too –char, unsigned char Used for characters, but really a 1-byte integer –double and long double Have more precision than float Exact sizes of these also depend on compiler –Don’t assume the size or accuracy of a type in C++ –Unless you use compiler-specific code –Possible exception of char (1-byte)

6 Guidelines for C++ Numbers Large values may be out of range for an int –E.g. Astronomical distances float / double rarely hold exactly the numbers you tell them to – there is usually a small approximation error –Except for a small range of integers –So never use == on floats or doubles Unless you can guarantee that the value will be an exact small integer (e.g. f == 0.0), this situation is rare –Repeated float calculations can cause the error to magnify –The approximation error will be greater for larger values Normally int / float = 4 bytes, double = 8 bytes –Some compilers/platforms use larger values now (int = 8 bytes)

7 Number Conversions in C++ C++ can automatically convert number types –But doesn’t always: 5 / 10 = 0, Correct: 5.0 / 10.0 The compiler usually issues a warning when it does perform a conversion –E.g. int i = 9.0/10.0; // Warning likely (i = 0) –Don’t ignore warnings – ensure it is what you want float / double are always rounded down to int int DownInt = 3.7f; // DownInt = 3 If you want to round to the nearest int: int NearInt = MyFloat + 0.5f; –N.B. 0.5 is a double, 0.5f is a float

8 Number Conversions in C++ Casting is explicitly requesting a conversion The modern C++ casting style is: float f = static_cast (MyInteger); –The older style should be avoided in new code (less safe): float f = (float)MyInteger; Casting will remove warnings about conversions However, use of casting sometimes implies poorly designed code –Ensure that the casting is necessary –Maybe you just need to reconsider the variable types that you are using

9 Basic Functions: Max & Min We frequently need the maximum or minimum of a pair of numbers Don’t repeatedly write if statements in your code Instead, the STL contains a max function int maxSize = std::max( size1, size2 ); This is a template function that works with any type Include the header file You may see code examples with min/max in a macro –A line using the keyword #define –Avoid this approach as it won’t check your types

10 Basic Functions: Remainders Finding remainders is also common –E.g. the remainder of 15 after dividing by 6, is 3 Can use C operators or standard library functions: –A % B = remainder of A / B where A and B are int –fmodf( A, B ) is the same for float –fmod( A, B ) for double E.g. 18 % 7 = 4, fmod( 5.4, 2.0 ) = 1.4 All C++ functions in this lecture are in

11 Math Functions: Absolute Value The absolute value or modulus of a scalar, written |x|, is mathematically defined like this: –Simply removes any minus sign, e.g. |5| = 5, |-23| = 23 –Roughly speaking |x| means “the size of x” –We will see it used for vectors later with this meaning Use abs / fabsf / fabs functions in C++: int i = abs( MyInteger ); float f = fabsf( MyFloat ); // fabs for double Use modulus to find the “distance” between two values int Dist = abs( X2 – X1 ) –Same result even if we swap X1 & X2

12 Math Functions: x y x y is calculated with C++ function pow : –y = pow( x, 4 ); // y = x 4 (= x*x*x*x) –y = pow( x, 0.5 ); // y = √x (= x ½ ) –y = pow( x, 0.333 ); // y = 3 √x (= x ⅓ ) Use pow for doubles, powf for floats Don’t always use pow : –for x 2 use x * x –for x 3 use x * x * x –for √x use sqrt( x ) More efficient for these simpler cases

13 Math Functions: cos & sin Given a right angled triangle: cos θ = AC / AB sin θ = BC / AB There are many other properties of cos and sin –E.g. Circle equations earlier In the C library, angles (i.e. θ ) are measured in radians –360º = 2 π radians, so aº = a * π /180 radians –E.g. 90 degrees = 90 * π /180 = π / 2 radians (about 1.57) So in C++: float f = cos( a * 3.14f/180.0f ); –Where a is an angle in degrees

14 Vectors A vector can be a directed edge joining two points But a vector can be defined without reference to points –E.g. the velocity of an aircraft Vectors are often used to represent a translation (movement) or a velocity The length of vector v(x, y) is: –Note the reuse of the modulus symbol (“size of”) This is the distance travelled for a translation, or the speed for a velocity

15 Normals A normal is any vector whose length is 1 –Sometimes, but not always, represented with a ^ symbol: Any vector can be converted to a normal (normalised) by dividing the components by the length: A normal can be seen as the direction of a vector –A vector can be broken up into normal and length, e.g. –Movement = Normal (direction of movement) + distance –Velocity = Normal (direction of motion) + speed Normals and normalising are very common in graphics

16 Dot Products A dot product is an operation on two vectors, giving a scalar result The dot product of the vectors v 1 (x 1,y 1 ) and v 2 (x 2,y 2 ) is: –Easily calculated The dot product is also equal to: Where θ is angle between the vectors –Just multiply together the lengths of the vectors and the angle between them –A useful form when working with angles

17 Dot Products Dot products become important when the vectors are normalised : | v 1 | = | v 2 | = 1 Using the formulas from last slide: v 1 v 2 = cosθ => θ = cos -1 ( v 1 v 2 ) => θ = cos -1 ( x 1 x 2 + y 1 y 2 ) Allows us can calculate the angle between two vectors: Need the vector components And cos -1 (in C++ use function acos ) Very useful in graphics (and games)


Download ppt "Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4."

Similar presentations


Ads by Google