算法之美——求两直线交点(三维叉积)——求四边形面积(二维叉积)

一般方程法:

直线的一般方程为F(x) = ax + by + c = 0。既然我们已经知道直线的两个点,假设为(x0,y0), (x1, y1),那么可以得到a = y0 – y1, b = x1 – x0, c = x0y1 – x1y0

因此我们可以将两条直线分别表示为

F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0

那么两条直线的交点应该满足

a0*x + b0*y +c0 = a1*x + b1*y + c1

由此可推出

x = (b0*c1 – b1*c0)/D

y = (a1*c0 – a0*c1)/D

D = a0*b1 – a1*b0 (D0时,表示两直线重合)

二者实际上就是连立方程组F0(x) = a0*x + b0*y + c0 = 0, F1(x) = a1*x + b1*y + c1 = 0的叉积应用

i     j     k

a0 b0 c0

a1 b1 c1

[cpp]  view plain  copy
 print ?
  1. #include <iostream>  
  2. #include <string>  
  3.   
  4. using namespace std;  
  5.   
  6. struct Point  
  7. {  
  8.     double x;  
  9.     double y;  
  10. };  
  11.   
  12. struct Line  
  13. {  
  14.     Point p1,p2;  
  15.     double a,b,c;  
  16. };  
  17.   
  18. void GetLinePara(Line &l)  
  19. {  
  20.     l.a=l.p1.y-l.p2.y;  
  21.     l.b=l.p2.x-l.p1.x;  
  22.     l.c=l.p1.x*l.p2.y-l.p1.y*l.p2.x;  
  23. }  
  24.   
  25. Point getCrossPoint(Line &l1,Line &l2)  
  26. {  
  27.     GetLinePara(l1);  
  28.     GetLinePara(l2);  
  29.     double D=l1.a*l2.b-l2.a*l1.b;    
  30.     Point p;    
  31.     p.x=(l1.b*l2.c-l2.b*l1.c)/D;    
  32.     p.y=(l1.c*l2.a-l2.c*l1.a)/D;    
  33.     return p;   
  34. }  
  35. int main()  
  36. {  
  37.     Line L1,L2;  
  38.     while(true)  
  39.     {  
  40.         cout<<"Line1:\n"<<"Point1.x: ";  
  41.         cin>>L1.p1.x;  
  42.         cout<<"Point1.y: ";  
  43.         cin>>L1.p1.y;  
  44.         cout<<"Line1:\n"<<"Point2.x: ";  
  45.         cin>>L1.p2.x;  
  46.         cout<<"Point2.y: ";  
  47.         cin>>L1.p2.y;  
  48.         cout<<"Line2:\n"<<"Point1.x: ";  
  49.         cin>>L2.p1.x;  
  50.         cout<<"Point1.y: ";  
  51.         cin>>L2.p1.y;  
  52.         cout<<"Line2:\n"<<"Point2.x: ";  
  53.         cin>>L2.p2.x;  
  54.         cout<<"Point2.y: ";  
  55.         cin>>L2.p2.y;  
  56.         cout<<endl;  
  57.   
  58.         Point PointCross=getCrossPoint(L1,L2);  
  59.         cout<<"CrossPoint.x:"<<PointCross.x<<endl;  
  60.         cout<<"CrossPoint.y:"<<PointCross.y<<endl;  
  61.         cout<<endl;  
  62.     }  
  63. }  


 

ZOJ Problem Set - 1683

题目大意:

在1*1的正方形格子上给出每边的两个点,按顺序连接对边的点,求这样形成的一系列四边形中最大的面积。

思路:

