Computer Graphics Basic Maths for Graphics in C++

Slides:



Advertisements
Similar presentations
Building Java Programs
Advertisements

Return values.
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
CS 201 Functions Debzani Deb.
1 Fundamental Data types Overview l Primitive Data Types l Variable declaration l Arithmetical Operations l Expressions l Assignment statement l Increment.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
22-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Python Programming, 2/e1 Python Programming: An Introduction to Computer Science Chapter 3 Computing with Numbers.
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
IT253: Computer Organization
Lecture 2 Introduction to Computer Programming CUIT A.M. Gamundani Presentation Layout Data types.
CSC1015F – Chapter 3, Computing with Numbers Michelle Kuttel
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
Lecture #5 Introduction to C++
CSC 107 – Programming For Science. Announcements  Lectures may not cover all material from book  Material that is most difficult or challenging is focus.
How a Computer Processes Information. Java – Numbering Systems OBJECTIVE - Introduction to Numbering Systems and their relation to Computer Problems Review.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
PHY 107 – Programming For Science. Announcements  Lectures may not cover all material from readings  Material that is most difficult or challenging.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Computer Graphics Matrices
Basic Java Syntax Comments Basic data types Operators and assignment.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Primitive Data Types. int This is the type you are familiar with and have been using Stores an integer value (whole number) between -2,147,483,648 (-2.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
CS 115 Lecture 5 Math library; building a project Taken from notes by Dr. Neil Moore.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Variables, Operators, and Expressions
Chapter 2 Binary Number Systems.
Programming and Data Structure
Chapter 6 - Functions modular programming general function format
Chapter 2 Basic Computation
Binary Numbers The arithmetic used by computers differs in some ways from that used by people. Computers perform operations on numbers with finite and.
Data Types, Variables & Arithmetic
Chapter 4 C Program Control Part I
Expressions.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Floating Point Math & Representation
Tokens in C Keywords Identifiers Constants
Assignment and Arithmetic expressions
Topic 3d Representation of Real Numbers
Fundamental Data Types
Chapter 2 Basic Computation
CS1S467 GUI Programming Lecture 3 Variables 2.
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Numerical Data Types.
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Arithmetic Expressions & Data Conversions
Lectures on Numerical Methods
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
CS190/295 Programming in Python for Life Sciences: Lecture 3
Chapter 3 Operators and Expressions
Fundamental Data Types
CO1301: Games Concepts Lecture 21 Timing + Quads + Numbers
Summary of what we learned yesterday
Loops.
Primitive Types and Expressions
Unit 3: Variables in Java
What is 311° in radians? Have you ever wondered how to find the radian equivalent of an angle like 311 degrees?
Arithmetic Expressions & Data Conversions
Presentation transcript:

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

Today’s Lecture Numbers Numeric Functions Math Functions Vectors & Vector Operations

Numbers 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 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

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)

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)

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

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

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

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 fmod( A, B ) for double and float E.g. 18 % 7 = 4, fmod( 5.4, 2.0 ) = 1.4 All C++ functions in this lecture are in <math.h>

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 functions in C++: int i = abs( MyInteger ); float f = abs( MyFloat ); Use modulus to find the “distance” between two values int Dist = abs( X2 – X1 ) Same result even if we swap X1 & X2

Math 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⅓) 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

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

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

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

Dot 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: 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

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