同步操作将从 飞行器智能感知与控制/python_turtle 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
// The Nature of Code// Daniel Shiffman// http://natureofcode.com// Pendulum// A Simple Pendulum Class// Includes functionality for user can click and drag the pendulumclass Pendulum {PVector position; // position of pendulum ballPVector origin; // position of arm originfloat r; // Length of armfloat angle; // Pendulum arm anglefloat aVelocity; // Angle velocityfloat aAcceleration; // Angle accelerationfloat ballr; // Ball radiusfloat damping; // Arbitary damping amountboolean dragging = false;// This constructor could be improved to allow a greater variety of pendulumsPendulum(PVector origin_, float r_) {// Fill all variablesorigin = origin_.get();position = new PVector();r = r_;angle = PI/4;aVelocity = 0.0;aAcceleration = 0.0;damping = 0.995; // Arbitrary dampingballr = 48.0; // Arbitrary ball radius}void go() {update();drag(); //for user interactiondisplay();}// Function to update positionvoid update() {// As long as we aren't dragging the pendulum, let it swing!if (!dragging) {float gravity = 0.4; // Arbitrary constantaAcceleration = (-1 * gravity / r) * sin(angle); // Calculate acceleration (see: http://www.myphysicslab.com/pendulum1.html)aVelocity += aAcceleration; // Increment velocityaVelocity *= damping; // Arbitrary dampingangle += aVelocity; // Increment angle}}void display() {position.set(r*sin(angle), r*cos(angle), 0); // Polar to cartesian conversionposition.add(origin); // Make sure the position is relative to the pendulum's originstroke(0);strokeWeight(2);// Draw the armline(origin.x, origin.y, position.x, position.y);ellipseMode(CENTER);fill(175);if (dragging) fill(0);// Draw the ballellipse(position.x, position.y, ballr, ballr);}// The methods below are for mouse interaction// This checks to see if we clicked on the pendulum ballvoid clicked(int mx, int my) {float d = dist(mx, my, position.x, position.y);if (d < ballr) {dragging = true;}}// This tells us we are not longer clicking on the ballvoid stopDragging() {aVelocity = 0; // No velocity once you let godragging = false;}void drag() {// If we are draging the ball, we calculate the angle between the// pendulum origin and mouse position// we assign that angle to the pendulumif (dragging) {PVector diff = PVector.sub(origin, new PVector(mouseX, mouseY)); // Difference between 2 pointsangle = atan2(-1*diff.y, diff.x) - radians(90); // Angle relative to vertical axis}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。