1. 求交点

      见函数GetCrossPoint (http://blog.csdn.NET/abcjennifer/article/details/7584628)

2. 求面积

      四边形面积可由两个三角形面积相加而得,这里使用叉积,因为三角形的两个向量边叉乘的结果为其所形成的平行四边形面积,所以三角形面积为1/2×向量边1 x 向量边2

      见函数GetArea()


Code:

[cpp]  view plain  copy
 print ?
  1. #include"iostream"  
  2. #include"stdio.h"  
  3. #include"math.h"  
  4. using namespace std;  
  5.   
  6. #define N 40  
  7. int n;  
  8.   
  9. struct Point {  
  10.     double x;  
  11.     double y;  
  12. } pset[N + 1][N + 1];  
  13.   
  14. struct Line {  
  15.     void L(Point pp1, Point pp2) {  
  16.         p1 = pp1;  
  17.         p2 = pp2;  
  18.     }  
  19.     Point p1, p2;  
  20.     double a, b, c;  
  21. } lineset[N][N];  
  22.   
  23. void GetLinePara(Line *l) {  
  24.     l->a = l->p1.y - l->p2.y;  
  25.     l->b = l->p2.x - l->p1.x;  
  26.     l->c = l->p1.x * l->p2.y - l->p2.x * l->p1.y;  
  27. }  
  28.   
  29. Point GetCrossPoint(Line *l1, Line *l2) {  
  30.     GetLinePara(l1);  
  31.     GetLinePara(l2);  
  32.     double D = l1->a * l2->b - l2->a * l1->b;  
  33.     Point p;  
  34.     p.x = (l1->b * l2->c - l2->b * l1->c) / D;  
  35.     p.y = (l1->c * l2->a - l2->c * l1->a) / D;  
  36.     return p;  
  37. }  
  38.   
  39. double Xmult(Point a, Point b, Point c)//get vector ac and bc 计算两个二维向量叉积,两条边界向量  
  40. {  
  41.     return (c.x - a.x) * (c.y - b.y) - (c.y - a.y) * (c.x - b.x);  
  42. }  
  43.   
  44. double GetArea(int i, int j) {  
  45.     double area1 = Xmult(pset[i][j], pset[i + 1][j], pset[i + 1][j + 1]) / 2;  
  46.     double area2 = Xmult(pset[i][j], pset[i][j + 1], pset[i + 1][j + 1]) / 2;  
  47.     return fabs(area1) + fabs(area2);  
  48. }  
  49.   
  50. void Init_Points() {  
  51.     int i;  
  52.     for (i = 1; i <= n; i++)  
  53.         scanf("%lf", &pset[i][0].x), pset[i][0].y = 0;  
  54.     for (i = 1; i <= n; i++)  
  55.         scanf("%lf", &pset[i][n + 1].x), pset[i][n + 1].y = 1;  
  56.     for (i = 1; i <= n; i++)  
  57.         scanf("%lf", &pset[0][i].y), pset[0][i].x = 0;  
  58.     for (i = 1; i <= n; i++)  
  59.         scanf("%lf", &pset[n + 1][i].y), pset[n + 1][i].x = 1;  
  60.   
  61.     pset[0][0].x = pset[0][0].y = 0;  
  62.     pset[n + 1][0].x = 1, pset[n + 1][0].y = 0;  
  63.     pset[0][n + 1].x = 0, pset[0][n + 1].y = 1;  
  64.     pset[n + 1][n + 1].x = pset[n + 1][n + 1].y = 1;  
  65. }  
  66.   
  67. void Init_Lines() {  
  68.     int i, j;  
  69.     Line l1, l2;  
  70.     for (i = 1; i <= n; i++) {  
  71.         for (j = 1; j <= n; j++) {  
  72.             l1.L(pset[i][0], pset[i][n + 1]);  
  73.             l2.L(pset[0][j], pset[n + 1][j]);  
  74.             pset[i][j] = GetCrossPoint(&l1, &l2);  
  75.         }  
  76.     }  
  77. }  
  78.   
  79. int main() {  
  80.     int i, j;  
  81.     while (cin >> n&&n) {  
  82.         Init_Points();  
  83.         Init_Lines();  
  84.         double maxarea = 0;  
  85.         double area;  
  86.         for (i = 0; i < n + 1; i++)  
  87.             for (j = 0; j < n + 1; j++) {  
  88.                 area = GetArea(i, j);  
  89.                 maxarea = area > maxarea ? area : maxarea;  
  90.             }  
  91.         printf("%lf\n", maxarea);  
  92.     }  
  93. }  
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值