Please help me with this. Thank you! spirograph This toy consisted of pens and a
ID: 3872677 • Letter: P
Question
Please help me with this. Thank you!
spirograph This toy consisted of pens and a wheel that would travel inside of another wheel. The figure below shows us one of the paths traced out as one wheel travels inside of another one. The traced curve is the light figure. It turns out that one of a mathematical ilk can actually derive the equations for the pen's trajectory x = (R-r)*cos (phi) + p*cos (( (R-r)/r)"phi ) I want you to write a program called yourlastnameassign1.R It will have inputs of R, r, and p with default values of 100, 2, and 80. Your function should allowl to take on 2001 values between 0 and 2*pi. The plot area is xmin--180.0, ymin =-180.0, Xmax = 180.0, ymax = 180.The next page indicates the rules of the assignment, what to turn in, and the grading sheet I will use.Explanation / Answer
window.requestAnimFrame = (function () {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function Scene() {
this.animation = undefined;
this.canvas = undefined;
this.height = 0;
this.width = 0;
this.context = undefined;
this.paused = false;
this.stats = undefined;
this.istats = undefined;
}
Scene.prototype = {
constructor: Scene,
setup: function (canvas, animation, width, height, stats) {
this.canvas = canvas;
this.animation = animation;
this.height = this.canvas.height = height;
this.width = this.canvas.width = width;
this.context = this.canvas.getContext('2d');
this.stats = stats && window.Stats;
if (this.stats) {
this.istats = new Stats();
this.istats.setMode(0);
this.istats.domElement.style.position = 'absolute';
this.istats.domElement.style.left = '0px';
this.istats.domElement.style.top = '0px';
this.istats.domElement.style.zIndex = '99999';
document.body.appendChild(this.istats.domElement);
}
},
animate: function () {
if (!this.paused) {
requestAnimFrame(this.animate.bind(this));
}
this.stats && (this.istats.begin());
this.animation(this); // removed draw function, it was stupid.
this.stats && (this.istats.end());
}
};
function Particle() {
this.x = 0;
this.sx = 0; // start x
this.vx = 0; // velocity x
this.y = 0;
this.sy = 0; // start y
this.vy = 0; // velocity y
this.thrust = 0;
this.angle = 0;
this.size = 1;
this.canExplode = false;
this.color = "#fff";
}
Particle.prototype = {
constructor: Particle,
update: function () {
this.vy += settings.gravity;
this.x += this.vx;
this.y += this.vy;
}
};
function ParticleEmitter() {
this.particles = [];
}
ParticleEmitter.prototype = {
constructor: ParticleEmitter,
create: function (args) {
var n = args.n,
x = args.x,
y = args.y,
angle = args.angle,
speed = args.speed,
size = args.size || 1,
color = args.color || "#fff",
explode = args.explode || false;
var particle = new Particle(),
na = angle + 180;
particle.x = particle.sx = x;
particle.y = particle.sy = y;
particle.size = size / 2;
particle.thrust = speed;
particle.color = color;
particle.vx = Math.cos((na * Math.PI / 180)) * particle.thrust;
particle.vy = Math.sin((na * Math.PI / 180)) * particle.thrust;
particle.canExplode = explode;
this.particles.push(particle)
},
update: function (ctx, width, height) {
var self = this;
this.particles.forEach(function (particle, i) {
var colors = ['#D6BF86', '#FFFBD0', '#9C2A00', particle.color];
ctx.fillStyle = particle.color;
ctx.beginPath();
ctx.arc(particle.x, particle.y, particle.size / 2, 0, Math.PI * 2, false);
ctx.fill();
particle.update(width, height);
if (!(particle.x < width && particle.x > 0 && particle.y < height)) {
self.particles.splice(i, 1);
if (particle.canExplode) {
for (var i = 0, l = (particle.size * particle.size); i < l; i++) {
self.create({
x: particle.x + -particle.vx,
y: particle.y + -particle.vy,
angle: Math.random() * 360 | 0,
color: colors[Math.random() * colors.length | 0],
speed: Math.random() * 3 + settings.explosion_strength, //2
explode: false,
size: Math.random() * ((particle.size * 2) * settings.fragment_size_ratio) + .1
});
}
}
}
});
}
};
var settings = {
angle: 145,
speed: 7,
gravity: 0.17,
bullet_size: 22,
explosion_strength: 1.7,
fragment_size_ratio: .33,
bullet_color: '#fff',
mouse_aim: true,
max_speed: 30
};
// stuff
var canvas = document.getElementById('canvas'),
len = document.getElementById('len'),
height = canvas.height = document.body.offsetHeight,
width = canvas.width = document.body.offsetWidth,
scene = new Scene(),
emitter = new ParticleEmitter(),
gx = width / 8,
gy = height - height / 8;
function run(scene) {
var ctx = scene.context;
ctx.clearRect(0, 0, scene.width, scene.height);
emitter.update(ctx, scene.width, scene.height);
var a = settings.angle + 180,
endX = gx + (settings.speed * 2) * Math.cos(a * Math.PI / 180),
endY = gy + (settings.speed * 2) * Math.sin(a * Math.PI / 180);
ctx.strokeStyle = "#fff";
ctx.beginPath();
ctx.moveTo(gx, gy);
ctx.lineTo(endX, endY);
ctx.stroke();
}
scene.setup(canvas, run, width, height, !1);
scene.animate();
window.onresize = function () {
height = canvas.height = scene.height = document.body.offsetHeight;
width = canvas.width = scene.width = document.body.offsetWidth;
gx = width / 8;
gy = height - height / 8;
};
canvas.onclick = function (event) {
emitter.create({
x: gx,
y: gy,
angle: settings.angle,
speed: settings.speed,
explode: true,
color: settings.bullet_color,
size: settings.bullet_size // 10
});
};
canvas.onmousemove = function (event) {
if (settings.mouse_aim) {
var deltaY = event.clientY - gy;
var deltaX = event.clientX - gx;
var deg = Math.atan2(deltaY, deltaX) * 180 / Math.PI;
settings.angle = deg + 180;
var xs = deltaX * deltaX,
ys = deltaY * deltaY,
distance = Math.sqrt(xs * ys);
distance = (distance / 1000);
if (distance > settings.max_speed) {
distance = settings.max_speed
};
settings.speed = distance;
}
};
var gui = new dat.GUI();
gui.add(settings, 'angle', 0, 360).listen();;
gui.add(settings, 'speed', 0, settings.max_speed).listen();;
gui.add(settings, 'gravity', -1, 2);
gui.add(settings, 'bullet_size', 1, 35);
gui.add(settings, 'explosion_strength', .1, 20);
gui.add(settings, 'fragment_size_ratio', 0.01, 1);
gui.addColor(settings, 'bullet_color', '#fff');
gui.add(settings, 'mouse_aim');
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.