<p>Using what Rahul kindly suggested in the comments, I realised that my tangent can be rotated in the opposite direction for two cases:</p>

<ol>
<li>if <code>dy/dt</code> and <code>dx/dt</code> are both negative, yet <code>dy/dx</code> ends up being positive</li>
<li>if <code>dy/dt</code> is negative and <code>dx/dt</code> is positive, so the overall <code>dy/dx</code> is negative</li>
</ol>

<p>This is because my angle calculation, which is done by <code>float angle = atan(dydx) * 180/M_PI;</code> will only calculate acute angles from the horizontal, and hence miss out the opposite cases.</p>

<p>Thanks a bunch!</p>

CC BY-SA 4.0
2018-11-05T05:26:49.550Z
2018-11-05T05:26:49.550Z
umop apisdn
<p>I am well aware that there are several ways to find the tangents to a Bezier curve. However, I choose to just substitute the result of differentiating the Bezier curve equation.</p>

<p>So, given four control points, <code>ptOne</code>, <code>ptTwo</code>, <code>ptThree</code> and <code>ptFour</code>:</p>

<pre><code>// Differentiate x with respect to t
float dxdt = -3*(1 - t)*(1 - t)*ptOne.x
+ 3*(1 - 4*t + 3*t*t)*ptTwo.x
+ 3*(2*t - 3*t*t)*ptThree.x
+ 3*t*t*ptFour.x;

// Differentiate y with respect to t
float dydt = -3*(1 - t)*(1 - t)*ptOne.y
+ 3*(1 - 4*t + 3*t*t)*ptTwo.y
+ 3*(2*t - 3*t*t)*ptThree.y
+ 3*t*t*ptFour.y;

// Find the tangent, dy/dx
float dydx = dydt * (1/dxdt);

// Find the rotation angle in degrees
float angle = atan(dydx) * 180/M_PI;
</code></pre>

<p>The strange thing is that this does not seem to be working for all kinds of curve. For example, if I draw a curve like this, with the first control point being the second leftmost point and then in a clockwise direction:</p>

<p><a href="https://i.sstatic.net/eSfoL.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/eSfoL.png" alt="enter image description here"></a></p>

<p>and attempt to draw the tangents:</p>

<p><a href="https://i.sstatic.net/i0Kdk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/i0Kdk.png" alt="enter image description here"></a></p>

<p>You can see that some tangents (in particular, the first two and very last one) are not oriented in the right direction. Why might this be happening? I triple checked my differentiation and can't find anything wrong...</p>

CC BY-SA 4.0
2018-11-03T16:47:39.613Z
OK, I'll give it a shot tomorrow. Thanks for the suggestion!
umop apisdn
CC BY-SA 4.0
2018-11-05T09:55:24.263Z
@Rahul Noting that, sometimes one needs to use the 2nd (or 3rd) derivatives if the 1st derivative vanishes.
Simon F
CC BY-SA 4.0
2018-11-06T08:53:13.173Z
@Rahul It's been a long time since I was on the OpenVG spec group so this is not exactly fresh in my memory, but what of the cubic Bezier curve with control points A, A, B, C?
Simon F
<mathematics><bezier-curve>
Wrong tangential directions for cubic bezier curve
umop apisdn
# Wrong tangential directions for cubic bezier curve

(Asked by: username_0 on 2018-11-03)

I am well aware that there are several ways to find the tangents to a Bezier curve. However, I choose to just substitute the result of differentiating the Bezier curve equation.

So, given four control points, `ptOne`, `ptTwo`, `ptThree` and `ptFour`:

```
// Differentiate x with respect to t
float dxdt = -3*(1 - t)*(1 - t)*ptOne.x
+ 3*(1 - 4*t + 3*t*t)*ptTwo.x
+ 3*(2*t - 3*t*t)*ptThree.x
+ 3*t*t*ptFour.x;

// Differentiate y with respect to t
float dydt = -3*(1 - t)*(1 - t)*ptOne.y
+ 3*(1 - 4*t + 3*t*t)*ptTwo.y
+ 3*(2*t - 3*t*t)*ptThree.y
+ 3*t*t*ptFour.y;

// Find the tangent, dy/dx
float dydx = dydt * (1/dxdt);

// Find the rotation angle in degrees
float angle = atan(dydx) * 180/M_PI;

```

The strange thing is that this does not seem to be working for all kinds of curve. For example, if I draw a curve like this, with the first control point being the second leftmost point and then in a clockwise direction:

[![enter image description here](https://i.sstatic.net/eSfoL.png)](https://i.sstatic.net/eSfoL.png)

and attempt to draw the tangents:

[![enter image description here](https://i.sstatic.net/i0Kdk.png)](https://i.sstatic.net/i0Kdk.png)

You can see that some tangents (in particular, the first two and very last one) are not oriented in the right direction. Why might this be happening? I triple checked my differentiation and can't find anything wrong...

**Tags:** mathematics, bezier-curve

> **Comments:**
> **username_0**: OK, I'll give it a shot tomorrow. Thanks for the suggestion!  
> **username_1**: @Rahul Noting that, sometimes one needs to use the 2nd (or 3rd) derivatives if the 1st derivative vanishes.  
> **username_1**: @Rahul It's been a long time since I was on the OpenVG spec group so this is not exactly fresh in my memory, but what of the cubic Bezier curve with control points A, A, B, C?  

---

## 1 answers

### Answer by username_0 on 2018-11-05. Score: 0 | Accepted answer

Using what Rahul kindly suggested in the comments, I realised that my tangent can be rotated in the opposite direction for two cases:

1. if `dy/dt` and `dx/dt` are both negative, yet `dy/dx` ends up being positive
2. if `dy/dt` is negative and `dx/dt` is positive, so the overall `dy/dx` is negative

This is because my angle calculation, which is done by `float angle = atan(dydx) * 180/M_PI;` will only calculate acute angles from the horizontal, and hence miss out the opposite cases.

Thanks a bunch!


---

