Calculate tangent line by mouse/mouse coordinate

Asked

Viewed 161 times

6

I want to calculate the tangent line to the points X and Y found by the mouse, being limited to the circle drawn... I was only able to develop until halfway through the program. Someone can save me??

I am developing the program in Processing for academic purposes.

Follow my code so far:

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

void draw() 
{
    int x = 0;  
    float nX = mouseX, nY = pow(mouseX,2)/300;

    noStroke();
    fill(255);
    rect(0,0,300,300); 

    fill(255,0,0);
    ellipse(nX,nY,10,10);
    text("  "+nX+" "+"/"+nY,nX,nY-5);

    fill(255,0,0);
    stroke(3);
    line(nX, 0, nX, 300);
    line(0, nY, 300, nY);

    while (x < 300) {
        fill(0);
        ellipse(x, x*x/300, 2, 2);
        x = x + 1;
    }

}
  • Um... for me it was not very clear what you want to do. What is the effect you want? The program will draw a circle at the first mouse click, and then will draw the tangent to the circle at a new point under the circle in which the user clicks, is that it? You can include in the question an illustration of the final result?

  • can show us with a drawing the final result you expect?

1 answer

2

To calculate the tangent line, we need to calculate the derivative, which will give us the angular coefficient of the line:

Then we draw a line that passes through the mouse point and with the angular coefficient equal to the derivative applied at that point. The two points To and B which form this line must have x = 0 and x = 300, respectively, and we calculate the height using the formula of the tangent line:

Being P the x value of the mouse, making the accounts for the points, we arrived in:

Now, finally... the programming:

Seeing that is equal to nX and is nY, and declaring coef as ...

float coef = nX / 150;
line(0, nY - coef * nX, 300 , nY + (300 - nX) * coef);

Ready! This will make your tangent straight!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.