-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec.js
More file actions
162 lines (126 loc) · 3.47 KB
/
vec.js
File metadata and controls
162 lines (126 loc) · 3.47 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
let vec;
let scale;
function start(){
vec = new Vector(0, 0);
scale = 25;
setCanvas(600, 600);
translate(300, 300);
}
function draw(){
//Grid
for(let x = Math.floor(-((width / 2) / scale)); x < (width / 2) / scale; x++){
for(let y = -(width / 2); y < height / scale; y++){
strokeWeight(2);
setStroke("black");
setFill("grey");
rect(x * scale, y * scale, x * scale + scale, y * scale +scale);
}
}
//Axis
setColor("darkgreen");
strokeWeight(5);
line(-(width / 2), 0, width / 2, 0);
line(0, -(width / 2), 0, width / 2);
//Vector line from origin
setColor("red");
strokeWeight(5);
line(0, 0, vec.x * scale, vec.y * scale);
preImage();
postImage();
}
function parseVec(){
let toParse = document.getElementById("vecVal").value;
let x = parseFloat(/<(\-?[\d]*),/.exec(toParse)[1]);
let y = parseFloat(/,\s?(\-?[\d]*)>/.exec(toParse)[1]);
vec = new Vector(x, -y);
}
function preImage(){
let points2 = [];
setColor("blue");
let points = document.getElementById("points").value.split("\n");
for(let i = 0; i < points.length; i++){
if(points[i].charAt(0) != "="){
let x = parseFloat(/\(?(\-?[\d\.]*),/.exec(points[i])[1]);
let y = parseFloat(/,(\-?[\d\.]*)\)?/.exec(points[i])[1]);
circ(x * scale, -y * scale, 6);
points2.push(new Vector(x * scale, -y * scale));
}else{
let eq = parseEquation(points[i]);
for(let x = Math.floor(-((width/2)/scale)); x < (width/2)/scale; x += 0.01){
let y = eq.getY(x);
circ(x * scale, -y * scale, 2);
}
}
}
if(points2.length != 0){
polygon(points2);
}
}
function postImage(){
let points2 = [];
setColor("red");
let points = document.getElementById("points").value.split("\n");
for(let i = 0; i < points.length; i++){
if(points[i].charAt(0) != "="){
let x = parseFloat(/\(?(\-?[\d\.]*),/.exec(points[i])[1]);
let y = parseFloat(/,(\-?[\d\.]*)\)?/.exec(points[i])[1]);
circ((x + vec.x) * scale, (-y + vec.y) * scale, 6);
points2.push(new Vector((x + vec.x) * scale, (-y + vec.y) * scale));
}else{
let eq = parseEquation(points[i]);
for(let x = Math.floor(-((width/2)/scale)); x < (width/2)/scale; x += 0.01){
let y = eq.getY(x);
circ((x + vec.x) * scale, (-y + vec.y) * scale, 2);
}
}
}
if(points2.length != 0){
polygon(points2);
}
}
function zoom(amount){
scale += amount;
}
function parseEquation(eq){
let terms = [];
let nterms = [];
let term = getMatch(/([\-\+\d\.]*x[\^\-\d]*)/, eq);
for(let i = 0; i < term.length; i++){
let k = parseFloat(/(\-?[\d\.]*)x/.exec(term[i])[1]);
let b = 1;
if(/x\^(\-?[\d\.]*)/.test(term[i])){
b = parseFloat(/x\^(\-?[\d\.]*)/.exec(term[i])[1]);
}
terms.push(new Term(k, b));
nterms.push(-k);
}
if(/([\+\-][\d\.]*)/.test(eq)){
ns = getMatch(/([\+\-][\d\.]*)/, eq);
for(let i = 0; i < ns.length; i++){
nterms.push(parseFloat(ns[i]));
}
}
return new Equation(terms, nterms);
}
class Term {
constructor(k, b) {
this.k = k;
this.b = b;
}
}
class Equation {
constructor(terms, nterms) {
this.terms = terms;
this.nterms = nterms;
}
}
Equation.prototype.getY = function(x) {
let val = 0;
for (let i = 0; i < this.terms.length; i++) {
val += this.terms[i].k * Math.pow(x, this.terms[i].b);
}
for (let i = 0; i < this.nterms.length; i++) {
val += this.nterms[i];
}
return val;
};