Numerical Integration: 2nd and 4th order Runge-Kutta schemes

Second-order Runge-Kutta scheme (RK2)


There are several variations of the second order Runge-Kutta schemes of numerical integration.

They are predictor-correction methods that it involves 2 stages. A step that predicts the value at the next time step (using the explicit Euler's method as predictor) and another step that applies a correction to the estimated value.

Put simply, it improves upon the Euler method by adding a midpoint in the step which increases the accuracy by one order.

The local error at each step is order \(O(h^3)\) giving a global error of order \(O(h^2)\).

Heun method


The Heun's method is expressed as follows:
\[y_{n+1} = y_n + \frac{h}{2}[f(t_n, y_n) + f(t_{n+1},y_{n+1})]\]
where:
  • \(y_n\) is the approximation of solution at time \(t_n\)
  • \(y_{n+1}\) is the approximation of solution at time \(t_{n+1}\)
  • \(h\) is the step size
  • \(f(t,y)\) is the function that defines the derivative of \(y\) with repsect to \(t\), ie \(\dot y = f(t, y)\)

The two steps are as follows:
  1. In the first step, calculate the estimate of slope of solution at beginning of time step. Then calculate a provisional value of the solution at the end of the time step, by using explicit Euler.
  2. Calculate the estimate of slope of solution at provisional value of the solution. Then use this estimate to calculate a new improved estimate of solution at end of time step.
To make the above single line formula easier to understand, the method may be expressed as follows:
\[y_{n+1}=h\frac{1}{2}\left[k_1+k_2  \right]\]
where:
\begin{aligned} k_{1}&=\ f(t_{n},y_{n}),\\ k_{2}&=\ f(t_{n+1},hf(t_{n+1}, y_{n+1})) = f(t_{n+1},hk_1) \end{aligned}

Written in pseudo computer code, this becomes:
\[\begin{aligned}p_1&=x_n\\v_1&=v_n\\a_1&=a(p_1,v_1)\\p_2&=p_1+v_1\cdot\Delta t\\v_2&=v_1+a_1\cdot \Delta t\\a_2&=a(p_2,v_2)\\x_{n+1}&=x_n+\frac{v_1+v_2}{2}\cdot \Delta t\\v_{n+1}&=v_n+\frac{a_1+a_2}{2}\cdot \Delta t \end{aligned} \]

Comparison to basic Euler


In essence, where as the basic forward Euler method takes the slope of the solution curve at the beginning of the timestep \((k_1)\) and extrapolates the solution at the end of the time step \(t_n\).

The Heun method estimates a better slope by taking the average of the slope at the beginning of the time step and the end of the timestep. However we do not know the slope at \(t_{n+1}\). So we estimate it using basic Euler.


Midpoint method


Explicit Midpoint method


The explicit midpoint method, sometimes known as the modified Euler method is:
\[y_{n+1}=y_n + hf(t_n+h/2, y_n+hf(t_n,y_n))\]
or,
\[y_{n+1}=y_n+hk_2\]
where:
\[\begin{aligned} k_{1}&=f(t_{n},y_{n}), \\  k_{2}&=f(t_{n+\frac{1}{2}},y_{n+\frac{1}{2}}) = f(t_{n+\frac{1}{2}},hk_1)     \end{aligned}\]

Implicit Midpoint method

\[y_{n+1}=y_n + hf(t_{n+\frac{h}{2}}, \frac{1}{2}(y_n+y_{n+1}))\]



Fourth-order Runge Kutta method (RK4)



The "classic Runge-Kutta method" or simply the "Runge-Kutta method" refers to the following.

The formula is given by

\[\begin{aligned} y_{n+1}&=y_{n}+{\frac {h}{6}}\left(k_{1}+2k_{2}+2k_{3}+k_{4}\right),\\ t_{n+1}&=t_{n}+h\\ \end{aligned} \]
for n = 0, 1, 2, 3, ..., using:
\[{\displaystyle {\begin{aligned}k_{1}&=\ f(t_{n},y_{n}),\\k_{2}&=\ f\!\left(t_{n}+{\frac {h}{2}},y_{n}+h{\frac {k_{1}}{2}}\right),\\k_{3}&=\ f\!\left(t_{n}+{\frac {h}{2}},y_{n}+h{\frac {k_{2}}{2}}\right),\\k_{4}&=\ f\!\left(t_{n}+h,y_{n}+hk_{3}\right)\end{aligned}}}\]


Here \(y_{n+1}\) is the RK4 approximation of \(y(t_{n+1})\), and the next value \((y_{n+1})\) is determined by the present value \((y_{n})\) plus the weighted average of four increments, where each increment is the product of the size of the interval, h, and an estimated slope specified by function f on the right-hand side of the differential equation.




Useful References


Numerical Integration: Verlet integration


Position Verlet


 To a close approximation (actually performing a Taylor Series Expansion about  x(t±Δt) ), that might look like this:

\[x(t+\Delta t) = x(t) + v(t)\Delta t + \frac{1}{2}a(t)\Delta t^2 + \frac{1}{6}b(t) \Delta t^3 + \mathcal{O}(\Delta t^4)\]


This means that if we need to find the next  x, we need the current  x ,  v ,  a , etc. However, because few people calculate the jerk term, our error is typically  \(\mathcal{O}(\Delta t^3)\) . That said, we can calculate  x  with less knowledge and higher accuracy if we play a trick! Let's say we want to calculate  x  of the previous timestep. Again, to a close approximation, that might look like this:

\[x(t-\Delta t) = x(t) - v(t)\Delta t + \frac{1}{2}a(t)\Delta t^2 - \frac{1}{6}b(t) \Delta t^3 + \mathcal{O}(\Delta t^4)\]


Now, we have two equations to solve for two different timesteps in x, one of which we already have. If we add the two equations together and solve for x(t+Δt), we find that

\[x(t+ \Delta t) = 2x(t) - x(t-\Delta t) + a(t)\Delta t^2 + \mathcal{O}(\Delta t^4)\]
So, this means we can find our next  x simply by knowing our current  x , the  x  before that, and the acceleration! No velocity necessary! In addition, this drops the error to \(\mathcal{O}(\Delta t^4)\) , which is great! Here is what it looks like in code:


  
  XXXXXXXXXXXXXXXXX





Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, \(\frac{1}{2}mv^2\). In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to  O(Δt2)  accuracy by using the Stormer-Verlet method. We have the equations for  x(t+Δt)  and  x(t−Δt) above, so let's start there. If we subtract the latter from the former, we get the following:
\[x(t+\Delta t) - x(t - \Delta t) = 2v(t)\Delta t + \frac{1}{3}b(t)\Delta t^3\]
When we solve for  v(t), we get
\[\begin{align} v(t) &= \frac{x(t+\Delta t) - x(t-\Delta t)}{2\Delta t} + \frac{b(t) \Delta t^3}{3 \Delta t} \\  v(t) &= \frac{x(t+\Delta t) - x(t-\Delta t)}{2\Delta t} + \mathcal{O}(\Delta t^2). \end{align}\]

Note that the 2 in the denominator makes sense because we are going over 2 timesteps. It's essentially solving  \(v=\frac{Δx}{Δt}\). In addition, we can calculate the velocity of the next timestep like so
\[v(t+\Delta t) = \frac{x(t+\Delta t) - x(t)}{\Delta t} + \mathcal{O}(\Delta t)\]


Velocity Verlet

Now, let's say we actually need the velocity to calculate out next timestep. Well, in this case, we simply cannot use the above approximation and instead need to use the Velocity Verlet algorithm.

In some ways, this algorithm is even simpler than above. We can calculate everything like

\[\begin{align} x(t+\Delta t) &=x(t) + v(t)\Delta t + \frac{1}{2}a(t)\Delta t^2 \\ a(t+\Delta t) &= f(x(t+\Delta t)) \\ v(t+\Delta t) &= v(t) + \frac{1}{2}(a(t) + a(t+\Delta t))\Delta t \end{align}\]
which is literally the kinematic equation above, solving for  x ,  v , and  a  every timestep. You can also split up the equations like so
\[\begin{align} v(t+\frac{1}{2}\Delta t) &= v(t) + \frac{1}{2}a(t)\Delta t \\ x(t+\Delta t) &=x(t) + v(t+\frac{1}{2}\Delta t)\Delta t \\ a(t+\Delta t) &= f(x(t+\Delta t)) \\ v(t+\Delta t) &= v(t+\frac{1}{2}\Delta t) + \frac{1}{2}a(t+\Delta t)\Delta t \end{align}\]

Here is the velocity Verlet method in code:



  XXXXXXXXXXXXXXXXXX


Even though this method is more widely used than the simple Verlet method mentioned above, it unfortunately has an error term of \(\mathcal{O}(\Delta t^2)\), which is two orders of magnitude worse. 


Verlet Leapfrog Algorithm

Takes the form

\[\begin{aligned}x_{i+1}&=x_{i}+v_{i}\Delta t+{\frac {1}{2}}a_{i}\Delta t^{2},\\v_{i+1}&=v_{i}+{\frac {1}{2}}(a_{i}+a_{i+1})\Delta t.\end{aligned}\]


Useful References

https://en.wikipedia.org/wiki/Verlet_integration

https://julesjacobs.com/2019/03/15/leapfrog-verlet.html

https://pikuma.com/blog/verlet-integration-2d-cloth-physics-simulation


https://www.algorithm-archive.org/contents/verlet_integration/verlet_integration.html

Collision Response with Rotation

 Linear velociites at point p before the collision

\[ \begin{aligned} v_{ap}=v_a+(\omega_a \times r_{ap})\\ v_{bp}=v_b+(\omega_b \times r_{bp}) \end{aligned}\]

After collision:

\[ \begin{aligned} v_{ap}'=v_a'+(\omega_a' \times r_{ap})\\ v_{bp}'=v_b'+(\omega_b' \times r_{bp}) \end{aligned}\]


Recall that relative velocity when there is no rotation:

\[ \begin{aligned}  v_{ab}'=v_a-v_b \\  v_{ab}'=v_{a}'-v_{b}'  \end{aligned}\]

and with rotation:

\[ \begin{aligned}   v_{abp}= v_{ap}- v_{bp}\\  v_{abp}'=v_{ap}'- v_{bp}'     \end{aligned}\]

We can also say that:

\[ \begin{aligned}  v_{ab}'\cdot n=-e(v_{ab}\cdot n)\\ v_{ab}'\cdot t=-f(v_{ab}\cdot t)    \end{aligned}\]


In a world of no ration, impulse method describes the change in their linear velociteis by the impulse J, scaled by the inverse of their corresponding masses, m(a) and m(b). Note that below asumes J is pointing from A to B.

\[ \begin{aligned}    v_{a}'= v_{a}- \frac{J}{m_a}\\ v_{b}'= v_{b}+ \frac{J}{m_b}     \end{aligned}\]

The rotational equivalent is:

\[ \begin{aligned}    \omega_a'=\omega_a-\left( r_{ap}\times \frac{J}{I_a} \right) \\ \omega_b'=\omega_b+\left( r_{bp}\times \frac{J}{I_b} \right)      \end{aligned}\]

Impulse can be decomposed into components along the collision normal and tangent.

\[J=j_n\cdot n + j_t\cdot t\]

We can say that:

\[\omega_a' = \omega_a -\left( r_{ap}\times \frac{j_n\cdot n + j_t \cdot t}{I_a} \right)=\omega_a-\frac{j_n}{I_a}(r_{ap}\times n)-\frac{j_t}{I_a}(r_{ap}\times t)\]

Simiarly,

\[\omega_b' = \omega_b +\left( r_{bp}\times \frac{j_n\cdot n + j_t \cdot t}{I_b} \right)=\omega_b+\frac{j_n}{I_b}(r_{bp}\times n)+\frac{j_t}{I_b}(r_{bp}\times t)\]

Note that the equation has been expanded to describe the change in angular velocities, casued by the normal and tangent compoenents of the impulse.

The corresponding equations describing the linear velocity changes are:

\[\begin{aligned} v_a'=v_a-\frac{J}{m_a}=v_a-\frac{j_n}{m_a}\cdot n-\frac{j_t}{m_a}\cdot t \\ v_b'=v_b+\frac{J}{m_b}=v_b+\frac{j_n}{m_b}\cdot n+\frac{j_t}{m_b}\cdot t  \end{aligned}\]

From the above, by substitution we get:

\[\begin{aligned}v_{ap}'=\left( v_{a}-\frac{j_n}{m_a}n-\frac{j_t}{m_a}t \right)+\left( \omega_a-\frac{j_n}{I_a}(r_{ap}\times n)-\frac{j_t}{I_a}(r_{ap \times t}) \right)\times r_{ap}\\ v_{bp}'=\left( v_{b}+\frac{j_n}{m_b}n+\frac{j_t}{m_b}t \right)+\left( \omega_b+\frac{j_n}{I_b}(r_{bp}\times n)+\frac{j_t}{I_b}(r_{bp \times t}) \right)\times r_{bp}\end{aligned}    \]

Ignore the contribution from the angular velocity tangent component as immaterial.

Then perform the dot product on both sides by n.

\[\begin{aligned} v_{ap}'\cdot n=\left[ \left( v_{a}-\frac{j_n}{m_a}n-\frac{j_t}{m_a}t \right)+\left( \omega_a-\frac{j_n}{I_a}(r_{ap}\times n) \right)\times r_{ap}\ \right]\cdot n     \\ v_{bp}'\cdot n= \left[\left( v_{b}+\frac{j_n}{m_b}n+\frac{j_t}{m_b}t \right)+\left( \omega_b+\frac{j_n}{I_b}(r_{bp}\times n) \right)\times r_{bp}   \right]\cdot n      \end{aligned}\]

Which can be expanded to:

\[\begin{aligned} v_{ap}'\cdot n=(v_a\cdot n)-\frac{j_n}{m_a}+(\omega_a\times r_{ap})\cdot n-\frac{j_n}{I_a} \left[ \left( r_{ap}\times n\right) \times r_{ap} \right]\cdot n \\ v_{bp}'\cdot n=(v_b\cdot n)+\frac{j_n}{m_b}+(\omega_b\times r_{bp})\cdot n+\frac{j_n}{I_b} \left[ \left( r_{bp}\times n\right) \times r_{bp} \right]\cdot n   \end{aligned}\]

Let's look at the last part of this expression, and to make the analysis simpler, call \(D= r_{ap}\times n\). Utilizing the scalar triple product identity \((D\times E)\cdot F = D\cdot (E\times F)\), we can say that:

\[\left[ (r_{ap}\times n)\times r_{ap} \right]\cdot n =(D \times r_{ap})\cdot n=D\cdot(r_{ap}\times n)\]

which means that;

\[\left[ (r_{ap}\times n)\times r_{ap} \right]\cdot n =(r_{ap}\times n)\cdot (r_{ap}\times n)=\left\| r_{ap}\times n\right\|^2\]

so,

\[\begin{equation} \begin{split} v_{ap}'\cdot n=(v_a\cdot n)-\frac{j_n}{m_a}+(\omega_a\times r_{ap})\cdot n-\frac{j_n}{I_a}\left\| r_{ap}\times n \right\|^2\\ =(v_a+\omega_a\times r_{ap})\cdot n-\frac{j_n}{m_a}-\frac{j_n}{I_a}\left\| r_{ap}\times n \right\|^2 \end{split} \end{equation}\]

but recall that:

\[v_{ap}=v_a+(\omega_a\times r_{ap})\]

so

\[v_{ap}'\cdot n=v_{ap}\cdot n -\frac{j_n}{m_a}-\frac{j_n}{I_a}\left\| r_{ap}\times n \right\|^2\]

similarly,

\[v_{bp}'\cdot n=v_{bp}\cdot n +\frac{j_n}{m_b}+\frac{j_n}{I_b}\left\| r_{bp}\times n \right\|^2\]

Subtracting the above two equations:

\[(v_{ap}'-v_{bp}')\cdot n=(v_{ap}-v_{bp})\cdot n-j_n\left( \frac{1}{m_a}+\frac{1}{m_b} \right)-\frac{\left\| r_{ap}\times n \right\|^2}{I_a}+\frac{\left\| r_{bp}\times n \right\|^2}{I_b}\]


Since \(v_{abp}'=-e(v_{abp}\cdot n)\):

\[-e(v_{abp}\cdot n)=v_{abp}\cdot n-j_n\left[\frac{1}{m_a}+\frac{1}{m_b}+\frac{\left\| r_{ap}\times n \right\|^2}{I_a}+\frac{\left\| r_{bp}\times n \right\|^2}{I_b}  \right]\]

Collecting terms we get:

\[j_n=\frac{(1+e)(v_{abp}\cdot n)}{\left( \frac{1}{m_a}+ \frac{1}{m_b}+\frac{\left\| r_{ap}\times n \right\|^2}{I_a}  ++\frac{\left\| r_{bp}\times n \right\|^2}{I_b}  \right)}\]

Collision Response without Rotation

\[\begin{aligned}v'_a=v_a+\Delta v_a\\ v'_b=v_b+\Delta v_b \end{aligned}\]


\[\begin{aligned}v'_a=v_a-\frac{J}{m_a}\\  v'_b=v_b+\frac{J}{m_b} \end{aligned}\]

\[\begin{aligned}v'_a=v_a-\frac{J}{m_a}=v_a-(j_n\cdot n+j_t\cdot t)\frac{1}{m_a} \\ v'_b=v_b+\frac{J}{m_b} =v_b+(j_n\cdot n+j_t\cdot t)\frac{1}{m_b} \end{aligned}\]



\[(v_a'-v_b') = (v_a-v_b)+J(\frac{1}{m_a}+\frac{1}{m_b})\]

which can be expressed as:

\[v_{ab}' = v_{ab}+J(\frac{1}{m_a}+\frac{1}{m_b})\]

Dcompose velocity

\[v=v\cdot n+v\cdot t\]


Decomposition J

\[J=j_n\cdot n+j_t\cdot t\]



 \[v'_a\cdot n=\left( v_a-\left[ \frac{j_n}{m_a}n+\frac{j_t}{m_a}t \right]  \right)\cdot n=v_a\cdot n-\frac{j_n}{m_a}\]

\[v'_b\cdot n=\left( v_b+\left[ \frac{j_n}{m_b}n+\frac{j_t}{m_b}t \right]  \right)\cdot n=v_b\cdot n+\frac{j_n}{m_b}\]

Subtracting the preceding two equations results in the following:

\[((v'_a -v'_b)\cdot n = (v_a-v_b)\cdot n - j_n(\frac{1}{m_a}+\frac{1}{m_b})\]

Given how we have defined relative velocity the above equation simplifies to:

\[v'_{ab}\cdot n = v_{ab}\cdot n - j_n\left(  \frac{1}{m_a}+\frac{1}{m_b}  \right)\]

Given the definnition of the coefficient of restitution,

\[v'_{ab}\cdot n=-e(v_{ab}\cdot n) \]

we can say that:

\[-e(v_{ab}\cdot n)=  v_{ab}\cdot n-j_n\left( \frac{1}{m_a} +\frac{1}{m_b} \right)\]

Collecting the terms and solving for j(n), the impulse in the normal direction result in the following:

\[j_n=\frac{(1+e)(v_{ab}\cdot n)}{\frac{1}{m_a}+\frac{1}{m_b}}\]


We can adopt a similar approach to calculating the tangential component of J.

First perform the dot product with the t vector on both sides of the impulse equation.
\[v'_a\cdot t=\left( v_a-\left[ \frac{j_n}{m_a}n+\frac{j_t}{m_a}t \right] \right)\cdot t=v_a\cdot t-\frac{j_t}{m_a}\]
\[v'_b\cdot t=\left( v_b+\left[ \frac{j_n}{m_b}n+\frac{j_t}{m_b}t \right] \right)\cdot t=v_b\cdot t+\frac{j_t}{m_b}\]
Following similar steps adopted for the normal component, we get:
\[v'_{ab}\cdot t = v_{ab}\cdot t - j_t\left( \frac{1}{m_a}+\frac{1}{m_a} \right)\]
Given that:
\[v'_{ab}\cdot t=f(v_{ab}\cdot t) \]
Now substitute the equation for the friction coefficient:
\[f(v_{ab}\cdot t) = v_{ab}\cdot t - j_t\left( \frac{1}{m_a}+\frac{1}{m_b} \right)\]
Collecting the terms and solving for j(n), the impulse in the normal direction result in the following:
\[j_t=\frac{(1-f)(v_{ab}\cdot t)}{\frac{1}{m_a}+\frac{1}{m_b}}\]




Moment and Centre of Mass of Simple Systems

 



Remember, moment (or Torque) is force times distance. Consider a balanced seesaw, like below.


The moment of the red box is the product of \(m_1\) and \(x_1\), and the moment of the blue box is product of \(m_2\) and \(x_2\). The seesaw is balanced with the fulcrum shifted to the left of the centre of the seesaw, at x = 0, the "origin".

The centre of mass of the "system" along the x-axis is where the fulcrum is. Mathematically it can be represented as:
\[\bar{x}=\frac{m_1\times x_1 +m_2\times x_2 }{m_1+m_2}\]
where the capital M is the mass of the total system. 

It is the sum of the moment of each "element" divided by the total mass of the system. This makes intuitive sense. Note this analysis is in one dimension. If we extend the analysis to two dimensions. As more than one dimension is involved one needs to be aware of the "axis" around which the moment will act. However, the mathematics are basically the same - except we need to do the calculation along the x and y-axes.

\[\bar{x}=\frac{\sum_{i=1}^{n}{m_i\times x_i}}{\sum_{i=1}^{n}{m_i}}\]



\[M_x=\sum_{i=1}^nm_iy_i\]
and
\[M_y=\sum_{i=1}^nm_ix_i\]

Also, the coordinates of the center of mass \((\bar{x},\bar{y})\) of the system are
\[\bar{x}=\dfrac{M_y}{m}\]
\[\bar{y}=\dfrac{M_x}{m}\]



\[M_x=\lim_{Δm→0}\sum_{i=1}^nΔm_iy_i =\int_{0}^{n}ydm\]
\[\M_y=\lim_{Δm→0}\sum_{i=1}^nΔm_ix_i =\int_{0}^{n}xdm]

Moment



The moment of an element around the x-axis which is free to rotate is given by the product of the mass of the element and the distance (displacement) from the axis of rotation, ie y coordinate of the element.

Similarly, the moment of an element around the y-axis is the product of its mass and its distance from the axis, ie x.

Centre of Mass


The x-coordinate of the centre of mass is the sum of the moments about the y-axis, divided by the total mass of the system \[\bar{x}=\frac{m_1\times x_1 +m_2\times x_2 }{m_1+m_2}=\frac{1}{M_T}\sum_{i=0}^{n}{m_i\times x_i}\] Simiarly, the y-coordinate of the centre of mass is the moment of the whole system around the x-axis divided by the total mass of the system. \[\bar{y}=\frac{m_1\times y_1 +m_2\times y_2 }{m_1+m_2}=\frac{1}{M_T}\sum_{i=0}^{n}{m_i\times y_i}\]

Mass Moment of Inertia of Arbitrary Polygon



By integration

Calculate \(I_x\) and \(I_y\) and use the perpendicular axis theorem to get the \(I_o\). Finally you can use the parallel axis theorem to shift the origin to the centre of mass.

For a polygon \(A_0,A_1,A_2⋯A_{n−1}\) with \(A_i=(xi,yi)Ai=(x_i,y_i)\) and \(A_n=A_0\), those are equal to:

\[I_x =\rho \frac{1}{12}\sum_{i=0}^{n-1}\left(x_iy_{i+1}-x_{i+1}y_i\right)\left(y_i^2+y_iy_{i+1}+y_{i+1}^2\right)\]
\[I_y =\rho \frac{1}{12}\sum_{i=0}^{n-1}\left(x_iy_{i+1}-x_{i+1}y_i\right)\left(x_i^2+x_ix_{i+1}+x_{i+1}^2\right)\]

Now, denote by zz the line perpendicular to the \(xy\)-plane that passes through the origin. Then:

\[\begin{align}J_z &= I_x+I_y\\&= \rho \frac1{12}\sum_{i=0}^{n-1}(x_{i}y_{i+1}-x_{i+1}y_i)\left(x_{i+1}^2+x_{i+1}x_i+x_i^2 +y_{i+1}^2+y_{i+1}y_i+y_i^2\right)\end{align}\]

Then the Parallel Axis Theorem can be used to shift the reference axis to the centre of mass.

Useful References



Polygon: Polygon Clipping by Shterland-Hodgman Algorithm

There are four possible cases for any given edge of given polygon against current clipping edge e.

  1. Both vertices are inside : Only the second vertex is added to the output list
  2. First vertex is outside while second one is inside : Both the point of intersection of the edge with the clip boundary and the second vertex are added to the output list
  3. First vertex is inside while second one is outside : Only the point of intersection of the edge with the clip boundary is added to the output list
  4. Both vertices are outside : No vertices are added to the output list
There are two sub-problems that need to be discussed before implementing the algorithm:-
  1. To decide if a point is inside or outside the clipper polygon
  2. To find the point of intersection of an edge with the clip boundary
Determining which side a point lies againt a line
If the vertices of the clipper polygon are given in clockwise order then all the points lying on the right side of the clipper edges are inside that polygon. This can be calculated using :

Given that the line starst from (x1,y1) and ends at (x2,y2):
\[P=(x2-x1)(y-y1)-(y2-y1)(x-x1)\]
if P<0 the point is on the right sie of the lin
P=0 the ponit is on the line
P>0 pont is on left side




Useful References

Polygon Clipping | Sutherland–Hodgman Algorithm - GeeksforGeeks

Sutherland–Hodgman algorithm - Wikipedia




Numerical Integration: 2nd and 4th order Runge-Kutta schemes

Second-order Runge-Kutta scheme (RK2) There are several variations of the second order Runge-Kutta schemes of numerical integration. They ar...