#include <opencv2/opencv.hpp>
#include <vector>
#include <cmath>
#include <iostream>
#include <limits>
struct Point2D {
double x;
double y;
};
std::vector<Point2D> translatePoints(const std::vector<Point2D>& points, const Point2D& sommet) {
std::vector<Point2D> translatedPoints;
for (const auto& point : points) {
translatedPoints.push_back({point.x - sommet.x, point.y - sommet.y});
}
return translatedPoints;
}
std::vector<Point2D> rotatePoints(const std::vector<Point2D>& points, double theta) {
std::vector<Point2D> rotatedPoints;
for (const auto& point : points) {
rotatedPoints.push_back({point.x * cos(theta) - point.y * sin(theta), point.x * sin(theta) + point.y * cos(theta)});
}
return rotatedPoints;
}
//renvoie a
float fitParabola(const std::vector<Point2D>& points, double& sumOfSquaredErrors) {
int n = points.size();
cv::Mat1f X(n, 2);
cv::Mat1f Y(n, 1);
for (int i = 0; i < n; i++) {
float x = points[i].x;
X(i, 0) = x*x;
X(i, 1) = 1;
Y(i) = points[i].y;
}
cv::Mat1f ab = (X.t() * X).inv() * X.t() * Y;
sumOfSquaredErrors = 0;
for (int i = 0; i < n; i++) {
float error = Y(i) - (ab(0) * X(i, 0) + ab(1));
sumOfSquaredErrors += error * error;
}
return ab(0);
}
int main() {
std::vector<Point2D> points = {/* vos points ici */};
Point2D sommet = {/* sommet ici */};
std::vector<Point2D> translatedPoints = translatePoints(points, sommet);
double bestTheta = 0;
double minSumOfSquaredErrors = std::numeric_limits<double>::infinity();
float bestCoefficient;
for (int i = 0; i < 360; ++i) {
double theta = i * CV_PI / 180.0; // Convertir en radians
std::vector<Point2D> rotatedPoints = rotatePoints(translatedPoints, theta);
double sumOfSquaredErrors;
float coefficient = fitParabola(rotatedPoints, sumOfSquaredErrors);
if (sumOfSquaredErrors < minSumOfSquaredErrors) {
minSumOfSquaredErrors = sumOfSquaredErrors;
bestCoefficient = coefficient;
bestTheta = theta;
}
}
std::cout << "Meilleur angle theta (en radians) : " << bestTheta << std::endl;
std::cout << "Le coefficient de la parabole est : " << bestCoefficient << std::endl;
return 0;
}
Ce que je veux dans tous les cas c'est les coefficients a, b et c de la parabole comme si elle n'avait pas été tournée.
Si tu veut effectivement que le sommet soit en (0,0), c'est bien uniquement y = ax^2 qu'il faut prendre.sylvain231 a écrit:Ben314 >> tu cherches une parabole d'équation y = ax^2 il ne faut pas plutôt prendre y = ax^2 +b x ?
Utilisateurs parcourant ce forum : Aucun utilisateur enregistré et 37 invités
Tu pars déja ?
Identification
Pas encore inscrit ?
Ou identifiez-vous :