Project Euler 102
Project Euler 102
题目
Triangle containment
Three distinct points are plotted at random on a Cartesian plane, for which $-1000 \leq x,y\leq 1000$, such that a triangle is formed.
Consider the following two triangles:
It can be verified that triangle $ABC$ contains the origin, whereas triangle $XYZ$ does not.
Using triangles.txt (right click and ‘Save Link/Target As\dots’), a 27K text file containing the co-ordinates of one thousand “random” triangles, find the number of triangles for which the interior contains the origin.
NOTE: The first two examples in the file represent the triangles in the example given above.
解决方案
设向量$\overrightarrow{a}=(x_1,y_1),\overrightarrow{b}=(x_2,y_2)$。那么向量的叉积为
其中$\theta$为$\overrightarrow{a}$逆时针旋转到$\overrightarrow{b}$的角度。
如果向量$\overrightarrow{b}$的方向为向量$\overrightarrow{a}$逆时针旋转$180°$内,那么$\overrightarrow{a}\times\overrightarrow{b}>0$。
因此,对于$\triangle ABC$任意内一点$P$,如果$\overrightarrow{PA}\times \overrightarrow{PB},\overrightarrow{PB}\times \overrightarrow{PC},\overrightarrow{PC}\times \overrightarrow{PA}$有一个值为$0$,那么$P$在$\triangle ABC$边上。如果三个值的正负性都相同,那么$P$在$\triangle ABC$内部。否则P在$\triangle ABC$外部。
代码
1 | ls = open('p102_triangles.txt', 'r').readlines() |