声明一个结构体point,包含三个数据成员x、y、z,表示一个点在三维空间的坐标。定义一个point类型的结构体数组p[3],对其初始化。并使用指针变量对数组进行遍历,并找出该数组所有元素钟与原点距离的最小值。
时间: 2023-11-28 18:49:50 浏览: 31
下面是代码实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
struct point {
double x, y, z;
};
int main() {
point p[3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // 初始化结构体数组
double min_dis = INFINITY; // 初始化最小距离为无穷大
// 使用指针变量遍历结构体数组
for (point* ptr = p; ptr < p + 3; ptr++) {
double dis = sqrt(ptr->x * ptr->x + ptr->y * ptr->y + ptr->z * ptr->z); // 计算距离
if (dis < min_dis) {
min_dis = dis; // 更新最小距离
}
}
cout << "所有点中与原点最近的距离为:" << min_dis << endl;
return 0;
}
```
输出结果为:
```
所有点中与原点最近的距离为:3.74166
```
其中,INFINITY 是 C++ 标准库中定义的一个宏,表示正无穷大,头文件为 `<cmath>`。指针变量 `ptr` 在遍历结构体数组时,会指向每个结构体元素的首地址,使用 `->` 运算符来访问成员。
相关问题
声明一个结构体point,包含三个数据成员x、y、z,表示一个点在三维空间的坐标。在主函数中定义一个point类型的结构体变量,并该点到原点的距离。
以下是声明point结构体并计算点到原点距离的代码示例:
```c++
#include <iostream>
#include <cmath>
using namespace std;
struct point {
double x;
double y;
double z;
};
int main() {
point p;
p.x = 3.0;
p.y = 4.0;
p.z = 5.0;
double distance = sqrt(p.x * p.x + p.y * p.y + p.z * p.z);
cout << "The distance between the point (" << p.x << ", " << p.y << ", " << p.z << ") and the origin is " << distance << endl;
return 0;
}
```
在上述代码中,我们定义了一个point结构体,并在主函数中创建了一个point类型的变量p,并为其x、y、z三个成员赋值。接着,我们使用sqrt函数计算了该点到原点的距离,并将结果输出到控制台。
c++写一个判断三维空间中判断射线与平面是否相交
下面是一个简单的 C++ 代码实现:
```c++
#include <iostream>
#include <cmath>
using namespace std;
struct Vector3
{
double x, y, z;
Vector3() {}
Vector3(double x, double y, double z) : x(x), y(y), z(z) {}
Vector3 operator+(const Vector3& other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 operator-(const Vector3& other) const
{
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 operator*(double scalar) const
{
return Vector3(x * scalar, y * scalar, z * scalar);
}
double dot(const Vector3& other) const
{
return x * other.x + y * other.y + z * other.z;
}
Vector3 cross(const Vector3& other) const
{
return Vector3(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
}
double length() const
{
return sqrt(x * x + y * y + z * z);
}
};
struct Ray
{
Vector3 origin, direction;
Ray() {}
Ray(const Vector3& origin, const Vector3& direction) : origin(origin), direction(direction) {}
};
struct Plane
{
Vector3 point, normal;
Plane() {}
Plane(const Vector3& point, const Vector3& normal) : point(point), normal(normal) {}
};
bool intersect(const Ray& ray, const Plane& plane, double& t)
{
double denom = plane.normal.dot(ray.direction);
if (abs(denom) < 1e-6) // 判断是否平行
return false;
t = plane.normal.dot(plane.point - ray.origin) / denom;
return t >= 0;
}
int main()
{
// 示例:射线由点(0, 0, 0)出发,方向为(1, 1, 1);平面上的一点为(0, 0, 1),法向量为(0, 0, 1)
Ray ray(Vector3(0, 0, 0), Vector3(1, 1, 1));
Plane plane(Vector3(0, 0, 1), Vector3(0, 0, 1));
double t;
if (intersect(ray, plane, t))
{
Vector3 intersection = ray.origin + ray.direction * t;
cout << "Intersection point: (" << intersection.x << ", " << intersection.y << ", " << intersection.z << ")" << endl;
}
else
{
cout << "No intersection." << endl;
}
return 0;
}
```
这里我们定义了三个结构体:`Vector3` 表示三维向量,`Ray` 表示射线,`Plane` 表示平面。`intersect` 函数判断射线与平面是否相交,并返回相交参数t。在主函数中,我们给出了一个示例并输出相交点的坐标。