计算几何LineLeftIntersect

This article is made by Jason-Cow.
Welcome to reprint.
But please post the article's address.

好好领悟一下vector吧!加一行Left还可以判断是否在直线上...

struct L{
  D O;V v;db a;
  L(){}
  L(D O,V v):O(O),v(v){a=atan2(O.y,O.x);}//atan2(y,x)=atan(y/x) but the x is OK to be zero
  bl op<(const L&x)const{return a<x.a;}
};

bl Left(D A,L l){return Cross(l.v,A-l.O)>0;}
int main(){
  V A(1,1),B(2,2);
  L l(A,B-A);
  printf("l( O(%lf,%lf) V(%lf,%lf) a(%lf))\n",l.O.x,l.O.y,l.v.x,l.v.y,l.a);
  printf("( 1, 3) is on the %s\n",Left(V( 1, 3),l)?"Left":"right");
  printf("( 3, 1) is on the %s\n",Left(V( 3, 1),l)?"Left":"right");
  printf("(-1, 3) is on the %s\n",Left(V(-1, 3),l)?"Left":"right");
  printf("( 0,-2) is on the %s\n",Left(V( 0,-2),l)?"Left":"right");
  printf("( 0, 0) is on the %s\n",Left(V( 0, 0),l)?"Left":"right");
  return 0;
}
l( O(1.000000,1.000000) V(1.000000,1.000000) a(0.785398)) //a=pi/4
( 1, 3) is on the Left
( 3, 1) is on the right
(-1, 3) is on the Left
( 0,-2) is on the right
( 0, 0) is on the right

Intersect(L a,L b)  

Return The Dot Of Two Line

D Intersect(L a,L b){
    V u=a.O-b.O;
    return a.O+a.v*(Cross(b.v,u)/Cross(a.v,b.v));
}

int main()

int main(){
    L a(D(0,1),D(0,1)-D(-1,0));
    L b(D(1,0),D(1,0)-D(0,1));
    D A=Intersect(a,b);
    cout<<"("<<A.x<<","<<A.y<<")"<<endl;
  return 0;
}
(0,1)
~~Jason_liu O(∩_∩)O
原文地址:https://www.cnblogs.com/JasonCow/p/6596721.html