Converting Maths, Physics formulas to codes
Notes:
I. Converting Maths, Physics formulas to codes : [Starts at: 00min:00sec]
II. Addition of 2 numbers : [Starts at: 06min:07sec]
III. Area and Circumference of a circle : [Starts at: 09min:31sec]
IV. a + b / a - b : [Starts at: 16min:57sec]
V. (a + b) square : [Starts at: 20min:06sec]
VI. Projectile motion : [Starts at: 22min:21sec]
VII. x = x / ((x / 3) - 1) : [Starts at: 29min:52sec]
VIII. Pythagorus Theorem : [Starts at: 31min:44sec]
IX. Sine,Cos waves for circular, parabolit motion : [Starts at: 34min:55sec]
I. Converting Maths, Physics formulas to codes : [Starts at: 00min:00sec]
Steps for converting expresssions to codes:
1. Identify variables and constants
2. Identify the datatype of variables and constants (i.e. size, precision etc)
3. Convert expressions to codes
Note:
While converting expressions
must think about precedence of operators
numerator and denominators must be evaluated seperately
must use built in properties and methods
II. Addition of 2 numbers : [Starts at: 06min:07sec]
c = a +b
Example Code:
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float a = 10.0f, b = 10.0f, c = 0.0f;
c = a + b;
Debug.Log (c); // 20
}
}
III. Area of a circle : [Starts at: 09min:31sec]
areaOfCircle = PI * r square;
circumfereceOfCircle = 2 * PI * r;
where:
r is radius of circle
Example Code:
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float areaOfCircle = 0.0f, r = 3.0f;
const float PI = 3.142f;
areaOfCircle = PI * (r * r);
Debug.Log (areaOfCircle); // 28.278
// OR
// use built in properties and methods for accuracy
areaOfCircle = Mathf.PI * Mathf.Pow (r, 2);
Debug.Log (areaOfCircle); // 28.27433
float circumfereceOfCircle=0f;
circumfereceOfCircle = 2 * Mathf.PI * r;
Debug.Log(circumOfCircle);
}
}
IV. a + b / a - b : [Starts at: 16min:57sec]
a+b
-----
a -b
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float a = 10.0f, b = 10.0f;
Debug.Log (a + b / a - b); // wrong : 1
// Think about precedence for accuracy
// while dividing numerator and denominator must be evaluated seperately
Debug.Log((a+b) / (a-b)); //correct : Infinity
}
}
V. (a + b) square : [Starts at: 20min:06sec]
(a+b) square = a square + b square + 2ab
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float a = 10.0f, b = 10.0f;
Debug.Log((a*a) + (b*b) + (2* a*b)); // 400
// use built in functions
Debug.Log(Mathf.Pow(a,2) + Mathf.Pow(b,2) + (2* a*b)); // 400
}
}
VI. Projectile motion : [Starts at: 22min:21sec]
yf = u0*t - 1/2 * g * t square + y0
where
yf = Nexty position (variable)
u0 = initial velocity ( const)
g = gravirty (const)
t = time (variable)
y0 = Previousy position (variable)
Example Code:
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float yf = 0.0f, y0 = 0.0f, t = 1.0f;
const float g = -9.8f, u0 =40.0f;
yf = (u0 * t) - (0.5f * g * t * t) + y0;
Debug.Log (yf); // 44.9
// use built in functions
yf = (u0 * t) - (0.5f * g * Mathf.Pow(t,2)) + y0;
Debug.Log (yf); // 44.9
}
}
VII. x = x / ((x / 3) - 1) : [Starts at: 29min:52sec]
x
---------
x
--- - 1
3
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float x = 1.0f;
x = x / ((x / 3) - 1);
Debug.Log (x); // -1.5
}
}
VIII. Pythagorus Theorem : [Starts at: 31min:44sec]
c = square root of a square + b square
Example Code:
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float a=3.0f, b=4.0f, c=0.0f;
c = Mathf.Sqrt ((a * a) + (b * b));
Debug.Log (c); // 5
//0R
c = Mathf.Sqrt(Mathf.Pow(a,2)+Mathf.Pow(b,2));
Debug.Log (c); // 5
}
}
IX. Sine,Cos waves for circular, parabolit motion : [Starts at: 34min:55sec]
xf = cos (theta * freq) * amp + x0;
yf = sin (theta * freq) * amp + y0;
where:
theta should in radians
Example Code:
using UnityEngine;
public class ExpressionsDemo : MonoBehaviour {
void Start () {
float x0=100.0f, xf, freq=2.0f, amp=2.0f, theta = 90.0f;
xf = Mathf.Cos( (theta * (Mathf.PI/180)) *freq) * amp + x0;
Debug.Log (xf); // 98
xf = Mathf.Cos((theta * Mathf.Deg2Rad) *freq) * amp + x0;
Debug.Log (xf); //98
}
}