This film uses the midpoint shift method to simulate lightning.
The midpoint shift method is usually the algorithm for the raw terrain, and you will find that the shape of the lightning is similar to the edge of an idealized mountain range.
The following excerpt of the Lightning Recursive program can help you do all the work.
123456789101112131415 |
function drawLightning(x1,y1,x2,y2,displace)
{
if (displace < curDetail) {
graf.moveTo(x1,y1);
graf.lineTo(x2,y2);
}
else {
var mid_x = (x2+x1)/2;
var mid_y = (y2+y1)/2;
mid_x += (Math.random()-.5)*displace;
mid_y += (Math.random()-.5)*displace;
drawLightning(x1,y1,mid_x,mid_y,displace/2);
drawLightning(x2,y2,mid_x,mid_y,displace/2);
}
}
|
You can calculate the end-of-line coordinate (mid_x,mid_y) by passing the two endpoint coordinates (X1,Y1,X2,Y2) of a line segment and a displacement, and then replace it with a random value, which decreases according to the displacement value of each segment.
The first Division results in a larger displacement segment, and then the displacement segment decreases gradually as the displacement value decreases (each recursive displacement value is divided by 2), so that a segment is "broken".
We draw this line when the displacement value is lower than our defined minimum value (which can be modified using the "detail" slider). That is, if the conditional if (Displace<curdetail) is met, we draw the segment.
Four sliders in the original:
As the 1.detail increases, the number of lines decreases, and each line is longer.
2.thickness represents the thickness of the line
3.number bolts represents the number of lines
4.displacement displacement, which is the maximum value of the line's numerical direction offset
Original:http://krazydad.com/bestiary/bestiary_lightning.html
(turn) The realization of the lightning effect, the midpoint displacement method