求两条线段的交点java_求两条线段的交点

#include

#define ll long long

using namespace std;

struct Point{

ll x,y;

Point(ll x=0,ll y=0):x(x),y(y){};

};

ll gcd(ll a,ll b)

{

return a==0?b:gcd(b%a,a);

}

bool cheak(ll op1,ll a,ll b){

if(a>b)

swap(a,b);

return op1>=a&&op1<=b;

}

Point point_of_intersection(Point f1,Point f2,Point f3,Point f4,bool &mark)

{

ll a1,a2,b1,b2,c1,c2,c3,c4,D,D1,D2;

a1=f2.y-f1.y;

a2=f1.x-f2.x;

b1=a1*f1.x+a2*f1.y;

///b1=(y2-y1)*x1+(x1-x2)*y1

c1=f4.y-f3.y;

c2=f3.x-f4.x;

b2=c1*f3.x+c2*f3.y;

///b2=(y4-y3)*x3+(x3-x4)*y3

c3=f2.x-f1.x;

c4=f4.x-f3.x;

D=c3*c1-c4*a1;

Point res;

if(D==0){

mark=false;

return res;

}

D1=b2*c3-b1*c4;

if(D1%D){

mark=false;

return res;

}

res.x=int(D1/D);

D2=b2*a1-b1*c1;

if(D2%D){

mark=false;

return res;

}

res.y=int(D2/D);

if(!cheak(res.x,f1.x,f2.x)||!cheak(res.x,f3.x,f4.x)){

mark=false; return res;

}

if(!cheak(res.y,f1.y,f2.y)||!cheak(res.y,f3.y,f4.y)){

mark=false; return res;

}

return res;

}

Point edge[1006][2];

int main()

{

int n;

while( ~scanf("%d",&n)){

for(int i=1;i<=n;i++){

scanf("%lld%lld%lld%lld",&edge[i][0].x,&edge[i][0].y,&edge[i][1].x,&edge[i][1].y);

}

set > re;

long long ans=0,tmp;

bool mark;

for(int i=1;i<=n;i++){

tmp=gcd(abs(edge[i][0].x-edge[i][1].x),abs(edge[i][0].y-edge[i][1].y))+1;

re.clear();

for(int j=1;j

mark=true;

Point res=point_of_intersection(edge[i][0],edge[i][1],edge[j][0],edge[j][1],mark);

if(mark)

re.insert(make_pair(res.x,res.y));

}

ans+=tmp-re.size();

}

printf("%lld\n",ans);

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值