在“处理"中没有在期望的位置绘制. [英] Not drawing at desired position in "Processing"

查看:96
本文介绍了在“处理"中没有在期望的位置绘制.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过拖动鼠标在一个位置进行绘制(例如,左上象限中的一个圆圈),并使图形出现在画布中心周围的四个位置(包括鼠标光标下方的一个位置).我尝试了以下代码:

I am trying to draw at one place by dragging a mouse (for example, a circle in left upper quadrant) and have the figure appear at 4 places around center of canvas (including one right under the mouse cursor). I have tried following code:

int x=0;
int y=0;

void setup(){
   size(400, 400);  
   background(255); 
   translate(width/2, height/2); 
}
void draw() {
}

void mouseDragged(){
    translate(width/2, height/2); 

    for(int i=0; i<4; i++){
      rotate(PI/2); 
      x= mouseX ; y=mouseY;
      line(x, y, mouseX, mouseY);
    }
  }

但是,该图不会在鼠标光标下绘制.如果我非常靠近左上角,则线条会在画布的中心绘制.

However, the figure does not get drawn under the mouse cursor. If I am very close to upper left corner, the lines get drawn at center of canvas.

其次,总共绘制了5个数字,而不是我正在尝试的4个数字.如果我在左上角附近绘制,则不需要的图形会出现在画布的右下角.这个不需要的数字比其他数字暗淡.

Secondly, a total of 5 figures get drawn instead of 4 as I am trying. If I draw near left upper corner, the unwanted figure appears at right lower corner of canvas. This unwanted figure is fainter than other figures.

错误在哪里,如何解决这两个问题?我想通过旋转命令(围绕画布的中心旋转)来实现这一点,尽管我看到我也可以使用平移命令在画布上的4个位置进行绘制.

Where are the errors and how can both these problems be corrected? I want to achieve this with rotate commands (rotating around center of canvas), though I saw that I can also use translate command to draw at 4 places on canvas.

感谢您的帮助.

推荐答案

您的所有点都不在鼠标下方,因为在绘制点之前您正在将画布平移(转换为width/2, height/2),但是您使用的是绘制它们.将mouseX, mouseY视为距左上角的X和Y距离.如果要使一个点位于鼠标的下面,则必须使用与平移到该点的距离!

None of your points are under the mouse because you're translating the canvas (to width/2, height/2) before drawing your points, but you're using mouseX, mouseY to draw them. Think of mouseX, mouseY as the X and Y distance from the upper-left corner. If you want a point to be under the mouse, you have to use the distance from the point you translated to!

换句话说,代替这个:

x = mouseX; 
y = mouseY;

您想要这个:

x = width/2 - mouseX; 
y = height/2 - mouseY;

至于所画的额外点,似乎是使用mouseDragged()函数的副作用.发生了一些奇怪的事情,但是您可以通过使用draw()函数和mousePressed变量而不是mouseDragged()函数来解决此问题.

As for the extra point being drawn, that seems to be a side-effect of using the mouseDragged() function. Something strange is going on there, but you can get around this problem by just using the draw() function and the mousePressed variable instead of the mouseDragged() function.

:我切换了它们应按的顺序.查看我对您的

I switched the order they should be in. Check out my answer to your next question for a better explanation.

这篇关于在“处理"中没有在期望的位置绘制.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