在加工中如何在圆上移动圆? [英] How to move a circle across a line in processing?

查看:89
本文介绍了在加工中如何在圆上移动圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加工草图中有一个线段和一个圆.在草图中,圆找到线段上的最接近点,然后创建另一条线以显示该最接近点.我希望圆越过这条线向最近的点移动. 我也希望圆在线段本身上找到最接近的点,但是我的草图现在的作用就好像线一直在继续.感谢您的帮助.

I have a line segment and a circle in a processing sketch. In the sketch the circle finds the closest point on the line segment and another line is created to show that closest point. I want the circle to move across this line towards the closest point. Also I want the circle to find the closest point on the line segment itself, but my sketch right now acts as though the line goes on forever. Any help is appreciated.

float x1,y1,x2,y2;
float cx,cy;
float x4,y4;

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

void init() {
   x1 = (int)random(100,500);
   y1 = (int)random(100,500);
   x2 = (int)random(100,500);
   y2 = (int)random(100,500);
   cx = (int)random(100,500);
   cy = (int)random(100,500);   
}   

void draw() {
   background(60);
   init();
   stroke(220);
   line(x1,y1,x2,y2);
   noFill();
   ellipse(cx,cy,50,50);
   noStroke();
   fill(220,20,20);//red- center of circle
   ellipse(cx,cy,8,8);
   // calculate the point
   float k = ((y2-y1) * (cx-x1) - (x2-x1) * (cy-y1)) / 
        ((y2-y1)*(y2-y1) + (x2-    x1)*(x2-x1));
   float x4 = cx - k * (y2-y1);
   float y4 = cy + k * (x2-x1);
   fill(20,20,220); //blue - point on line segment
   ellipse(x4,y4, 8,8);
   stroke(0);
   line(cx,cy,x4,y4);
   noLoop();
}

void keyPressed() { 
   loop(); 
} 

推荐答案

如果有两点,可以使用

If you have two points, you can use the lerp() function to get a series of points between them.

以特定的增量计算两个数字之间的数字. amt参数是在两个值之间进行插值的量,其中0.0等于第一个点,0.1紧邻第一个点,0.5介于第一个点的中间,依此类推.等等.lerp函数便于沿a轴创建运动.直线路径并用于绘制虚线.

Calculates a number between two numbers at a specific increment. The amt parameter is the amount to interpolate between the two values where 0.0 equal to the first point, 0.1 is very near the first point, 0.5 is half-way in between, etc. The lerp function is convenient for creating motion along a straight path and for drawing dotted lines.

您可以在多次调用draw()函数之间创建一个用作amt自变量的变量.然后随时间增加该变量以移动点,并在那里绘制圆.

You can create a variable that you use as the amt argument between multiple calls to the draw() function. Then just increase that variable over time to move the point, and draw your circle there.

这篇关于在加工中如何在圆上移动圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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