Implementation of lightning effect, midpoint displacement method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.