# Dev:Linking to Blines - Equations

WARNING: This text is under HEAVY development. Please be patient.

## Main equations

• $\displaystyle (x_1,y_1), (x_2,y_2)$ - vertices, defining bline segment
• $\displaystyle (x,y)$ - current bline point
• $\displaystyle (x_{t1},y_{t1}), (x_{t2},y_{t2})$ - tangent points defining bline segment
• $\displaystyle (x_{t},y_{t})$ - tangent coordinates for current bline point
• u - Amount of current segment, [0,1]
• $\displaystyle (x,y) = (1-u)^3 (x_1,y_1) + 3 u(1-u)^2 (x_{t1},y_{t1}) + 3 u^2 (1-u) (x_{t2},y_{t2}) + u^3 (x_2,y_2)$ - bline point

#### Relative tangents

O'kay, I know, the $\displaystyle (x_{t1},y_{t1}), (x_{t2},y_{t2})$ defining absolute position of tangents, but in synfig we have their coordinates relative to vertex. Moreover, coordinates of yellow tangent are inverted.

Let's say:

• $\displaystyle (\Delta x_{t1},\Delta y_{t1})$ and $\displaystyle (\Delta x_{t2},\Delta y_{t2})$ - relative coordinates of tangents

Then:

• $\displaystyle (x_{t1},y_{t1}) = (x,y) + (\Delta x^{t1},\Delta y^{t1})$
• $\displaystyle (x_{t2},y_{t2}) = (x,y) - (\Delta x^{t2},\Delta y^{t2})$

Make substitution:

• Bline point: $\displaystyle (x,y) = (1-(3u^2-2u^3)) (x_1,y_1) + 3u(1-u)^2 (\Delta x_{t1},\Delta y_{t1}) - 3u^2(1-u) (\Delta x_{t2}, \Delta y_{t2}) + (3u^2-2u^3) (x_2,y_2)$

### Tangent coordinates

<dooglus> look at etl/_calculus.h
<dooglus> class derivative<hermite<T> >
<dooglus>   T a = func, b = func, c = func, d = func;
<dooglus>   typename hermite<T>::argument_type y(1-x);
<dooglus>   return ((b-a)*y*y + (c-b)*x*y*2 + (d-c)*x*x) * 3;

Can't understand, why *3? --Zelgadis 05:31, 8 March 2008 (EST)

For x:

• $\displaystyle \Delta x_{t} = (1-u)^2 \Delta x_{t1} + 2u(1-u) ((x_2 - \Delta x_{t2}) - (x_1 + \Delta x_{t1})) + u^2 \Delta x_{t2} =$ $\displaystyle = -2u(1-u)x_1 + (1-u)(1-3u)\Delta x_{t1} + (3u^2 - 2u)\Delta x_{t2} + 2u(1-u)x_2$

### Checking formulas

Well, I really wasn't sure if this formulas correct, that's why I did a little research and made some graphics and calculations. They are available here.

As result I got some final corrections to formulas (for x only):

• Bline point: $\displaystyle x = (1-(3u^2-2u^3)) x_1 + u(1-u)^2 \Delta x_{t1} - u^2(1-u) \Delta x_{t2} + (3u^2-2u^3) x_2$
• Bline tangent: $\displaystyle \Delta x_{t} = -6u(1-u)x_1 + (1-u)(1-3u)\Delta x_{t1} + (3u^2 - 2u)\Delta x_{t2} + 6u(1-u)x_2$

O'kay I understand what equations above could be wrong. But no one will argue what equations for bline point and tangents generally have form:

• $\displaystyle (x,y) = c_1(u) (x_1,y_1) + c_2(u) (x_{t1},y_{t1}) + c_3(u) (x_{t2},y_{t2}) + c_4(u) (x_2,y_2)$
• $\displaystyle (\Delta x_{t},\Delta y_{t}) = c_{t1}(u) (x_1,y_1) + c_{t2}(u) (\Delta x_{t1}, \Delta y_{t1}) + c_{t3} (\Delta x_{t2}, \Delta y_{t2}) + c_{t4}(u) (x_2,y_2)$

where $\displaystyle c_i(u)$ - some function from 'u' argument

So I will use this notation further.

For now we assume what:

• $\displaystyle c_1(u)=1-(3u^2-2u^3)$
• $\displaystyle c_2(u)=u(1-u)^2$
• $\displaystyle c_3(u)=- u^2(1-u)$
• $\displaystyle c_4(u)=3u^2-2u^3$
• $\displaystyle c_{t1}(u)=-6u(1-u)$
• $\displaystyle c_{t2}(u)=(1-u)(1-3u)$
• $\displaystyle c_{t3}(u)=3u^2 - 2u$
• $\displaystyle c_{t4}(u)=6u(1-u)$

