3

I have a rectangle with an aspect ratio of 2:1. I want to define a smooth curve joining two opposite corners such that the curve's tangent is parallel to the longer edge of the rectangle at one corner, and at the other corner the tangent is at 45deg. For example, if the rectangle's longer edges are horizontal, a curve would enter horizontally at the bottom left and leave at the top right at an angle of 45deg.

How can I define a formula for that curve? I don't mind whether it's Bezier or elliptical etc, but being able to reproduce it accurately in inkscape will be very helpful.

realh
  • 185

2 Answers2

2

I forgot that I need to caculate the length of the curve and find a number of equidistant points along it, so a Bezier curve would make the second part easier. Fortunately Wikipedia's explanation of quadratic Beziers is quite clear and points out that P1 is where the two tangents meet, and P1 is easy to find:

P0's tangent is the x-axis, so a 45 degree line passing through P2 (2, 1) crosses it at P1 (1, 0).

Unfortunately, finding the length of a quadratic curve is rather complicated, and my calculus is rusty, but I think I can manage it.

realh
  • 185
1

A quadratic equation has the properties you want. Imagine a rectangle positioned long side down up against the y-axis - it must have a slope of 1 with a tangent at 45 degrees somewhere. The general form of the quadratic is: $$y = ax^2 + bx + c$$ With slope: $$y' = 2ax + b$$ We know that for y' we want: $$1 = 2ax + b$$ But we also know that at x = 0 for y' we have: $$0 = 2a(0) + b$$ $$b = 0$$ Which determines b as a constant. So substituting b = 0 and, for example, x = 2 (this is a unique value that sets the scale of the graph) into equation 3 gives: $$a = \frac14$$ If our rectangle is above right of the origin then c = 0. So substituting all the constants into the general form gives: $$y = \frac14x^2$$ $$y' = \frac12x$$ At x = 2 this yields both a y-value and a slope of 1, as desired.

Edit - To use the arc length formula with this curve first observe that the arc length l increases smoothly as we move along, therefore: $$l(x + \epsilon) = l(x) + \epsilon l'(x)$$ As with all smooth curves. In particular: $$l(x + \epsilon) - l(x) = \epsilon l'(x)$$ But we also know that: $$s = \epsilon \sqrt(1 + y'^2)$$ From Pythagoras and the normal proof of the formula, where s is a small section of the curve. However, s equals the LHS of the second equation, so combining and cancelling: $$l'(x) = \sqrt(1 + y'^2)$$ $$l'(x) = \sqrt(1 + \frac14x^2)$$ According to the Wolfram online integrator the integral of this is: $$l(x) = \frac12 x\sqrt(\frac14x^2 + 1) + sinh^{-1}(\frac12x)$$ If you want to substitute for l(x) and solve for x you may have to do it numerically, or you could find the points using another numerical method.

  • Thanks, that's easy to understand. I tried to upvote it but I don't have enough reputation. – realh Sep 11 '15 at 16:52
  • No problem, you should be able to accept it with the tick. –  Sep 11 '15 at 16:55
  • Thanks for helping with the length too. Can you just clarify something, does the square root in the last expression end with the close bracket before "+ sinh"? – realh Sep 12 '15 at 13:36
  • I can't say much about that because I just typed the RHS for l'(x) into the box on the Wolfram website - it shows that integrals are quite often hard to find or hard to use. Usually you would have to: 1. Substitute 2 for x and work out l(x); 2. Divide the value into a number n of portions of length p; 3. Substitute the values 1... (n - 1) X p into the LHS of the integral equation; and 4. Solve the integral for x, n - 1 times. Step 4 would entail rearranging for x but to me that looks impossible, so it would have to be done numerically. The other numerical method uses iterative approximations. –  Sep 12 '15 at 14:53
  • What is this for exactly? –  Sep 12 '15 at 14:57
  • I'm writing a game based on controlling trains with signals and points (switches) and I want the curved sections of track to look nice, and for the trains to look like they're running along them properly. – realh Sep 12 '15 at 20:12
  • I tried that equation with x = 2 and the result was 2.295587149 which seems right to me; the straight line between the same points would be 2.236067977. – realh Sep 12 '15 at 20:20
  • All curved sections are quadratic? Anyway, iterative approximation would work like this (it obviously requires coding): 1. Choose a 'very small' x-value as the increment; 2. Work out the corresponding y-values; 3. Work out the linear distances between the y-values; 4. Share out the linear distances between the sections so that the sections (of the curve) are equal; 5. Use a smaller x increment if necessary. This method should also be easier to generalize. –  Sep 15 '15 at 20:34