forked from CodingTrain/Infinite-Rainbows
/
Vec2D.js
executable file
·139 lines (116 loc) · 2.38 KB
/
Vec2D.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Infinite Rainbows
// This code is adapted from Infinite Rainbows,
// a generative animation by [Marius Watz](http://mariuswatz.com/).
// The original Actionscript version was commissioned by
// [POKE](http://pokelondon.com) as part of Good Things Should Never End,
// an online brand experience for Orange UK.
// Marius Watz and Poke have generously given permission
// for this p5.js port started by Daniel Shiffman for use
// on the YouTube channel "Coding Rainbow."
// This work is licensed under a Creative Commons
// Attribution-NonCommercial 4.0 International License
// http://creativecommons.org/licenses/by-nc/4.0/).
// Still in progress!
// Vec2D - simple 2D vector class
// processing.unlekker.net
class orange.lib.Vec2D {
var x,y,DEG_TO_RAD:Number;
function Vec2D(_inx,_iny) {
DEG_TO_RAD=Math.PI/180;
if(arguments.length==0) {
x=0;
y=0;
}
else {
x=_inx;
y=_iny;
}
}
function setTo(_inx,_iny) {
if(arguments.length==1) {
x=_inx.x;
y=_inx.y;
}
else {
x=_inx;
y=_iny;
}
}
function addTo(_inx,_iny) {
if(arguments.length==1) {
x=_inx.x;
y=_inx.y;
}
else {
x+=_inx;
y+=_iny;
}
}
function addDirection(dir,speed) {
x+=Math.cos(dir*DEG_TO_RAD)*speed;
y+=Math.sin(dir*DEG_TO_RAD)*speed;
}
function setCirclePos(dir,radius) {
x=Math.cos(dir*DEG_TO_RAD)*radius;
y=Math.sin(dir*DEG_TO_RAD)*radius;
}
function sub(_inx,_iny) {
x-=_inx;
y-=_iny;
}
function subV(v) {
x-=v.x;
y-=v.y;
}
function mult(m) {
x*=m;
y*=m;
}
function div(m) {
x/=m;
y/=m;
}
function lengthV() {
return Math.sqrt(x*x+y*y);
}
function angle() {
return Math.atan2(y,x);
}
static function findAngle(xd,yd) {
return Math.atan2(yd,xd);
}
function norm(m:Number) {
var l=Math.sqrt(x*x+y*y);
if(l!=0) {
x/=l;
y/=l;
}
if(m!=undefined) {
x*=m;
y*=m;
}
}
function setToTangent(v1,v2) {
x=-(v2.y-v1.y);
y=v2.x-v1.x;
var l=Math.sqrt(x*x+y*y);
if(l!=0) {
x/=l;
y/=l;
}
}
function tangent(v1,v2) {
return new Vec2D(-y,x);
}
function rotate(val) {
var cosval=Sys.COS(val);
var sinval=Sys.SIN(val);
var tmpx=x*cosval - y*sinval;
var tmpy=x*sinval + y*cosval;
x=tmpx;
y=tmpy;
}
function toString():String {
return "["+x+","+y+"]";
}
}