300P: ColoringRectangle
Greedy algorithm works to this problem:
After we sort the red disks and blue disks by descending order, the final disk placed order would be determined if we choose one of the largest red and blue disks as the left-most disk, where the red disks alternate with the blue disks.
Trick: The disk cannot be used whose diameter is less the height of the rectangle.
500: BuildingCities
Build a graph with at most 50 * 2000 (f[i][j]) vertexes, which means the minimum distance of vertex i which has used j new cities.
Then run shortest path algorithm (SPFA or Dijkstra) on this graph to find the answer.
950: FencingGarden
Tags: SRM
Given a single convex polygon and a sequence of straight lines, quickly determine whether the straight line intersect with the convex polygon for each query straight line.
I’ll show you an efficient algorithm which can run in O(n – log(n)). It can also report the two intersections after running binary search. (more…)
Tags: Computational Geometry
This problem can be described like this: Given a single convex polygon and a sequence of query points, quickly determine whether a point is in the polygon or not.
I’ll show you two efficient algorithm which can both run in O (n – log(n)). In addition, the first algorithm can expand to star-shaped polygon and the second can expand to monotone polygon.
Star-shaped polygon: a polygon P is called star-shaped if and only if there exists a point z such that for each point p of P the segment zp lies entirely within P.
Monotone polygon: a polygon P is called monotone with respect to a straight line L, if every line orthogonal to L intersects P at most twice. We call straight line L as the normal line of the polygon P.
Convex polygon is star-shaped and is also monotone with respect to any straight line.
1. Choose an edge AB and its midpoint P.
2. Link P to every vertex on the polygon so we can get n vectors which are already sorted by those slopes.
3. For each point X of a query, check if it is on the right side of vector AB. If it does, then return X is outside of the polygon. If it’s on line AB, then check if it is on the segment AB. If it does, then return X is on the edge AB of the polygon.
4. Now, we are sure that X is on the left side of vector AB. The next step, we use new vector PX to run a binary search which is based on the vectors we got at step 2, then we can get an interval where vector PX belongs. There must be an edge of the polygon in that interval.
5. Check the relation between the edge and the vector PX. Finally, we can get the answer.
In step 4, function atan2 can be use to get the slope of each vector, and then we run binary search by those slopes. Actually, we can run binary search on any objects as long as we define the “less” between them. Slope of course can be used to define “less”, but to be better we can use cross product to define “less” to get a more graceful implement.
Similar algorithm is possible for star-shaped polygon where we need to calculate the kernel first. Point P can be found in the kernel and “less” can also be defined by slopes or product cross.
Tags: Computational Geometry
矩阵真的好优美,好多在数学中那些乘乘加加的操作,其本质都是矩阵。这些操作,往往都是把事物进行分解,对每一个基量进行操作,最后再把这些操作整合起来。
向量旋转,这个几何中的基本操作,可以相当优美的用矩阵来操作。究其原因,也是因为向量是多维的。用矩阵来操作,正是把向量分解开来,分别旋转,最后再进行整合。
看下面的矩阵
这个是二维向量的旋转矩阵,它可以将一个向量逆时针旋转一个角度。
将其变形,变会得到二维向量的顺时针旋转的形式:
这里要注意一种特殊的情况,就是当角度为90度的时候,sin和cos的结果只有1, 0, -1三种可能性。所以这个矩阵可以改写成特殊的形式,其意义在于用这样的旋转操作,不会产生精度问题。sin和cos的运算的精度是比较低的,能少用则尽量少用。在计算几何中的向量旋转操作,大部分都可以通过变形,只用到旋转90度,从而避免精度问题。
再来说一点就是,这些旋转矩阵都有一些特点,最明显的莫过于他们的行列式的值都是1。所以在验证正确性的时候,可以用这个操作。也正因为有这一个性质,所以向量才只会进行旋转,不会进行缩放。
三维向量的旋转矩阵:
上面三个公式的旋转方向可以看成按右手定则。
来看第三个公式,这个公式是围绕Z轴,把X轴往Y轴方向移动。我们把拇指向上(表示Z轴),手指指向X轴,然后手指自然弯曲方向便是旋转方向。
其它两个公式也是类似。
其实第三个公式,去掉Z轴,就是开头讲的逆时针旋转的公式。
在其它方向上的旋转都可以由这三个矩阵组合而来。
围绕轴u = (ux, uy, uz)来旋转的矩阵代码如下:
要注意这个ux * ux + uy * uy + uz * uz = 1
有一个比较有意思的问题就是,知道旋转矩阵之后,怎么来确定向量u呢?
我们做如下变形之后,可以得到:
Ru = Iu –> (R – I)u = 0
也就是说我们要找到一个非0的向量,使得他和一个矩阵乘起来得到一个空矩阵。这个问题可以用高斯消元来解决。实际上我们就是要解一个线性方程组。
本来想写更多东西的,可惜我的数学能力实在有限,很多东西自己都没有理解。等以后有机会了,再来补充此文吧。
Tags: 计算几何