File: ParametricFunction.js

package info (click to toggle)
openboard 1.5.4%2Bdfsg1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 29,372 kB
  • sloc: cpp: 72,239; javascript: 6,503; xml: 172; makefile: 156; ansic: 38; sh: 2
file content (116 lines) | stat: -rw-r--r-- 2,640 bytes parent folder | download | duplicates (3)
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


function ParametricFunction(fctX, fctY){

	this.setFct = function(fctX, fctY){
		this.fct = "x(t) = " + fctX + " ; y(t) = " + fctY;
		this.fctX = fctX;
		this.fctY = fctY;
		this.fx = new Function("t", "return "+this.fctX);
		this.fy = new Function("t", "return "+this.fctY);
	}
	
	if(fctX && fctY){
		this.setFct(fctX, fctY);
	}
	this.couleur = fct.couleur;
	this.width = document.getElementById("inputTaille").value;
	this.startAngle = 0;
	this.endAngle = 2*Math.PI;
	this.from = "0";
	this.to = "2*pi";
	this.style = document.getElementById("selectStyle").value;
	
	this.getX = function(t){
		return this.fx(t);
	};
	
	this.getY = function(t){
		return this.fy(t);
	};
	
	this.set = function(f){
		var fctX = fct.remplacer(fct.verifier(f.fctX));
		var fctY = fct.remplacer(fct.verifier(f.fctY));
		this.setFct(fctX, fctY);
		this.couleur = f.couleur;
		this.width = f.width;
		this.style = f.style;
		this.from = f.from;
		this.to = f.to;
		this.startAngle = eval(fct.remplacer(fct.verifier(f.from)));
		this.endAngle = eval(fct.remplacer(fct.verifier(f.to)));
		return this;
	};
	
	this.get = function(){
		var f = {};
		f.type = "parametric";
		f.fctX = this.fctX;
		f.fctY = this.fctY;
		f.couleur = this.couleur;
		f.width = this.width;
		f.style = this.style;
		f.from = this.from;
		f.to = this.to;
		return f;
	};
		
	this.readableText = function(){
		return "x(t) = " + this.fctX + "; y(t) = " + this.fctY;
	};
	
	this.setStartEnd = function(start, end){
		if(start > end){
			var tmp = start;
			start = end;
			end = start;
		}
		this.startAngle = start;
		this.endAngle = end;
	};
	
	this.plot = function(ctx, precision, affichage){
		if(this.style == "points"){
			precision *= 2;
		}
		
		ctx.beginPath();
		ctx.lineWidth = this.width;
		ctx.strokeStyle = this.couleur;
		ctx.fillStyle = this.couleur;
		notDefined = true;
		for(var t = this.startAngle; t <= this.endAngle; t+=precision){
			var x = this.fx(t);
			var y = this.fy(t);
			if(!isNaN(x) && !isNaN(y)){
				// Transform coordinates
				var pointX = (x - affichage.xGauche) * affichage.multX;
				var pointY = affichage.hauteur - (y - affichage.yBas) * affichage.multY;
				
				// Draw point
				if(notDefined){
					notDefined = false;
					ctx.moveTo(pointX, pointY);
				}
				else{
					if(this.style == "continu"){
						ctx.lineTo(pointX, pointY);
					}
					else if(this.style == "points"){
						ctx.beginPath();
						ctx.arc(pointX, pointY, this.width, 0, 2*Math.PI, true);
						ctx.fill();
					}
					else{
						ctx.lineTo(pointX, pointY);
						notDefined = true;
					}
				}
			}
		}
		if(this.style != "points"){
			ctx.stroke();
		}
	};
}