本机和在线草图之间的不同处理渲染

时间:2019-03-30 08:57:55

标签: processing open-source p5.js processing.js

在直接使用Processing并在浏览器中使用Processing.js运行此示例时,我得到不同的结果。为什么?

我对我的结果感到满意,并希望在开放的Processing中共享它,但是渲染完全不同,我不知道为什么。下面是一个最小的工作示例。

/* Program that rotates a triange and draws an ellipse when the third vertex is on top of the screen*/

float y = 3*height/2;
float x = 3*width/2;

float previous_1 = 0.0;
float previous_2 = 0.0;
float current;
float angle = 0.0;


void setup() {
  size(1100, 500);
}

void draw() {

  fill(0, 30);

  // rotate triangle
  angle = angle - 0.02;
  translate(x, y); 
  rotate(angle); 

  // display triangle
  triangle(-50, -50, -30, 30, -90, -60);

  // detect whether third vertex is on top by comparing its 3 successive positions
  current = screenY(-90, -60); // current position of the third vertex

  if (previous_1 < previous_2 && previous_1 < current) {
    // draw ellipse at the extrema position
    fill(128, 9, 9);
    ellipse(-90, -60, 7, 10); 
  }

  // update the 2 previous positions of the third vertex
  previous_2 = previous_1;
  previous_1 = current;
}
  • 在加工中,椭圆形是在三角形顶点位于顶部时绘制的,这是我的目标。
  • 在在线素描中,椭圆是在整个时间内绘制的:/

2 个答案:

答案 0 :(得分:2)

问题出在screenY函数中。本地和在线打印处理草图中的current变量。在OpenProcessing中,变量current迅速增长到数千以上,而本地变量则保持在0到〜260之间。

OpenProcessing似乎在此函数内部存在错误。

但是,要解决此问题,建议您在圆的顶部绘制三角形时进行其他注册,例如,使用角度变量:

// Calculate angle and modulo it by 2 * PI
angle = (angle - 0.02) % (2 * PI);

// If the sketch has made a full revolution
if (previous_1 < previous_2 && previous_1 < angle) {
    // draw ellipse at the extrema position
    fill(128, 9, 9);
    ellipse(-90, -60, 7, 10); 
}

// update the 2 previous angles of the third vertex
previous_2 = previous_1;
previous_1 = angle;

但是,由于如何绘制三角形,椭圆的角度大约为PI / 3。要解决此问题,一种方法是像这样将屏幕旋转angle + PI / 3

rotate(angle + PI / 3);

您可能需要尝试更多的角度偏移才能将椭圆完美地绘制在圆的顶部。

答案 1 :(得分:1)

为了获得与通过本地运行Processing相同的结果,您需要在调用size

时将渲染模式指定为3d。

例如:

void setup() {
  size(1100, 500, P3D);
}

您还需要在对screenY()的调用中指定z坐标

current = screenY(-90, -60, 0);

通过这两项更改,您应该在线获得与本地运行相同的结果。

在线:

Triangle Ellipse Example

本地:

Triangle Ellipse Exampl