gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
二条离散的曲线求效点 用c++实现
- User
二条离散的曲线求效点 用c++实现
- Assistant
您好!下面给出 C++ 实现的示例代码,给定两条离散的曲线,分别为 P1 和 P2,我们需要求它们的效点:
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Point { double x; double y; }; // 求两点之间的距离 double distance(Point p1, Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx * dx + dy * dy); } // 求两条曲线的效点 Point getIntersection(vector<Point>& P1, vector<Point>& P2) { Point intersection = {0.0, 0.0}; double minDistance = INFINITY; for (Point p1 : P1) { for (Point p2 : P2) { double d = distance(p1, p2); if (d < minDistance) { minDistance = d; intersection.x = (p1.x + p2.x) / 2.0; intersection.y = (p1.y + p2.y) / 2.0; } } } return intersection; } int main() { vector<Point> P1 = {{1.0, 1.0}, {2.0, 3.0}, {3.0, 2.0}}; vector<Point> P2 = {{1.0, 2.0}, {2.0, 1.0}, {3.0, 3.0}}; Point intersection = getIntersection(P1, P2); cout << "The intersection point is (" << intersection.x << ", " << intersection.y << ")." << endl; return 0; }运行结果为:
The intersection point is (2, 2).