【问题标题】:Rolling polygon滚动多边形
【发布时间】:2014-08-20 19:59:28
【问题描述】:

我已经使用 vertex() 函数在处理中创建了一个多边形

现在我想滚动这个多边形,如下图 ..link 所示。请帮我。

void setup() {
  size(400, 400);
}

float angle=0, yl, dx=0;
int N=4, r=50;
void draw() {
  background(-1);
  if (left) dx-=1; 
  if (right) dx+=1;
  if (mousePressed==true)N = (int)map(mouseX, 0, width, 3, 25);
  translate(width>>1, height>>1);
  line(-width/2, 0, width/2, 0);
  float innerAngle = PI-TWO_PI/N; 
  translate(0, -r);
  rotate(PI/2);
  //------------------------------------------
  float theta  = acos(dx/r);
  float dy = r*sin(theta);
  //float dx = r*cos(theta);
  //------------------------------------------
  pushMatrix();
  translate(dx, dy);
  rotate(PI/2-theta);
  polygon(N, r);
  popMatrix();
}

void polygon(int edges, int r) {
  noFill();
  beginShape();
  for (int i=0; i<edges; i++) {
    float x = r*cos(i*TWO_PI/edges);
    float y = r*sin(i*TWO_PI/edges);
    line(0, 0, x, y);
    vertex(x, y);
    pushStyle();
    fill(0);
    text(i+1, x, y);
    popStyle();
  }
  endShape(CLOSE);
  ellipse(0, 0, 2, 2);
}

boolean  right=false, left=false;
void keyPressed() {
  if (key==CODED) {
    if (keyCode == LEFT) {
      left=true;
    }
    if (keyCode == RIGHT) {
      right=true;
    }
  }
}

void keyReleased() {
  left=false;
  right=false;
}

【问题讨论】:

标签: processing polygon


【解决方案1】:

我复制了这段代码,但我不记得是谁了。我是从处理论坛得到的。我认为这可能是 Amnon 的代码,但不确定可能是 Quark 的代码。无论如何,这是对论坛中一个我再也找不到的问题的答案。它可能会帮助您入门。它正在旋转一个矩形:

Square square;
void setup() {  
  size(600, 200);  
  smooth();  
  square = new Square(30, 100, 40, 60, 0.05);
}
void draw() {  
  background(200);  
  line(0, square.y+square.h/2, width, square.y+square.h/2);  
  square.update();  
  square.display();
}
class Square {  
  float x, y, w, h, speed, rX, rY;  
  PVector[] p;  
  Square(float x, float y, float w, float h, float speed) {    
    this.x = x;    
    this.y = y;    
    this.w = w;    
    this.h = h;    
    this.speed = speed;       
    // corner points   
    p = new PVector[4];    
    p[0] = new PVector(-w/2, -h/2);    
    p[1] = new PVector(w/2, -h/2);    
    p[2] = new PVector(w/2, h/2);    
    p[3] = new PVector(-w/2, h/2);
  }  
  void update() {   
    // rotate corners and find most-bottom corner   
    PVector bott = new PVector();    
    for (int i = 0; i<p.length; i++) {      
      p[i].rotate(speed);          
      if (p[i].y > bott.y) {        
        bott = p[i];
      }
    }        
    // calculate x-value    
    float lastrX = rX;    
    rX = x + (w/2 - bott.x);    
    if (lastrX > rX) {      
      x += (lastrX - rX);      
      rX += (lastrX - rX);
    }    // calculate y-value    
    rY = y - (bott.y-h/2);
  } 
  // draw rectangle    
  void display() {    
    pushMatrix();    
    translate(rX, rY);    
    beginShape();    
    vertex(p[0].x, p[0].y);    
    vertex(p[1].x, p[1].y);    
    vertex(p[2].x, p[2].y);    
    vertex(p[3].x, p[3].y);    
    vertex(p[0].x, p[0].y);    
    endShape();    
    popMatrix();
  }
}

【讨论】:

    【解决方案2】:

    围绕接触“地面”的最右侧顶点顺时针旋转多边形,直到另一个顶点接触地面。然后根据需要围绕该顶点重复旋转多边形。

    提示:在旋转新顶点之前使多边形与地面齐平(忽略要旋转的第一个顶点)。否则,被旋转的顶点可能在地面之上或之下。

    注意:看起来您不是在围绕任何特定顶点旋转它,因此可能只是围绕多边形的中心旋转它,这是可行的,但要解决起来要复杂得多。

    【讨论】:

    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-20
    • 2016-09-02
    • 2014-02-02
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多