# how to get the point in ellipse?

4,376 Reputation points
2024-09-03T04:15:50.3566667+00:00

I have ellipse and one point. I know the cener (x,y) and the point (x1,y1) how to get the point of S?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,685 questions

1. 11,506 Reputation points Microsoft Vendor
2024-09-03T06:25:08.52+00:00

Hi, @mc
Use ellipse

and line.
`point 1` and `point c` are known points. In question, it is `x1,y1` `x,y`

So:

``````#include <iostream>
#include <cmath>
#include <vector>

struct Point {
double x, y;
};

Point findIntersection(double ellipseX, double ellipseY, double MajorAxis, double MinorAxis, double x1, double y1) {
std::vector<Point> intersections;

// cal delta_x , delta_y
double dx = x1 - ellipseX;
double dy = y1 - ellipseY;

double A = (dx * dx) / (MajorAxis * MajorAxis) + (dy * dy) / (MinorAxis * MinorAxis);

if (A <= 1) {

std::cout << "the point is inside or on an ellipse\n";
return Point(x1, y1);
}

double t1 = std::sqrt(1.0 / A);
double t2 = -t1;

//two intersections
intersections.push_back({ ellipseX + t1 * dx, ellipseY + t1 * dy });
intersections.push_back({ ellipseX + t2 * dx, ellipseY + t2 * dy });

for (const auto& intersection : intersections) {

if ((intersection.x - x1) * (ellipseX - intersection.x) >= 0 &&
(intersection.y - y1) * (ellipseY - intersection.y) >= 0) {
std::cout << "Intersection: (" << intersection.x << ", " << intersection.y << ")\n";
return   intersection;
}
}

}

int main() {

double ellipseX = 0.0, ellipseY = 0.0; // center
double MajorAxis = 5.0, MinorAxis = 3.0;
double pX = 6.0, pY = 0.0; // point

Point intersection = findIntersection(ellipseX, ellipseY, MajorAxis, MinorAxis, pX, pY);

return 0;
}
``````

Best regards,

Minxin Yu