SoFunction
Updated on 2025-03-01

Share a written test question [How many parts can a plane be divided into with n straight lines]

Copy the codeThe code is as follows:

<html>
Question:<br />
How many parts can a plane be divided into with n straight lines?
Number of lines:<input type="text" /><br />
Intersection point:<label></label><br />
Number of segmentation:<label style="background:yellow;"></label><br />
<input type="button" onclick="calculate()" value="calculate"/>
</html>
<script type="text/javascript">
function calculate(line)
{
var line = ('line').value;
if(line == "")
{
line = 0;
('line').value = line;
}
var line = parseInt(line);
var innerPoint = line * (line -1) / 2;
var part = ((line,2) + line)/2 + 1;//line + innerPoint + 1 equals (square of the number of lines + number of lines)/2 + 1

('innerPoint').innerText = innerPoint;
('part').innerText = part;
}
</script>

Let me tell you the rules:

①The most divided part: number of lines + number of intersecting points + 1

② Number of intersecting points = number of intersecting points of (number of lines-1) + (number of lines-1). The newly added line can have intersections with lines other than them.

③ Use recursion to calculate the number of intersecting points, and then substitute them into ①

The above is normal mathematical thinking. Let’s talk about the knowledge I used in the practice test, which is what my code is.

I have listed some available parameters for 1 to 5 straight lines:

Number of intersecting points in straight lines

1  0    2    2

2    1  4   4

3    3  6     7

4    6    8    11

5   1010 16

It was found that diplomatic points were meaningless, they were twice the number of straight lines anyway

And the number of parts = number of straight lines + number of intersecting points + 1

The number of intersecting points in adjacent straight lines forms an arithmetic sequence. The tolerance of this arithmetic sequence is 1, 1-0=1,3-1=2,6-3=3,10-6=4, 1+0=1,2+1=3,3+3=6, etc. However, this still uses the recursion to require the previous corresponding number of intersecting points, so look at the rules vertically, 2*1=2 3*2=6 4*3=12... It is exactly twice the number of intersecting points.