判断线段是否相交的函数和求直线交点的函数

// ToLineCrossPofloat.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct POINT
{
 int x;
 int y;
};
/*
判断两条线段是否相交(有交点)
*/
bool IsLineSegmentCross(POINT pFirst1, POINT pFirst2, POINT pSecond1, POINT pSecond2)
{
 //每个线段的两点都在另一个线段的左右不同侧,则能断定线段相交
 //公式对于向量(x1,y1)->(x2,y2),判断点(x3,y3)在向量的左边,右边,还是线上.
 //p=x1(y3-y2)+x2(y1-y3)+x3(y2-y1).p<0 左侧, p=0 线上, p>0 右侧
 long Linep1,Linep2;
 //判断pSecond1和pSecond2是否在pFirst1->pFirst2两侧
 Linep1 = pFirst1.x * (pSecond1.y - pFirst2.y) +
  pFirst2.x * (pFirst1.y - pSecond1.y) +
  pSecond1.x * (pFirst2.y - pFirst1.y);
 Linep2 = pFirst1.x * (pSecond2.y - pFirst2.y) +
  pFirst2.x * (pFirst1.y - pSecond2.y) +
  pSecond2.x * (pFirst2.y - pFirst1.y);
 if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pSecond1和pSecond2在pFirst1->pFirst2同侧
 {
  return false;
 }
 //判断pFirst1和pFirst2是否在pSecond1->pSecond2两侧
 Linep1 = pSecond1.x * (pFirst1.y - pSecond2.y) +
  pSecond2.x * (pSecond1.y - pFirst1.y) +
  pFirst1.x * (pSecond2.y - pSecond1.y);
 Linep2 = pSecond1.x * (pFirst2.y - pSecond2.y) +
  pSecond2.x * (pSecond1.y - pFirst2.y) +
  pFirst2.x * (pSecond2.y - pSecond1.y);
 if ( ((Linep1 ^ Linep2) >= 0 ) && !(Linep1==0 && Linep2==0))//符号位异或为0:pFirst1和pFirst2在pSecond1->pSecond2同侧
 {
  return false;
 }
 //否则判为相交
 return true;
}
/*
求两直线交点,前提是两条直线必须有交点
在相交的情况下,可以应付各种情况(垂直、系数等)
*/
POINT GetCrossPoint(POINT p1, POINT p2, POINT q1, POINT q2)
{
 //必须相交求出的才是线段的交点,但是下面的程序段是通用的
 assert(IsLineSegmentCross(p1,p2,q1,q2));
 /*根据两点式化为标准式,进而求线性方程组*/
 POINT crossPoint;
 long tempLeft,tempRight;
 //求x坐标
 tempLeft = (q2.x - q1.x) * (p1.y - p2.y) - (p2.x - p1.x) * (q1.y - q2.y);
 tempRight = (p1.y - q1.y) * (p2.x - p1.x) * (q2.x - q1.x) + q1.x * (q2.y - q1.y) * (p2.x - p1.x) - p1.x * (p2.y - p1.y) * (q2.x - q1.x);
 crossPoint.x =(int)( (double)tempRight / (double)tempLeft );
 //求y坐标 
 tempLeft = (p1.x - p2.x) * (q2.y - q1.y) - (p2.y - p1.y) * (q1.x - q2.x);
 tempRight = p2.y * (p1.x - p2.x) * (q2.y - q1.y) + (q2.x- p2.x) * (q2.y - q1.y) * (p1.y - p2.y) - q2.y * (q1.x - q2.x) * (p2.y - p1.y);
 crossPoint.y =(int)( (double)tempRight / (double)tempLeft );
 return crossPoint;
}
int main(void)
{
 POINT pointA,pointB,pointC,pointD;
 POINT pointCross;
 bool bCross(false);
 pointA.x = 0;pointA.y = 0;
 pointB.x = 0;pointB.y = 100;

 pointC.x = 350;pointC.y = 10;
 pointD.x = -10;pointD.y = 10;
 bCross = IsLineSegmentCross(pointA,pointB,pointC,pointD);
 if (bCross)
 {
  pointCross = GetCrossPoint(pointA,pointB,pointC,pointD);
  printf("交点坐标x=%d,y=%d/n",pointCross.x,pointCross.y);
 }
 else
 {
  printf("They are not crossed!");
 }
 return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的 C++ 函数,用于判断两条三维线段是否相交: ```cpp #include <iostream> #include <cmath> using namespace std; struct Point3D { double x, y, z; }; bool isIntersected(Point3D p1, Point3D p2, Point3D p3, Point3D p4) { Point3D p13, p43, p21; double d1343, d4321, d1321, d4343, d2121; double numer, denom; const double EPSILON = 0.0000001; p13.x = p1.x - p3.x; p13.y = p1.y - p3.y; p13.z = p1.z - p3.z; p43.x = p4.x - p3.x; p43.y = p4.y - p3.y; p43.z = p4.z - p3.z; if (fabs(p43.x) < EPSILON && fabs(p43.y) < EPSILON && fabs(p43.z) < EPSILON) return false; p21.x = p2.x - p1.x; p21.y = p2.y - p1.y; p21.z = p2.z - p1.z; if (fabs(p21.x) < EPSILON && fabs(p21.y) < EPSILON && fabs(p21.z) < EPSILON) return false; d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z; d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z; d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z; d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z; d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z; denom = d2121 * d4343 - d4321 * d4321; if (fabs(denom) < EPSILON) return false; numer = d1343 * d4321 - d1321 * d4343; double mua = numer / denom; double mub = (d1343 + d4321 * mua) / d4343; Point3D pa, pb; pa.x = p1.x + mua * p21.x; pa.y = p1.y + mua * p21.y; pa.z = p1.z + mua * p21.z; pb.x = p3.x + mub * p43.x; pb.y = p3.y + mub * p43.y; pb.z = p3.z + mub * p43.z; double dist = sqrt((pa.x - pb.x) * (pa.x - pb.x) + (pa.y - pb.y) * (pa.y - pb.y) + (pa.z - pb.z) * (pa.z - pb.z)); if (dist < EPSILON) return true; else return false; } int main() { Point3D p1 = {0, 0, 0}; Point3D p2 = {1, 1, 1}; Point3D p3 = {0, 1, 0}; Point3D p4 = {1, 0, 1}; if (isIntersected(p1, p2, p3, p4)) { cout << "The two line segments intersect." << endl; } else { cout << "The two line segments do not intersect." << endl; } return 0; } ``` 该函数的输入是两个三维线段的端点坐标,输出是一个布尔值,表示它们是否相交。如果相交,返回 `true`,否则返回 `false`。 该函数使用了两条线段的参数方程,通过解它们的交点判断它们是否相交。如果交点两条线段之间,则它们相交,否则它们不相交。 该函数还考虑了一些特殊情况,例如两条线段重合、平行、退化为点或直线等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值