Фрактал

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск

Код программы

Код программы на языке JavaScript:
  1 window.addEventListener('load',main,false);
  2 function main () {
  3 	var x; var y;
  4 	var ctx = cnv.getContext('2d');
  5 	var h = cnv.height;
  6 	var w = cnv.width;
  7 	var scale = 1/w;
  8 	var a_11;var a_12;var a_21; var a_22;
  9 	var numb1; var numb2; var decim;
 10 	var on = false;
 11 	var interv;
 12 	a_11 = 1;
 13 	a_12 = 1;
 14 	a_21 = 1;
 15 	a_22 = 1;
 16 	var degrees = 0;
 17 	var sposob;
 18 	var clear;
 19 
 20 	a11.onchange = function () {
 21 		a_11 = parseFloat(document.getElementById('a11').value);
 22 		clearInterval(interv);
 23 		on = false;
 24 	}
 25 	a12.onchange = function() {
 26 		a_12 = parseFloat(document.getElementById('a12').value);
 27 		clearInterval(interv);
 28 		on = false;
 29 	}
 30 	a21.onchange = function () {
 31 		a_21 = parseFloat(document.getElementById('a21').value);
 32 		clearInterval(interv);
 33 		on = false;
 34 	}
 35 	a22.onchange = function () {
 36 		a_22 = parseFloat(document.getElementById('a22').value);
 37 		clearInterval(interv);
 38 		on = false;
 39 	}
 40 	
 41 	degree.onchange = function() {
 42 		degrees = parseFloat(document.getElementById('degree').value);
 43 		clearInterval(interv);
 44 		on = false;
 45 	}
 46 		
 47 	cnv.onmousedown = function () {
 48 		var rect = cnv.getBoundingClientRect();
 49 		x = (event.clientX - rect.left);
 50 		y = (event.clientY - rect.top);
 51 		clear = document.getElementsByName('clear');
 52 		if (clear[0].checked == true) {
 53 		ctx.clearRect(0,0,w,h);
 54 		ctx.beginPath();
 55 		ctx.arc(x,y,3,0,2*Math.PI);
 56 		ctx.fillStyle = 'blue';
 57 		ctx.fill();
 58 		}
 59 		if (on == false) {
 60 			interv = setInterval(control,0.001);
 61 			on = true;
 62 		}
 63 		sposob = document.getElementsByName('sposob');
 64 		if (sposob[0].checked == true) {
 65 			a_11 = Math.cos(degrees);
 66 			a_22 = a_11;
 67 			a_12 = Math.sin(degrees);
 68 			a_21 = -a_12;
 69 		}
 70 		if (sposob[1].checked == true) {
 71 			a_11 = Math.cos(degrees*2*Math.PI/360);
 72 			a_12 = Math.sin(degrees*2*Math.PI/360);
 73 			a_21 = -(a_12/a_11);
 74 			a_22 = a_11 - a_21*a_12;
 75 		}
 76 	}
 77 	
 78 	function Func (numb) {
 79 		if (numb>=0) { 
 80 		decim = parseFloat(numb) - parseInt(numb);
 81 	    return (decim);
 82 		} else {
 83 		decim = parseFloat(numb) - (parseInt(numb) - 1);
 84 		return (decim);
 85 		}
 86 	}
 87 	function coord() {
 88 		x = (x)*scale;
 89 		y = (y)*scale;
 90 		sposob = document.getElementsByName('sposob');
 91 		if (sposob[0].checked == true) { 
 92 			numb1 = a_11*x+a_12*y;
 93 			numb2 = a_21*x+a_22*y;
 94 			x = Func(numb1);
 95 			y = Func(numb2);
 96 			console.log(1);
 97 		}
 98 		if (sposob[1].checked == true) {
 99 			numb1 = a_11*x+a_12*y;
100 			x = Func(numb1);
101 			numb2 = a_21*x+a_22*y;
102 			y = Func(numb2);
103 			console.log(2);
104 		}
105 		y = y/scale;
106 		x = x/scale;
107 		console.log(x,y);
108 	}
109 	function draw() { 
110 		ctx.beginPath();
111 		ctx.rect(x,y,1,1);
112 		ctx.strokeStyle = 'red';
113 		ctx.stroke();
114 	}
115 	
116 	function control () {
117 		coord();
118 		draw();
119 	}
120 	
121 	
122 }