Share a single question [There are n straight lines that can divide a plane into many parts]_javascript tips

Source: Internet
Author: User
Tags arithmetic
Copy Code code as follows:

Title: <br/>
There are n lines that can divide a plane up to a number of parts <br/><br/>
Number of lines: <input type= "text" id= "line"/><br/>
Inner intersection: <label id= "Innerpoint" ></label><br/>
Split number: <label id= "part" style= "Background:yellow;" ></label><br/>
<input type= "button" onclick= "Calculate ()" value= "calculation"/>
<script type= "Text/javascript" >
function calculate (line)
{
var line = document.getElementById (' line '). Value;
if (line = = "")
{
line = 0;
document.getElementById (' line '). Value = line;
}
var line = parseint (line);
var innerpoint = line * (line-1)/2;
var part = (Math.pow (line,2) + line)/2 + 1;//line + innerpoint + 1 equals (number of lines squared + lines)/2 + 1

document.getElementById (' Innerpoint '). innertext = Innerpoint;
document.getElementById (' part '). innertext = part;
}
</script>

Say the law:

① the most divided parts: line number + internal intersection points +1

The number of intersections within the ② = (line number-1) of the internal intersection point + (line number-1), the newly added line can be with the other than the line intersection points

③ uses recursion to find the inner intersection number and then ① the calculation

The above is normal mathematical thinking, the following say I use the line test knowledge, is my Code Dongdong

I have listed some of the available parameters for the 1~5 line:

The number of diplomatic points in the intersection of the number of lines

1 0 2 2

2 1 4 4

3 3 6 7

4 6 8 11

5 10 10 16

It turns out that diplomatic points are meaningless, twice times the number of straight lines.

and part number = line number + Internal intersection point +1

The number of intersection points within the number of adjacent lines constitute a arithmetic progression, this arithmetic progression tolerance is 1, 1-0=1,3-1=2,6-3=3,10-6=4, transverse look 1+0=1,2+1=3,3+3=6 ... but this still uses recursion. The number of corresponding internal intersections is required, So longitudinal look at the law, 2*1=2 3*2=6 4*3=12 ... exactly twice times the number of internal intersections.

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.