gpt4 book ai didi

java - 加工时如何将圆移过线?

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:42 25 4
gpt4 key购买 nike

我在加工草图中有一条线段和一个圆。在草图中,圆找到线段上最近的点,并创建另一条线来显示该最近的点。我希望圆圈穿过这条线向最近的点移动。另外,我希望圆找到线段本身最近的点,但我的草图现在的表现就好像这条线永远持续下去。如有任何帮助,我们将不胜感激。

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();
}

最佳答案

如果有两个点,可以使用lerp()函数来获取它们之间的一系列点。

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 参数。然后随着时间的推移增加该变量来移动该点,并在那里绘制圆圈。

关于java - 加工时如何将圆移过线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com