## General model

So we need to determine position of vertex engaged in loop if she attachet ot some position of other bline.

NOTICE: We need such recalculation only for cases when in loop included only 1 segment from each bline. Like this:  We don't need to do recalculation for cases like those: So, we have N blines (N bline segments) engaged in a loop.

To determine the x coordinate of vertex what finalizes the loop we need to solve matrix equation having form

$\displaystyle (A - B)*\alpha = \beta$

NOTE: Solution formulas for y coordinate will be the same.
• $\displaystyle A, B$ - matrices 4N x 4N
• $\displaystyle \alpha, \beta$ - 4N column vectors
• $\displaystyle \alpha$ - is unknown quantity

Each line in matrix or column vector corresponding to particular vertex or it's tangent.

After solving system we will find column vector $\displaystyle \alpha$ which have form: $\displaystyle (x^{}_1,\Delta x^{}_{t1},\Delta x^{}_{t2},x^{}_2, x^{}_1,\Delta x^{}_{t1},\Delta x^{}_{t2},x^{}_2, ... ,x^{[N]}_1,\Delta x^{[N]}_{t1},\Delta x^{[N]}_{t2},x^{[N]}_2)$ where $\displaystyle x^{[i]}_j$ - point j of bline [i] and $\displaystyle \Delta x^{[i]}_tj$ tangent of point j of bline i. From this vector we could retrieve x coordinate of desired vertex or tangent.

So:

• the first line of each matrix or column vector corresponding to first vertex of first bline,
• second line - corresponding to tangent of first vertex of first bline,
• third line - to tangent of second vertex of first bline,
• fourth - second vertex of first bline,
• fifth - first vertex of second bline,
• sixth - tangent of first vertex of second bline,
• ...and so on...
NOTE: Talking about bline we are talking about single (!) segment of bline which engaged in loop. That's why I not specify which tangent (yellow or red) we using - it's always tangent for current segment.

