# Tangent line of curve

**URL:** https://discourse.processing.org/t/tangent-line-of-curve/19600
**Category:** Coding Questions
**Created:** [April 9, 2020, 9:59pm UTC](https://discourse.processing.org/t/tangent-line-of-curve/19600 "2020-04-09T21:59:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Martaf2001](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@Martaf2001](https://discourse.processing.org/u/Martaf2001)
#### Post date: [April 9, 2020, 9:59pm UTC](https://discourse.processing.org/t/tangent-line-of-curve/19600/1 "2020-04-09T21:59:10Z")

</div>

Hi everyone!!!  
I’m new at coding and I have a problem, I would like to draw a program where there is a bicorn curve and every time the mouse is on the curve, it can be generated a tangent line, so on infinite points, but for now I just have the static drawing but I don’t know how to generate a tangent line on the curve as the mouse move…

This is all I have for now

```auto
void setup(){
size(500,500);
}

void draw(){
  background(0);
   translate(width/2, height/2);
   
   noFill();
   stroke(255);
   beginShape();
   for(float a=0; a < TWO_PI; a+=0.01) {
     float r=200;
     float x=(r)*sin (a);
     float y=(-r)*(pow(cos(a),2)/(2-cos(a)));
     vertex(x,y);
   }
 endShape();
 
}

```

THANK YOU SO MUCH !!! 😄

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 10, 2020, 9:13am UTC](https://discourse.processing.org/t/tangent-line-of-curve/19600/2 "2020-04-10T09:13:45Z")

</div>

Hello,

Welcome to the forum.

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 11, 2020, 9:57pm UTC](https://discourse.processing.org/t/tangent-line-of-curve/19600/3 "2020-04-11T21:57:16Z")

</div>

One method:

Currently you draw your bicorn with:

```auto
float x=(r)*sin (a);

```

Say you have your mouseX as input. Your mouseX = 50. What is a solution for `a` for a point on your bicorn that also has x = 50?

If you can solve for a, then you know r (constant) and x (the mouse) – so now you can solve for y on your bicorn. Now you know a point on your bicorn that is x-aligned with the mouse.

To test, add this to the end of your draw:

```auto
  float mx = mouseX-width/2;
  float my = mouseY-height/2;
  ellipse(mx, my, 10, 10);
  mouseCurvePoint(mx);

```

…and create a mouseCurvePoint function to draw a circle.

```auto
void mouseCurvePoint(float x) {

  // solve 'a' here given x, r
  
  // solve 'y' here given x, r, a

  println(x, a);
  ellipse(x, y, 10, 10); // mark your solution point
}

```

 ![Screen Shot 2020-04-11 at 2.58.51 PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/1efe1c2e7b50fe271f7c7a04d133bc8747b83572.png)

Now that you have a point, you need to solve for the slope at that point in order to know the slope of your tangent line. You can then create a line segment of that slope that passes through your point using the slope-intercept form of a line – and you have your tangent!
