平面c++实现

平面

在这里插入图片描述
平面用垂线(法线) 表示
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
根据 y=vt+a
推出 L(t) = (1,4,-2)·t+ (2,-4,3)

    template <typename coord_type, size_t dim = DIM3>
    class Plane
    {
        Vector3f normal;
        float d = 0.0;

    public:
        Plane(){};

        Plane(Vector3f &_normal, float _constant)
            : normal(_normal), d(_constant)
        {
        }

        Plane(Point3d &_p1, Point3d &_p2, Point3d &_p3)
        {
            Vector3f v12 = _p2 - p1;
            Vector3f v13 = _p3 - p1;

            normal = crossProduct3D(v12, v13);
            d = dotProduct(normal, _p1);
        }
    };

线与平面的交点

在这里插入图片描述
在这里插入图片描述

结果: t1=-(n·P)+D

bool Intersection(const Line3d& line, const Planef& plane, Point3d& point)
{
	auto n = plane.getNormal();
	auto D = plane.getD();
	auto d = line.getDir();
	auto p = line.getPoint();

	auto nd = dotProduct(n, d);
	if (!isEqualDouble(nd, ZERO))
	{
		auto t = (-1 * dotProduct(n, p) + D) / nd;
		point.assign(X, p[X] + t * d[X]);
		point.assign(Y, p[Y] + t * d[Y]);
		point.assign(Z, p[Z] + t * d[Z]);
		return true;
	}
	else
		return false;
	return false;
}

两个平面相交

在这里插入图片描述
n为(A,B,C)->法向量,P(x1,y1,z1)这是两平面相交线中任意一点。

那么通式可以表示为R=a·n1+b·n2
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

bool Intersection(const Planef& p1, const Planef& p2, Line3d& l)
{
	auto n1 = p1.getNormal();
	auto n2 = p2.getNormal();
	auto d1 = p1.getD();
	auto d2 = p2.getD();

	auto direction = crossProduct3D(n1, n2);

	if (isEqualDouble(direction.magnitude(), ZERO))
		return false;

	auto n1n2 = dotProduct(n1, n2);
	auto n1n2_2 = n1n2 * n1n2;

	auto a = (d2 * n1n2 - d1) / (n1n2_2 - 1);
	auto b = (d1 * n1n2 - d2) / (n1n2_2 - 1);

	auto point = n1 * a + n2 * b;

	l.setPoint(point);
	direction.normalize();
	l.setDirection(direction);
	
	return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yhaida

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值