与曲线相切
Tangent to the curve
我是编码新手,遇到问题,我想画一个有曲线的程序,但现在我只有静态图,但我不知道如何生成切线由于他的导数,曲线上的线,随着鼠标的移动…
This is all I have for now
void draw(){
background(255);
noFill();
stroke(0);
beginShape();
for(float a=0; a < TWO_PI; a+=0.01) {
float r=78;
float x=sin (a);
float y=(pow(cos(a),2)/(2-cos(a)))
我的想法是制作一个光标,它随曲线移动,生成的每个 tima 都是自己的切线。
非常感谢!!!!!
我建议使用 PVector
进行计算。
创建一个计算形状上的点的函数:
PVector fSahpe(float a) {
float r = 200;
float x = r * sin(a);
float y = -r *(pow(cos(a),2)/(2-cos(a)));
return new PVector(x, y);
}
你必须找到形状上离鼠标位置最近的点。在绘制形状时找到最近的点。请注意,由于形状已平移,因此必须沿相反方向移动用于比较鼠标位置与形状上的点的鼠标位置:
PVector m = new PVector(mouseX-width/2, mouseY-height/2);
dist()
can be used to compute the Euclidean distance两点之间:
float mindist = 1000;
float mina = 0;
for(float a=0; a < TWO_PI; a+=0.01) {
PVector p = fSahpe(a);
// [...]
float dist = PVector.dist(p, m);
if (dist < mindist) {
mindist = dist;
mina = a;
}
}
定义阈值距离。如果鼠标到曲线上最近点的距离低于该距离,则绘制切线:
if (mindist < 10) {
// [...] draw tangent
}
计算曲线上的2个相互靠近的点,其中一个点是距离鼠标光标最近的点:
PVector p0 = fSahpe(mina);
PVector p1 = fSahpe(mina+0.01);
这2点ar在近似切线上。计算从一个点到另一个点的向量并将其缩放到一定长度(长度是切线的一半长度):
PVector dir = PVector.sub(p1, p0);
dir.normalize().mult(100);
计算切线的起点和终点:
PVector l0 = PVector.add(p0, dir);
PVector l1 = PVector.sub(p0, dir);
查看完整示例:
void setup() {
size(500, 500);
}
PVector fSahpe(float a) {
float r = 200;
float x = r * sin(a);
float y = -r *(pow(cos(a),2)/(2-cos(a)));
return new PVector(x, y);
}
void draw(){
background(0);
translate(width/2, height/2);
noFill();
strokeWeight(1);
stroke(255);
float mindist = 1000;
float mina = 0;
PVector m = new PVector(mouseX-width/2, mouseY-height/2);
beginShape();
for(float a=0; a < TWO_PI; a+=0.01) {
PVector p = fSahpe(a);
vertex(p.x, p.y);
float dist = PVector.dist(p, m);
if (dist < mindist) {
mindist = dist;
mina = a;
}
}
endShape();
if (mindist < 10) {
PVector p0 = fSahpe(mina);
PVector p1 = fSahpe(mina+0.01);
PVector dir = PVector.sub(p1, p0);
dir.normalize().mult(100);
PVector l0 = PVector.add(p0, dir);
PVector l1 = PVector.sub(p0, dir);
strokeWeight(3);
stroke(255, 0, 0);
line(l0.x, l0.y, l1.x, l1.y);
}
}
我是编码新手,遇到问题,我想画一个有曲线的程序,但现在我只有静态图,但我不知道如何生成切线由于他的导数,曲线上的线,随着鼠标的移动…
This is all I have for now
void draw(){
background(255);
noFill();
stroke(0);
beginShape();
for(float a=0; a < TWO_PI; a+=0.01) {
float r=78;
float x=sin (a);
float y=(pow(cos(a),2)/(2-cos(a)))
我的想法是制作一个光标,它随曲线移动,生成的每个 tima 都是自己的切线。
非常感谢!!!!!
我建议使用 PVector
进行计算。
创建一个计算形状上的点的函数:
PVector fSahpe(float a) {
float r = 200;
float x = r * sin(a);
float y = -r *(pow(cos(a),2)/(2-cos(a)));
return new PVector(x, y);
}
你必须找到形状上离鼠标位置最近的点。在绘制形状时找到最近的点。请注意,由于形状已平移,因此必须沿相反方向移动用于比较鼠标位置与形状上的点的鼠标位置:
PVector m = new PVector(mouseX-width/2, mouseY-height/2);
dist()
can be used to compute the Euclidean distance两点之间:
float mindist = 1000;
float mina = 0;
for(float a=0; a < TWO_PI; a+=0.01) {
PVector p = fSahpe(a);
// [...]
float dist = PVector.dist(p, m);
if (dist < mindist) {
mindist = dist;
mina = a;
}
}
定义阈值距离。如果鼠标到曲线上最近点的距离低于该距离,则绘制切线:
if (mindist < 10) {
// [...] draw tangent
}
计算曲线上的2个相互靠近的点,其中一个点是距离鼠标光标最近的点:
PVector p0 = fSahpe(mina);
PVector p1 = fSahpe(mina+0.01);
这2点ar在近似切线上。计算从一个点到另一个点的向量并将其缩放到一定长度(长度是切线的一半长度):
PVector dir = PVector.sub(p1, p0);
dir.normalize().mult(100);
计算切线的起点和终点:
PVector l0 = PVector.add(p0, dir);
PVector l1 = PVector.sub(p0, dir);
查看完整示例:
void setup() {
size(500, 500);
}
PVector fSahpe(float a) {
float r = 200;
float x = r * sin(a);
float y = -r *(pow(cos(a),2)/(2-cos(a)));
return new PVector(x, y);
}
void draw(){
background(0);
translate(width/2, height/2);
noFill();
strokeWeight(1);
stroke(255);
float mindist = 1000;
float mina = 0;
PVector m = new PVector(mouseX-width/2, mouseY-height/2);
beginShape();
for(float a=0; a < TWO_PI; a+=0.01) {
PVector p = fSahpe(a);
vertex(p.x, p.y);
float dist = PVector.dist(p, m);
if (dist < mindist) {
mindist = dist;
mina = a;
}
}
endShape();
if (mindist < 10) {
PVector p0 = fSahpe(mina);
PVector p1 = fSahpe(mina+0.01);
PVector dir = PVector.sub(p1, p0);
dir.normalize().mult(100);
PVector l0 = PVector.add(p0, dir);
PVector l1 = PVector.sub(p0, dir);
strokeWeight(3);
stroke(255, 0, 0);
line(l0.x, l0.y, l1.x, l1.y);
}
}