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

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

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

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

Computer GraphicsBasic Maths for Graphics in C++

CO2409 Computer Graphics

Week 4

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

Today’s LectureToday’s Lecture

1. Numbers

2. Numeric Functions

3. Math Functions

4. Vectors & Vector Operations

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

• 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

NumbersNumbers

.

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

Numbers in C++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

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

More Numbers in C++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)

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

Guidelines for C++ Numbers 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)

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

Number Conversions in C++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 intint 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

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

Number Conversions in C++Number Conversions in C++

• Casting is explicitly requesting a conversion

• The modern C++ casting style is:float f = static_cast<float>(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

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

Basic Functions: Max & MinBasic 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 functionint maxSize = std::max( size1, size2 );

• This is a template function that works with any type• Include the <algorithm> 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

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

Basic Functions: RemaindersBasic 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 <math.h>

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

Math Functions: Absolute ValueMath 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 valuesint Dist = abs( X2 – X1 )

– Same result even if we swap X1 & X2

xxxxx || otherwise ,|| then0 if

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

Math Functions: xyMath Functions: xy

• xy is calculated with C++ function pow:– y = pow( x, 4 ); // y = x4 (= 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 x2 use x * x– for x3 use x * x * x– for √x use sqrt( x )

• More efficient for these simpler cases

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

Math Functions: cos & sinMath Functions: cos & sin

• Given a right angled triangle:cos θ = AC / ABsin θ = 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

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

VectorsVectors

• 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: 22|| yx v

– Note the reuse of the modulus symbol (“size of”)

• This is the distance travelled for a translation, or the speed for a velocity

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

NormalsNormals

• 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

vvvv /,/ˆ is , of normal the yxyx

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

Dot ProductsDot Products

• A dot product is an operation on two vectors, giving a scalar result

• The dot product of the vectors v1(x1,y1) and v2(x2,y2) is:

– Easily calculated

• The dot product is also equal to:

2121 yyxx 21 vv

cos2121 vvvv

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

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

Dot ProductsDot Products

• Dot products become important when the vectors are normalised :

|v1| = |v2| = 1

• Using the formulas from last slide:

v1• v2 = cosθ

=> θ = cos-1(v1• v2)

=> θ = cos-1(x1 x2 + y1 y2)

• 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)