Фрактал

Материал из Department of Theoretical and Applied Mechanics
Версия от 19:24, 14 июля 2020; Nickendsm (обсуждение | вклад) (Код программы)

Перейти к: навигация, поиск

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

Код программы на языке 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 		if (degrees != 0) {
 64 		sposob = document.getElementsByName('sposob');
 65 		if (sposob[0].checked == true) {
 66 			a_11 = Math.cos(degrees);
 67 			a_22 = a_11;
 68 			a_12 = Math.sin(degrees);
 69 			a_21 = -a_12;
 70 		}
 71 		if (sposob[1].checked == true) {
 72 			a_11 = Math.cos(degrees*2*Math.PI/360);
 73 			a_12 = Math.sin(degrees*2*Math.PI/360);
 74 			a_21 = -(a_12/a_11);
 75 			a_22 = a_11 - a_21*a_12;
 76 		}
 77 		}
 78 	}
 79 	
 80 	function Func (numb) {
 81 		if (numb>=0) { 
 82 		decim = parseFloat(numb) - parseInt(numb);
 83 	    return (decim);
 84 		} else {
 85 		decim = parseFloat(numb) - (parseInt(numb) - 1);
 86 		return (decim);
 87 		}
 88 	}
 89 	function coord() {
 90 		x = (x)*scale;
 91 		y = (y)*scale;
 92 		sposob = document.getElementsByName('sposob');
 93 		if (sposob[0].checked == true) { 
 94 			numb1 = a_11*x+a_12*y;
 95 			numb2 = a_21*x+a_22*y;
 96 			x = Func(numb1);
 97 			y = Func(numb2);
 98 		}
 99 		if (sposob[1].checked == true) {
100 			numb1 = a_11*x+a_12*y;
101 			x = Func(numb1);
102 			numb2 = a_21*x+a_22*y;
103 			y = Func(numb2);
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 }