Column vector $\displaystyle \beta$ have following structure. If position of vertex/tangent corresponding to vector element is "static" (i.e. it's not linked any other bline segment of the loop) then vector element is $\displaystyle \beta_i$ where $\displaystyle \beta_i$ is current x coordinate of this vector/tangent. If vertex/tangent is not "static" then corresponding vector element is zero.

NOTE: When we calling vertex position "static" it's not means what vertex not linked to anything. The vertex could be linked to bline and still considered as "static" if that bline is not engaged into loop what we processing.

B matrix is zero-filled 4Nx4N matrix which we modifying in folowing way (rows and columns in matrix are numbered from 1):

• if first vertex of bline [i] is linked to bline [j] then element at ( (i-1)*4+1 , (i-1)*4+1 ) = 1
• if tangent of first vertex of bline [i] is linked to bline [j] then element at ( (i-1)*4+2 , (i-1)*4+2 ) = 1
• if tangent of second vertex of bline [i] is linked to bline [j] then element at ( (i-1)*4+3 , (i-1)*4+3 ) = 1
• if second vertex of bline [i] is linked to bline [j] then element at ( (i-1)*4+4 , (i-1)*4+4 ) = 1

A matrix is modified E matrix. $\displaystyle E = \begin{pmatrix} 1 & 0 & 0& ... & 0 \\ 0 & 1 & 0& ... & 0 \\ 0 & 0 & 1 & ... & 0 \\...&...&...&...&... \\ 0 & 0 & 0& ... & 1 \\ \end{pmatrix}$

It is modified in such way:

• if first vertex of bline [i] is linked to bline [j] then element at position ( (i-1)*4+1, (i-1)*4+1 ) replaced with zero and elements in line (i-1)*4+1 at positions (j-1)*4+1, (j-1)*4+2, (j-1)*4+3, (j-1)*4+4 are replaced with $\displaystyle c^{[j]}_1(u^{[i]}_1), c^{[j]}_2(u^{[i]}_1), c^{[j]}_3(u^{[i]}_1), c^{[j]}_4(u^{[i]}_1)$
• if tangent of first vertex of bline [i] is linked to bline [j] then element at position ( (i-1)*4+2, (i-1)*4+2 ) replaced with zero and elements in line (i-1)*4+2 at positions (j-1)*4+1, (j-1)*4+2, (j-1)*4+3, (j-1)*4+4 are replaced with $\displaystyle c^{[j]}_{t1}(u^{[i]}_1), c^{[j]}_{t2}(u^{[i]}_1), c^{[j]}_{t3}(u^{[i]}_1), c^{[j]}_{t4}(u^{[i]}_1)$
• if tangent of second vertex of bline [i] is linked to bline [j] then element at position ( (i-1)*4+3, (i-1)*4+3 ) replaced with zero and elements in line (i-1)*4+3 at positions (j-1)*4+1, (j-1)*4+2, (j-1)*4+3, (j-1)*4+4 are replaced with $\displaystyle c^{[j]}_{t1}(u^{[i]}_2), c^{[j]}_{t2}(u^{[i]}_2), c^{[j]}_{t3}(u^{[i]}_2), c^{[j]}_{t4}(u^{[i]}_2)$
• if second vertex of bline [i] is linked to bline [j] then element at position ( (i-1)*4+4, (i-1)*4+4 ) replaced with zero and elements in line (i-1)*4+4 at positions (j-1)*4+1, (j-1)*4+2, (j-1)*4+3, (j-1)*4+4 are replaced with $\displaystyle c^{[j]}_1(u^{[i]}_2), c^{[j]}_2(u^{[i]}_2), c^{[j]}_3(u^{[i]}_2), c^{[j]}_4(u^{[i]}_2)$

where $\displaystyle u^{[i]}_k$ is a position of vertex/tangent k of bline [i] on bline which it's linked to.

## Examples

Ok, I sure you guys are wondering how is this work. (I personally wondering IF this works or not :D). Let's view some examples.

### One spline

Case 1: Given Bline . Vertex 2 of bline  linked to bline . For x coordinates we have following system of equations:

• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle x^{}_{2} = c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + c^{}_3(u^{}_2) \Delta x^{}_{t2} + c^{}_4(u^{}_2) x^{}_2$ - because $\displaystyle x^{}_2$ belongs to bline 

$\displaystyle X^{[i]}_j$ is a constant value of $\displaystyle x^{[i]}_j$ and $\displaystyle \Delta X^{[i]}_{j}$ is a constant value of $\displaystyle \Delta x^{[i]}_{j}$ . $\displaystyle u^{}_2$ is a position of vertex 2 of bline  on the bline which it's linked to (i.e. bline ).

Transform:

• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + c^{}_3(u^{}_2) \Delta x^{}_{t2} + (c^{}_4(u^{}_2) - 1) x^{}_2 = 0$

So, we must solve the 4x4 matrix equation: $\displaystyle ( \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 &0 \\ 0 & 0 & 1 & 0 \\ c^{}_1(u^{}_2) & c^{}_2(u^{}_2) & c^{}_3(u^{}_2) & c^{}_4(u^{}_2) \end{pmatrix} -\begin{pmatrix} 0&0&0&0 \\ 0&0&0&0\\ 0&0&0&0 \\ 0&0&0&1\end{pmatrix}) \cdot \begin{pmatrix} x^{}_1 \\ \Delta x^{}_{t1} \\ \Delta x^{}_{t2} \\ x^{}_{2} \end{pmatrix} = \begin{pmatrix} X^{}_1 \\ \Delta X^{}_{t1} \\ \Delta X^{}_{t2} \\ 0 \end{pmatrix}$

dooglus, can you check how this formula works in code? Something like:

• Create bline with 2 points - 1 and 2
• Select point 2, right click on bline -> "Link to bline"
• Place (not link!) point 2 at the position calculated by this formula. If we'll have point 2 on bline after that then it's ok for now, if not - something wrong.

Case 2: Given Bline . Vertex 2 of bline  linked to bline  with it's tangent. For x coordinates we have following system of equations:

• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = c^{}_{t1}(u^{}_2) x^{}_1 + c^{}_{t2}(u^{}_2) \Delta x^{}_{t1} + c^{}_{t3}(u^{}_2) \Delta x^{}_{t2} + c^{}_{t4}(u^{}_2) x^{}_2$ - because tangent $\displaystyle \Delta x^{}_{t2}$ linked to bline 
• $\displaystyle x^{}_{2} = c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + c^{}_3(u^{}_2) \Delta x^{}_{t2} + c^{}_4(u^{}_2) x^{}_2$ - because $\displaystyle x^{}_2$ belongs to bline 

$\displaystyle X^{[i]}_j$ is a constant value of $\displaystyle x^{[i]}_j$ and $\displaystyle \Delta X^{[i]}_{j}$ is a constant value of $\displaystyle \Delta x^{[i]}_{j}$ . $\displaystyle u^{}_2$ is a position of vertex 2 of bline  on the bline which it's linked to (i.e. bline ).

Transform:

• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = c^{}_{t1}(u^{}_2) x^{}_1 + c^{}_{t2}(u^{}_2) \Delta x^{}_{t1} + c^{}_{t3}(u^{}_2) \Delta x^{}_{t2} + (c^{}_{t4}(u^{}_2) - 1) x^{}_2$
• $\displaystyle c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + (c^{}_3(u^{}_2) -1) \Delta x^{}_{t2} + c^{}_4(u^{}_2) x^{}_2 = 0$

So, we must solve the 4x4 matrix equation: $\displaystyle ( \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 &0 \\ c^{}_{t1}(u^{}_2) & c^{}_{t2}(u^{}_2) & c^{}_{t3}(u^{}_2) & c^{}_{t4}(u^{}_2) \\ c^{}_1(u^{}_2) & c^{}_2(u^{}_2) & c^{}_3(u^{}_2) & c^{}_4(u^{}_2) \end{pmatrix} -\begin{pmatrix} 0&0&0&0 \\ 0&0&0&0\\ 0&0&1&0 \\ 0&0&0&1\end{pmatrix}) \cdot \begin{pmatrix} x^{}_1 \\ \Delta x^{}_{t1} \\ \Delta x^{}_{t2} \\ x^{}_{2} \end{pmatrix} = \begin{pmatrix} X^{}_1 \\ \Delta X^{}_{t1} \\ 0 \\ 0 \end{pmatrix}$

### Two splines

Case 1: Given Blines  and .

Vertex 1 of bline  linked to bline .

Vertex 2 of bline  linked to bline . For x coordinates we have following system of equations:

• $\displaystyle x^{}_1 = c^{}_1(u^{}_1) x^{}_1 + c^{}_2(u^{}_1) \Delta x^{}_{t1} + c^{}_3(u^{}_1) \Delta x^{}_{t2} + c^{}_4(u^{}_1) x^{}_2$ - because $\displaystyle x^{}_1$ belongs to bline 
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle x^{}_2 = X^{}_2$
• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle x^{}_{2} = c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + c^{}_3(u^{}_2) \Delta x^{}_{t2} + c^{}_4(u^{}_2) x^{}_2$ - because $\displaystyle x^{}_2$ belongs to bline 

$\displaystyle X^{[i]}_j$ is a constant value of $\displaystyle x^{[i]}_j$ and $\displaystyle \Delta X^{[i]}_{j}$ is a constant value of $\displaystyle \Delta x^{[i]}_{j}$ . $\displaystyle u^{}_2$ is a position of vertex 2 of bline  on the bline which it's linked to (i.e. bline ). $\displaystyle u^{}_1$ is a position of vertex 1 of bline  on the bline which it's linked to (i.e. bline ).

Transform:

• $\displaystyle -x^{}_1 + c^{}_1(u^{}_1) x^{}_1 + c^{}_2(u^{}_1) \Delta x^{}_{t1} + c^{}_3(u^{}_1) \Delta x^{}_{t2} + c^{}_4(u^{}_1) x^{}_2 = 0$ - because $\displaystyle x^{}_1$ belongs to bline 
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle x^{}_2 = X^{}_2$
• $\displaystyle x^{}_1 = X^{}_1$
• $\displaystyle \Delta x^{}_{t1} = \Delta X^{}_{t1}$
• $\displaystyle \Delta x^{}_{t2} = \Delta X^{}_{t2}$
• $\displaystyle c^{}_1(u^{}_2) x^{}_1 + c^{}_2(u^{}_2) \Delta x^{}_{t1} + c^{}_3(u^{}_2) \Delta x^{}_{t2} + c^{}_4(u^{}_2) x^{}_2 - x^{}_{2} = 0$ - because $\displaystyle x^{}_2$ belongs to bline 

So, we must solve the 8x8 matrix equation: $\displaystyle ( \begin{pmatrix} 0 & 0 & 0 & 0 & c^{}_1(u^{}_1) & c^{}_2(u^{}_1) & c^{}_3(u^{}_1) & c^{}_4(u^{}_1) \\ 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\ c^{}_1(u^{}_2) & c^{}_2(u^{}_2) & c^{}_3(u^{}_2) & c^{}_4(u^{}_2) & 0 & 0 & 0 & 0 \end{pmatrix} - \begin{pmatrix} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 1 \end{pmatrix}) \cdot \begin{pmatrix} x^{}_1 \\ \Delta x^{}_{t1} \\ \Delta x^{}_{t2} \\ x^{}_{2} \\ x^{}_1 \\ \Delta x^{}_{t1} \\ \Delta x^{}_{t2} \\ x^{}_{2} \end{pmatrix} = \begin{pmatrix} 0 \\ \Delta X^{}_{t1} \\ \Delta X^{}_{t2} \\ X^{}_{2} \\ X^{}_1 \\ \Delta X^{}_{t1} \\ \Delta X^{}_{t2} \\ 0 \end{pmatrix}$

## Conclusion

Maybe formulas are not correct, but I think you've got the idea: For N Bline segments in loop we have 4N equations. If vertex is linked then we got appropriate equation, depending on what is linked. If vertex is not linked - just assigning constants.