tag:support.nodebox.net,2012-11-01:/discussions/show-your-work/436-quad-curvesNodeBox: Discussion 2021-06-20T07:21:40Ztag:support.nodebox.net,2012-11-01:Comment/492418872021-06-11T00:35:15Z2021-06-20T07:21:40ZQuad Curves<div><p>UPDATE: See reply to this note for a v2 version which corrects a minor bug in the quad_control node.</p>
<p>Bezier curves are at the heart of NodeBox's paths and shapes.</p>
<p>Beziers come in two basic flavors. A quadratic bezier spans part of a parabola and consists of a start point, an end point, and a control point which determines its slant, depth, and direction. A cubic bezier has two control points and can create a wide variety of curves. Higher order beziers have more control points, but in practice you can make most any curve by joining multiple cubic beziers together into splines (paths).</p>
<p>NodeBox is built entirely out of cubic beziers. But, ironically, it comes with only one node to make these curves directly, and that will only make quadratic curves. Instead of providing a control point, the <strong>quad_curve</strong> node asks for a T value and a Distance. T determines the skew: less that 50 skews one way, more than 50 skews the other. The distance determines how far the curve extends from the base line connecting the start and end points; a positive value dips to the right, a negative value to the left.</p>
<p>When you use a quad_curve node, NodeBox quietly converts it into an equivalent cubic bezier. If you do need to make your own cubic beziers you can use the curve node from my Node Library (see previous post).</p>
<p>This is all well and good. But occasionally you may want to draw a quadratic bezier using a given control point. The quad_curve node requires T and Distance which are not easy to determine from the control point. My curve node requires two control points, which are related to but different from that one control point. Or you may be in the reverse situation. You have drawn a curve using the quad_curve node and need to find the control point. What to do?</p>
<p>The attached network contains two nodes that will bridge this gap. Quad_control draws a quadratic bezier based on three points: start, control, and end. (If you ever need to derive the T and Distance values you can find them inside this node.) Find_control does the reverse; given a quad curve it returns the control point governing that curve.</p>
<p>The attached demo lets you set start and end points along with the T and Distance values to produce a quad curve (in black with two small dots showing the cubic control points produced when NodeBox converts it). The network feeds this curve into the find_control node to discover the control point (and plot it as a larger red dot). That control point, along with the start and end points, are then fed into a quad_control node to reproduce the quadratic bezier, shown in translucent red atop the original black curve.</p>
<p>You can fiddle with the values at the top to verify that quad_control always matches the output of Nodebox's quad_curve node.</p>
<p>If you peer inside quad_control you will see another handy node, point_on_line, that returns the point corresponding to any position on the line extending through a given line segment. I am, frankly, not sure whether or not to include any of these three nodes in the next release of my library. All three are handy, but only in unusual situations. If anyone has an opinion, please let me know.</p>
<p>John</p>
<p>P.S. I have long used the quad_curve node without understanding the actual geometric meaning of the T and Distance values. Having finally been forced to create these new nodes, I finally understand...</p>
<p>Project the control point onto a line extending through the base line of the curve (the line segment connecting the start and end points). By "project" I mean draw a line from the control point in a direction perpendicular to the base line and find where it intersects. The T value is the signed distance percentage from the start point to this projected point divided by the length of the base line. Distance is the signed distance from the control point to this projected point (positive if the control lies to the right of the baseline when facing from start to end, negative otherwise).</p>
<p>When you raise and lower the T value you are sliding the control point back and forth along a line parallel to the base line. Distance adjusts the distance between the two lines. The T and Distance values are intuitive to use, but their exact geometric relationship to the underlying curve was hard to work out.</p></div>johntag:support.nodebox.net,2012-11-01:Comment/492418872021-06-20T07:19:43Z2021-06-20T07:19:43ZQuad Curves<div><p>Attached is a very minor update to quad_control node. It works exactly as before, but corrects a bug which occurs when drawing curves exactly east to west.</p></div>john