-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07.js
91 lines (53 loc) · 2.91 KB
/
07.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
// A library/module/package is a collection of implementations
//
// 1. INTERNAL LIBRARY (STANDARD LIBRARY)
// 2. EXTERNAL LIBRARY (THIRD-PARTY LIBRARY)
// 3. OWN LIBRARY
var fs = require('fs') // Importing internal Node.js library
var geo = require('./geometry.js') // Importing own library
//////////////////////////////////////////
// EXAMPLES OF USING INTERNAL LIBRARIES //
//////////////////////////////////////////
// MATH (BUILT-IN OBJECT)
var x = 5
var y = 2
var z = Math.PI // Constant π Result: 3.141592...
z = Math.E // Constant e (Euler's constant) Result: 2.718281...
z = Math.pow(x, y) // Returns x raised to the power y
z = Math.exp(x) // Returns the exponential function of a number.
z = Math.log(x) // Returns the natural logarithm of a number.
z = Math.abs(x) // Returns the absolute value of a number.
z = Math.round(x) // Returns the value of a number rounded to the nearest integer.
z = Math.floor(x) // Returns the largest integer less than or equal to a number.
z = Math.ceil(x) // Returns the smallest integer greater than or equal to a number.
z = Math.sin(x) // Returns the sine of an angle (angle is in radians).
z = Math.cos(x) // Returns the cosine of an angle (angle in radians).
z = Math.tan(x) // Returns the tangent of an angle (angle in radians).
z = Math.atan(x) // Returns the arctangent of a number, in radians.
z = Math.atan2(y, x) // Returns the arctangent of the quotient of its arguments (y over x), in radians.
z = Math.max(x, y) // Returns the largest of zero or more numbers (try to use more numbers).
z = Math.min(x, y) // Returns the smallest of zero or more numbers (try to use more numbers).
// RANDOM (BUILT-IN OBJECT)
z = Math.random() // Returns a pseudo-random number between 0 and 1. Example result: 0.64816...
// Getting a random integer between two values, inclusive (example: between -3 and 10, inclusive)
var min = Math.ceil(-3)
var max = Math.floor(10)
var random_integer = Math.floor(Math.random() * (max - min + 1)) + min
// WRITE AND READ TEXT FILES (USING BUILT-IN NODE.JS MODULE)
var filename = 'text.txt'
var txt = 'Hello, world!' + '\n' + 'This is another text line!'
fs.writeFileSync(filename, txt, (err) => {if (err) throw err}) // Write a string in a new text file
var content = fs.readFileSync(filename, 'utf8') // Assign all the text from the file in a string variable
console.log(content)
//////////////////////////////////////////
// EXAMPLES OF USING EXTERNAL LIBRARIES //
//////////////////////////////////////////
// To be completed
////////////////////////////
// EXAMPLES OF OWN MODULE //
////////////////////////////
var p1 = new geo.Point(2, -1)
var p2 = new geo.Point(-2, 2)
var euc_dist = geo.euclidean_distance(p1, p2)
console.log(euc_dist)