简体   繁体   English

如何让时钟的指针旋转?

[英]How can I make the hands of the clock rotate?

How can I make the hands of the clock rotate?如何让时钟的指针旋转? By me only the whole picture rotates.在我看来,只有整个画面在旋转。 The hands are in the same class as the exterior of the clocks.指针与时钟的外部属于同一类。 The clocks move across the image.时钟在图像上移动。 Thank you.谢谢你。

Here is a part of the code:这是代码的一部分:

function startViz() {
  for (let i = 0; i < 5; i++) {
    bubbles.push(new Bubble());
  }
}

function drawViz() {
  ground.background(30);

  for (let b of bubbles) {
    b.update();
    b.show();
    b.show1();
  }
}

class Bubble {
  constructor() {
    this.r = random(50, 80);
    this.x = random(this.r, width - this.r);
    this.y = random(this.r, height - this.r);
    this.vx = random(-1 * (w*0.005), 3 * (w*0.005));
    this.vy = random(-1 * (w*0.005), 3 * (w*0.005));
    this.color = color(random(255), random(255), random(255));
  }

  show() {
    ground.noFill();
    ground.stroke(255);
    ground.fill(this.color);
    ground.strokeWeight(3);
    ground.circle(this.x, this.y, this.r * 2 * (w*0.005));
  }

  show1() {
    ground.line(this.x, this.y, this.x + this.r / 2 * (w*0.005), this.y + this.r / 2 * (w*0.005));
    ground.line(this.x, this.y, this.x - this.r / 2 * (w*0.005), this.y + this.r / 2 * (w*0.005));
  }

  update() {
    this.x += this.vx;
    this.y += this.vy;
    if (this.x > width - this.r || this.x < this.r) {
      this.vx *= -1;
    }
    if (this.y > height - this.r || this.y < this.r) {
      this.vy *= -1;
    }
  }
}

First I recommend fixing syntax errors and posting code that runs so it's easy for other contributors to replicate your issue.首先,我建议修复语法错误并发布运行的代码,以便其他贡献者轻松复制您的问题。

There are multiple ways to do this:有多种方法可以做到这一点:

  1. You can use push() / pop() calls to isolate coordinate systems and rotate the handles您可以使用push() / pop()调用来隔离坐标系并旋转手柄
  2. You can use the polar to cartesian coordinates conversion function to rotate the tips of the handles around the clock centres.您可以使用极坐标到笛卡尔坐标转换功能来围绕时钟中心旋转手柄的尖端。

The formula is:公式为:

x = cos(angle) * radius
y = sin(angle) * radius

(You can find a detailed explanation here ) (你可以在这里找到详细的解释

Option 1 is pretty straight forward:选项 1 非常简单:

show1() {
    ground.push();
    ground.translate(this.x, this.y);
    ground.rotate(frameCount * 0.1);
    ground.line(0, 0, this.r / 2 * (w*0.005), this.r / 2 * (w*0.005));
    ground.line(0, 0, - this.r / 2 * (w*0.005), this.r / 2 * (w*0.005));
    ground.pop();
}

In this case frameCount * 0.1 is used as an angle (in radians).在这种情况下frameCount * 0.1用作角度(以弧度为单位)。

Option 2 would look like this if you were to use two independent angles for each clock handle:如果您要为每个时钟手柄使用两个独立的角度,选项 2 将如下所示:

show1(){
    let angle1 = frameCount * 0.1;
    let angle2 = frameCount * 0.01666666667;
    
    let x1 = cos(angle1) * this.r;
    let y1 = sin(angle1) * this.r;
    
    let x2 = cos(angle2) * this.r * 0.75;
    let y2 = sin(angle2) * this.r * 0.75;
    
    ground.line(this.x, this.y, this.x + x1, this.y + y1);
    ground.line(this.x, this.y, this.x + x2, this.y + y2);
}

Here's a version of your sketch with the push() / pop() option and two angles:这是带有push() / pop()选项和两个角度的草图版本:

 let bubbles = []; let ground; let w; function setup() { createCanvas(400, 400); w = width; startViz(); ground = createGraphics(width, height); } function draw() { drawViz(); } function startViz() { for (let i = 0; i < 5; i++) { bubbles.push(new Bubble()); } } function drawViz() { ground.background(30); for (let b of bubbles) { b.update(); b.show(); b.show1(); } // noLoop(); image(ground, 0, 0); } class Bubble { constructor() { this.r = random(50, 80); this.x = random(this.r, width - this.r); this.y = random(this.r, height - this.r); this.vx = random(-1 * (w*0.005), 3 * (w*0.005)); this.vy = random(-1 * (w*0.005), 3 * (w*0.005)); this.color = color(random(255), random(255), random(255)); } show() { ground.noFill(); ground.stroke(255); ground.fill(this.color); ground.strokeWeight(3); ground.circle(this.x, this.y, this.r * 2 * (w*0.005)); } show1() { ground.push(); ground.translate(this.x, this.y); ground.rotate(frameCount * 0.1); ground.line(0, 0, this.r / 2 * (w*0.005), this.r / 2 * (w*0.005)); ground.pop(); ground.push(); ground.translate(this.x, this.y); ground.rotate(frameCount * 0.01); ground.line(0, 0, - this.r / 2 * (w*0.005), this.r / 2 * (w*0.005)); ground.pop(); } update() { this.x += this.vx; this.y += this.vy; if (this.x > width - this.r || this.x < this.r) { this.vx *= -1; } if (this.y > height - this.r || this.y < this.r) { this.vy *= -1; } } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>

Notice the order of transformations is important!注意转换的顺序很重要! First we translate to the clock's centre, then we rotate (otherwise we'd get a totally different result).首先我们转换到时钟的中心,然后我们旋转(否则我们会得到完全不同的结果)。

(I wasn't sure what w was meant to be so assumed width (similar to Anton's function )) (我不确定w是什么意思如此假设的width (类似于安东的函数))

The indentation isn't necessary, but hopefully it helps visually isolate coordinate systems in code.缩进不是必需的,但希望它有助于在视觉上隔离代码中的坐标系。

Additionally you can encapsulate repeating code into a reusably function:此外,您可以将重复代码封装到可重用函数中:

show1(){
    drawClockHandle(frameCount * 0.1, this.r);
    drawClockHandle(frameCount * 0.01666666667, this.r * 0.75);
}
drawClockHandle(angle, radius){
      ground.push();
      ground.translate(this.x, this.y);
      ground.rotate(angle);
      ground.line(0, 0, radius, 0);
    ground.pop();    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM