Merge pull request 'Smoothie programmer do the job' (#1) from MrBlonde/canvas:smoothie into master
Reviewed-on: djmil/canvas#1
This commit is contained in:
		
						commit
						9206eff8c1
					
				
							
								
								
									
										22
									
								
								tree.js
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								tree.js
									
									
									
									
									
								
							@ -1,7 +1,10 @@
 | 
			
		||||
function stem(ctx, x1, y1, length, angle) {
 | 
			
		||||
const balance_angle = angle => angle * (Math.PI / 180)
 | 
			
		||||
 | 
			
		||||
const stem = (ctx, x1, y1, length, angle) => {
 | 
			
		||||
	// Визначити координати кіня стовбура знаючи почткову точку стовбура, його довжину та кут нахилу. Так, це тригонометрія в дії :)
 | 
			
		||||
	var x2 = x1 + length * Math.sin(angle * (Math.PI /180));
 | 
			
		||||
	var y2 = y1 - length * Math.cos(angle * (Math.PI /180));
 | 
			
		||||
    const balanced_angle = balance_angle(angle)
 | 
			
		||||
	let x2 = x1 + length * Math.sin(balanced_angle);
 | 
			
		||||
	let y2 = y1 - length * Math.cos(balanced_angle);
 | 
			
		||||
	
 | 
			
		||||
	ctx.beginPath();
 | 
			
		||||
	ctx.moveTo(x1, y1);
 | 
			
		||||
@ -12,13 +15,13 @@ function stem(ctx, x1, y1, length, angle) {
 | 
			
		||||
 | 
			
		||||
	// Закоментуй цей IF. Що змінилось в картинці дерева? Чому стало гірше?
 | 
			
		||||
	if (length > 20) {
 | 
			
		||||
		x2 = x1 + (length * 0.95) * Math.sin(angle * (Math.PI /180));
 | 
			
		||||
		y2 = y1 - (length * 0.95) * Math.cos(angle * (Math.PI /180));
 | 
			
		||||
		x2 = x1 + (length * 0.95) * Math.sin(balanced_angle);
 | 
			
		||||
		y2 = y1 - (length * 0.95) * Math.cos(balanced_angle);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    if (length > 1) {
 | 
			
		||||
		var rnd1 = Math.random() *20  -10;  // [-10.0 .. +10.0]
 | 
			
		||||
		var rnd2 = Math.random() *0.2 +0.6; // [  0.6 ..   0.8]
 | 
			
		||||
		const rnd1 = Math.random() *20  -10;  // [-10.0 .. +10.0]
 | 
			
		||||
		const rnd2 = Math.random() *0.2 +0.6; // [  0.6 ..   0.8]
 | 
			
		||||
 | 
			
		||||
		// Малюємо розгалудження стовбура. За початок нового стовбура використовується кінець поточного стовбура
 | 
			
		||||
      	stem(ctx, x2, y2, length * rnd2, angle + 35 +rnd1);
 | 
			
		||||
@ -26,8 +29,5 @@ function stem(ctx, x1, y1, length, angle) {
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function tree(ctx, x, y, height) {
 | 
			
		||||
const tree = (ctx, x, y, height) => stem(ctx, x, y, height, 0);
 | 
			
		||||
 | 
			
		||||
	stem(ctx, x, y, height, 0);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user