how to get the point in ellipse?

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

User's image

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,760 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 12,086 Reputation points Microsoft Vendor
    2024-09-03T06:25:08.52+00:00

    Hi, @mc
    Use ellipse

    User's image

    and line.
    point 1 and point c are known points. In question, it is x1,y1 x,y
    User's image

    So:

    User's image

    #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


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.