algorithm - 如何解决这个算法问题(关于平面和线)?
问题描述
有一个问题“给定一些格式为 Ax + By + C = 0 的线,然后计算这些线可以划分为平面中的多少个区域。我对这个问题一无所知。谁能帮助我解决问题?
解决方案
一个平面可以按n
线划分的最大区域数为:
A(n) = ((n * n) + n + 2) / 2
请参阅此说明以供参考。
但是还剩下3个特殊情况:
- 行必须与其他行不同
- 平行线将导致更少的区域,因为线的交叉点更少
- 超过两条线交叉的每个点将导致更少的区域
所以伪代码的解决方案可能是:
function calculate_areas():
L = all lines
L' = remove_duplicate_lines(L)
n = number of lines in L'
p = number_of_parallel_lines(L') # note that three lines that are parallel to each other are counted as +2 here
c = number_of_crosspoints_of_more_than_two_lines(L') # for each crossing point add the number of lines that cross in this point - 2
a = (((n * n) + n + 2) / 2) - p - c
return a
推荐阅读
- react-native - Twilio 聊天 React Native SDK 错误:无法添加命令
- php - 出现 amp; 在网址中。我怎样才能删除它?
- r - 使用 cleanNLP 和 stanford-corenlp 后端注释西班牙语句子时的编码问题
- c++ - 如何在 geos-C++ 中执行 unaryunion 函数
- python-3.x - 如何在 Python3 中将 UTC 日期字符串转换为时区感知日期时间对象?
- gis - Netlogo GIS 不支持的形状类型文件
- html - SVG 元素在 Chrome 浏览器中居中对齐,但应向右对齐
- java - 改变方法内部的局部变量,方法执行时变量不变
- angular - 将自定义组件指令绑定到 for 循环中的对象
- node.js - 如何比较数组mongodb查询