Thủ thuật này là quy luật của Horner. Trong vòng lặp, thực hiện phép nhân (thay vì hai) đầu tiên, và sau đó một Ngoài ra. Chúng tôi cũng có thể loại bỏ một trong các phép nhân, phép nhân vô dụng của không: | elsif abs det_b epsilon The other cross product is zero and the other vector is also zero . return 1 if abs dx10 epsilon and abs dy10 epsilon Page 440 return 0 Default is no intersection. We ll test line_intersect with two pairs of lines. The first pair intersects at 3 4 and the second pair of lines do not intersect at all because they re parallel print Intersection n if line_intersect 3 0 3 6 1 1 6 6 print No intersection n unless line_intersect 1 1 6 6 4 2 7 5 Intersection No intersection Line Intersection The Horizontal-Vertical Case Often the general case of line intersection is too general if the lines obey Manhattan geometry that is if they re strictly horizontal or vertical a very different solution for finding the intersections is available. The solution is to use binary trees which were introduced in Chapter 3 Advanced ata Structures. We will slide a horizontal line from bottom to top over our plane constructing a binary tree of lines as we do so. The resulting binary tree contains vertical lines sorted on their x-coordinate for this reason the tree is called an x-tree. The x-tree is constructed as follows The points will be processed from bottom to top vertical lines before horizontal ones and from left to right. This means that both endpoints of a horizontal line will be seen simultaneously while the endpoints of a vertical line will be seen separately. Whenever the lower endpoint of a vertical line is seen that node is added to the binary tree with its x-coordinate as the value. This divides the points in the tree in a left-right manner if line a is left of line b node a will be left of node b in the tree. Whenever the upper endpoint of a vertical line is seen the corresponding node is deleted from the binary tree. Whenever a horizontal line is encountered the nodes in the tree the active vertical lines are checked to determine whether any of them intersect the horizontal line. The horizontal lines are not added to the tree their only duty is to .